path.c: do not alloc for .contents in the trie
PATCH 1/1 path.c: do not alloc for .contents in the trie
In 4e09cf2ac (path: optimize common dir checking, 2015-08-31) a trie was introduced to search non-linearly over common_list. The nodes in the trie are constructed from the values in that static variable. Each of those nodes can have in .contents a heap allocated string with the characters to be considered for the node, and in .len the count of this characters. Having this .len there is no need to have a local copy of the string, we can just have in .contents a pointer to a specific position in the specific string in the specific value in common_list. Only .len characters will be considered.
Let's make .contents const and remove that unneeded xstrndups.
Signed-off-by Rubén Justo <[email protected]>
--- path.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-)
path.c +5 -9
@@ -159,7 +159,7 @@ static struct common_dir common_list[] = {struct trie {struct trie *children[256 ];int len; -char *contents; +const char *contents;void *value; };
@@ -167,10 +167,8 @@ static struct trie *make_trie_node(const char *key, void *value) {struct trie *new_node =xcalloc (1 ,sizeof (*new_node)); new_node->len =strlen (key); -if (new_node->len) { - new_node->contents =xmalloc (new_node->len); -memcpy (new_node->contents, key, new_node->len); - } +if (new_node->len) + new_node->contents = key; new_node->value = value;return new_node; }
@@ -204,10 +202,8 @@ static void *add_to_trie(struct trie *root, const char *key, void *value)memcpy (child->children, root->children,sizeof (root->children)); child->len = root->len - i -1 ; -if (child->len) { - child->contents =xstrndup (root->contents + i +1 , - child->len); - } +if (child->len) + child->contents = root->contents + i +1 ; child->value = root->value; root->value =NULL ; root->len = i;
Signature · 1 line
2.36.1