[PATCH] path.c: do not alloc for .contents in the trie

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 <rjusto@xxxxxxxxx>
---
 path.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/path.c b/path.c
index a3cfcd8a6e..94483d98f9 100644
--- a/path.c
+++ b/path.c
@@ -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;
-- 
2.36.1



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux