mu: optimize indexing (get_uid_term)

Some users were report seeing get_uid_term high in the profiles; so
optimize this:

- make mu_util_get_hash a static inline function (used by get_uid_term)
- don't use 'realpath' in get_uid_term, seem that's the main culprit
- some slight faster string handling there too.
This commit is contained in:
djcb 2015-11-17 10:55:56 +02:00
parent dba2e50979
commit fe8b3430c6
4 changed files with 26 additions and 39 deletions

View File

@ -177,7 +177,8 @@ public:
mu_contacts_clear (_contacts);
}
std::string get_uid_term (const char *path) const;
// not re-entrant; stays valid until called again
const char *get_uid_term (const char *path) const;
MuContacts* contacts() { return _contacts; }

View File

@ -44,24 +44,16 @@
// note: not re-entrant
std::string
const char*
_MuStore::get_uid_term (const char* path) const
{
char real_path[PATH_MAX + 1];
static char uid_term[64] = { '\0' };
if (G_UNLIKELY(uid_term[0] == '\0'))
uid_term[0] = mu_msg_field_xapian_prefix(MU_MSG_FIELD_ID_UID);
static const std::string uid_prefix (
1, mu_msg_field_xapian_prefix(MU_MSG_FIELD_ID_UID));
strncpy (uid_term + 1, mu_util_get_hash (path), sizeof(uid_term) - 1);
/* check profile to see if realpath is expensive; we need
* realpath here (and in mu-msg-file) to ensure that the same
* messages are only considered ones (ignore e.g. symlinks and
* '//' in paths) */
// note: realpath fails when there's no file at path
if (!realpath (path, real_path))
strcpy (real_path, path);
return std::string (uid_prefix + mu_util_get_hash (real_path));
return uid_term;
}
@ -72,7 +64,6 @@ mu_store_new_read_only (const char* xpath, GError **err)
try {
return new _MuStore (xpath);
} catch (const MuStoreError& merr) {
mu_util_g_set_error (err, merr.mu_error(), "%s",
merr.what().c_str());

View File

@ -559,25 +559,3 @@ mu_util_read_password (const char *prompt)
}
const char*
mu_util_get_hash (const char* str)
{
unsigned djbhash, bkdrhash, bkdrseed;
unsigned u;
static char hex[18];
g_return_val_if_fail (str, NULL);
djbhash = 5381;
bkdrhash = 0;
bkdrseed = 1313;
for(u = 0; str[u]; ++u) {
djbhash = ((djbhash << 5) + djbhash) + str[u];
bkdrhash = bkdrhash * bkdrseed + str[u];
}
snprintf (hex, sizeof(hex), "%08x%08x", djbhash, bkdrhash);
return hex;
}

View File

@ -513,9 +513,26 @@ gboolean mu_util_g_set_error (GError **err, MuError errcode, const char *frm, ..
* @return the hash as a static string, which stays valid until this
* function is called again.
*/
const char* mu_util_get_hash (const char* str);
static inline const char*
mu_util_get_hash (const char* str)
{
unsigned djbhash, bkdrhash, bkdrseed;
unsigned u;
static char hex[18];
djbhash = 5381;
bkdrhash = 0;
bkdrseed = 1313;
for(u = 0; str[u]; ++u) {
djbhash = ((djbhash << 5) + djbhash) + str[u];
bkdrhash = bkdrhash * bkdrseed + str[u];
}
snprintf (hex, sizeof(hex), "%08x%08x", djbhash, bkdrhash);
return hex;
}
#define MU_COLOR_RED "\x1b[31m"