git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Rubén Justo" <rjusto@gmail.com>
To: Git List <git@vger.kernel.org>
Subject: [PATCH] path.c: do not alloc for .contents in the trie
Date: Mon, 17 Oct 2022 08:14:59 +0200	[thread overview]
Message-ID: <4db3d1ed-d530-2429-f578-33196ff71fb1@gmail.com> (raw)

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@gmail.com>
---
 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

                 reply	other threads:[~2022-10-17  6:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4db3d1ed-d530-2429-f578-33196ff71fb1@gmail.com \
    --to=rjusto@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).