summaryrefslogtreecommitdiff
path: root/src/atom.c
diff options
context:
space:
mode:
authorRan Benita <ran234@gmail.com>2012-07-23 16:03:34 +0300
committerRan Benita <ran234@gmail.com>2012-07-27 00:01:41 +0300
commit112cccb18ad1bc877b3c4a87fa536ea085c761b5 (patch)
tree75a76da2d1b06752956a105de54c979d4a0e36b6 /src/atom.c
parentc6279b8baeae7dd9ff486bc0fb60ff0bef0587af (diff)
downloadxorg-lib-libxkbcommon-112cccb18ad1bc877b3c4a87fa536ea085c761b5.tar.gz
Some atom related optimizations
We often get a strdup'd string, just to pass it over the atom_intern and then immediately free it. But atom_intern then strdup's it again (if it's not interned already); so instead we can have the interning "steal" the memory instead of allocing a new one and freeing the old one. This is done by a new xkb_atom_steal function. It also turns out, that every time we strdup an atom, we don't actually modify it afterwards. Since we are guaranteed that the atom table will live as long as the context, we can just use xkb_atom_text instead. This removes a some more dynamic allocations. For this change we had to remove the ability to append two strings, e.g. "foo" + "bar" -> "foobar" which is only possible with string literals. This is unused and quite useless for our purposes. xkb_atom_strdup is left unused, as it may still be useful. Running rulescomp in valgrind, Before: ==7907== total heap usage: 173,698 allocs, 173,698 frees, 9,775,973 bytes allocated After: ==6348== total heap usage: 168,403 allocs, 168,403 frees, 9,732,648 bytes allocated Signed-off-by: Ran Benita <ran234@gmail.com>
Diffstat (limited to 'src/atom.c')
-rw-r--r--src/atom.c34
1 files changed, 24 insertions, 10 deletions
diff --git a/src/atom.c b/src/atom.c
index 1d48d70..c5e0606 100644
--- a/src/atom.c
+++ b/src/atom.c
@@ -140,8 +140,14 @@ atom_strdup(struct atom_table *table, xkb_atom_t atom)
return ret ? strdup(ret) : NULL;
}
+/*
+ * If steal is true, we do not strdup @string; therefore it must be
+ * dynamically allocated, not be free'd by the caller and not be used
+ * afterwards. Use to avoid some redundant allocations.
+ */
xkb_atom_t
-atom_intern(struct atom_table *table, const char *string)
+atom_intern(struct atom_table *table, const char *string,
+ bool steal)
{
struct atom_node **np;
struct atom_node *nd;
@@ -168,12 +174,17 @@ atom_intern(struct atom_table *table, const char *string)
else {
/* now start testing the strings */
comp = strncmp(string, (*np)->string, len);
- if ((comp < 0) || ((comp == 0) && (len < strlen((*np)->string))))
+ if (comp < 0 || (comp == 0 && len < strlen((*np)->string))) {
np = &((*np)->left);
- else if (comp > 0)
+ }
+ else if (comp > 0) {
np = &((*np)->right);
- else
+ }
+ else {
+ if (steal)
+ free(UNCONSTIFY(string));
return (*np)->a;
+ }
}
}
@@ -181,13 +192,16 @@ atom_intern(struct atom_table *table, const char *string)
if (!nd)
return XKB_ATOM_NONE;
- nd->string = malloc(len + 1);
- if (!nd->string) {
- free(nd);
- return XKB_ATOM_NONE;
+ if (steal) {
+ nd->string = UNCONSTIFY(string);
+ }
+ else {
+ nd->string = strdup(string);
+ if (!nd->string) {
+ free(nd);
+ return XKB_ATOM_NONE;
+ }
}
- strncpy(nd->string, string, len);
- nd->string[len] = 0;
*np = nd;
nd->left = nd->right = NULL;