* [PATCH] path.c: do not alloc for .contents in the trie
@ 2022-10-17 6:14 Rubén Justo
0 siblings, 0 replies; only message in thread
From: Rubén Justo @ 2022-10-17 6:14 UTC (permalink / raw)
To: Git List
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
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2022-10-17 6:15 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-17 6:14 [PATCH] path.c: do not alloc for .contents in the trie Rubén Justo
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).