Optimize common pattern of alloc_ref from string
PATCH 1/1 Optimize common pattern of alloc_ref from string
From: Krzysztof Kowalczyk <[email protected]>
As a byproduct, fixes one place where string wasn't properly terminated.
--- builtin-fetch.c | 6 ++---- http-push.c | 6 ++---- remote.c | 28 +++++++++++++++------------- remote.h | 2 ++ transport.c | 6 ++---- walker.c | 3 +-- 6 files changed, 24 insertions(+), 27 deletions(-)
builtin-fetch.c +2 -4
@@ -508,10 +508,8 @@ static void find_non_local_tags(struct transport *transport, will_fetch(head, ref->old_sha1))) {path_list_insert (ref_name, &new_refs); - rm =alloc_ref (strlen (ref_name) +1 ); -strcpy (rm->name, ref_name); - rm->peer_ref =alloc_ref (strlen (ref_name) +1 ); -strcpy (rm->peer_ref->name, ref_name); + rm =alloc_ref_from_str (ref_name); + rm->peer_ref =alloc_ref_from_str (ref_name);hashcpy (rm->old_sha1, ref_sha1); **tail = rm;
http-push.c +2 -4
@@ -1761,8 +1761,7 @@ static void one_remote_ref(char *refname)struct ref *ref;struct object *obj; - ref =alloc_ref (strlen (refname) +1 ); -strcpy (ref->name, refname); + ref =alloc_ref_from_str (refname);if (http_fetch_ref (remote->url, ref) !=0 ) { fprintf(stderr,
@@ -1894,8 +1893,7 @@ static void add_remote_info_ref(struct remote_ls_ctx *ls)char *ref_info;struct ref *ref; - ref =alloc_ref (strlen (ls->dentry_name) +1 ); -strcpy (ref->name, ls->dentry_name); + ref =alloc_ref_from_str (ls->dentry_name);if (http_fetch_ref (remote->url, ref) !=0 ) { fprintf(stderr,
remote.c +15 -13
@@ -691,6 +691,18 @@ struct ref *alloc_ref(unsigned namelen)return ret; } +struct ref *alloc_ref_from_str (const char * str) +{ +struct ref *ret; +unsigned len =strlen (str) +1 ; +char *tmp =xmalloc (sizeof (struct ref ) + len); + ret = (struct ref *)tmp; +memset (tmp,0 ,sizeof (struct ref )); + tmp +=sizeof (struct ref ); +memcpy (tmp, str, len); +return ret; +} +static struct ref *copy_ref (const struct ref *ref) {struct ref *ret =xmalloc (sizeof (struct ref ) +strlen (ref->name) +1 );
@@ -797,7 +809,6 @@ static struct ref *try_explicit_object_name(const char *name) {unsigned char sha1[20 ];struct ref *ref; -int len;if (!*name) { ref =alloc_ref (20 );
@@ -807,21 +818,14 @@ static struct ref *try_explicit_object_name(const char *name) }if (get_sha1 (name, sha1))return NULL ; - len =strlen (name) +1 ; - ref =alloc_ref (len); -memcpy (ref->name, name, len); + ref =alloc_ref_from_str (name);hashcpy (ref->new_sha1, sha1);return ref; }static struct ref *make_linked_ref (const char *name,struct ref ***tail) { -struct ref *ret; -size_t len; - - len =strlen (name) +1 ; - ret =alloc_ref (len); -memcpy (ret->name, name, len); +struct ref *ret =alloc_ref_from_str (name);tail_link_ref (ret, tail);return ret; }
@@ -1125,9 +1129,7 @@ static struct ref *get_local_ref(const char *name)return NULL ;if (!prefixcmp (name,"refs/" )) { - ret =alloc_ref (strlen (name) +1 ); -strcpy (ret->name, name); -return ret; +return alloc_ref_from_str (name); } if (!prefixcmp(name, "heads/") ||
remote.h +2 -0
@@ -54,6 +54,8 @@ struct refspec {struct ref *alloc_ref (unsigned namelen); +struct ref *alloc_ref_from_str (const char * str); +struct ref *copy_ref_list (const struct ref *ref);int check_ref_type (const struct ref *ref,int flags);
transport.c +2 -4
@@ -504,8 +504,7 @@ static struct ref *get_refs_via_curl(struct transport *transport)strbuf_release (&buffer); - ref =alloc_ref (strlen ("HEAD" ) +1 ); -strcpy (ref->name,"HEAD" ); + ref =alloc_ref_from_str ("HEAD" );if (!walker->fetch_ref (walker, ref) && !resolve_remote_symref (ref, refs)) { ref->next = refs;
@@ -546,9 +545,8 @@ static struct ref *get_refs_from_bundle(struct transport *transport)die ("Could not read bundle '%s'." , transport->url);for (i =0 ; i < data->header.references.nr; i++) {struct ref_list_entry *e = data->header.references.list + i; -struct ref *ref =alloc_ref (strlen (e->name) +1 ); +struct ref *ref =alloc_ref_from_str (e->name);hashcpy (ref->old_sha1, e->sha1); -strcpy (ref->name, e->name); ref->next = result; result = ref; }
walker.c +1 -2
@@ -190,8 +190,7 @@ static int interpret_target(struct walker *walker, char *target, unsigned char *if (!get_sha1_hex (target, sha1))return 0 ;if (!check_ref_format (target)) { -struct ref *ref =alloc_ref (strlen (target)); -strcpy (ref->name, target); +struct ref *ref =alloc_ref_from_str (target);if (!walker->fetch_ref (walker, ref)) {hashcpy (sha1, ref->old_sha1);free (ref);
Signature · 1 line
1.5.4.5
Quoted 1 line · On Sat, May 10, 2008 at 04:26:58PM -0700, [email protected] wrote:
As a byproduct, fixes one place where string wasn't properly terminated.
Great. Does this fix a user-visible bug? It would be nice to mention in the commit log _which_ place (though after reading the patch carefully, it looks like the one interpret_target) so that people looking at the commit later can understand exactly what was fixed.
http-push.c static void one_remote_ref(char *refname) · line 1764
Quoted 3 lines
http-push.c +1
-2- ref =alloc_ref (strlen (refname) +1 ); -strcpy (ref->name, refname); + ref =alloc_ref_from_str (refname);
So this turns a 2-line construct into a 1-line construct...
remote.c struct ref *alloc_ref(unsigned namelen) · line 704
Quoted 11 lines
remote.c +11
-0+struct ref *alloc_ref_from_str (const char * str) +{ +struct ref *ret; +unsigned len =strlen (str) +1 ; +char *tmp =xmalloc (sizeof (struct ref ) + len); + ret = (struct ref *)tmp; +memset (tmp,0 ,sizeof (struct ref )); + tmp +=sizeof (struct ref ); +memcpy (tmp, str, len); +return ret; +}
But why do we need an 8-line function to do it?
The only difference I can see over
struct ref *alloc_ref_from_str(const char *str)
{
unsigned len = strlen(str) + 1;
struct ref *ret = alloc_ref(len);
memcpy(ret->name, str, len);
return ret;
}is that we avoid memsetting the name portion of the struct to 0 before copying to it. It seems like an unproven micro-optimization that makes it a bit harder to read.
-Peff
Quoted 8 lines · Jeff King
Quoted 1 line · On Sat, May 10, 2008 at 04:26:58PM -0700, [email protected] wrote:
As a byproduct, fixes one place where string wasn't properly terminated.
Great. Does this fix a user-visible bug? It would be nice to mention in the commit log _which_ place (though after reading the patch carefully, it looks like the one
interpret_target) so that people looking at the commit later can understand exactly what was fixed.
It was a subtle memory corruption that wouldn't cause problems in 99.99% cases, but valgrind would probably catch it. And yes, it's the interp_target().
Quoted 5 lines
http-push.c static void one_remote_ref(char *refname) · line 1764
Quoted 3 lines
http-push.c +1
-2- ref =alloc_ref (strlen (refname) +1 ); -strcpy (ref->name, refname); + ref =alloc_ref_from_str (refname);So this turns a 2-line construct into a 1-line construct...
And avoids future prossible mistakes with not terminating the string, like the one just commited.
Quoted 22 lines
remote.c struct ref *alloc_ref(unsigned namelen) · line 704
Quoted 11 lines
remote.c +11
-0+struct ref *alloc_ref_from_str (const char * str) +{ +struct ref *ret; +unsigned len =strlen (str) +1 ; +char *tmp =xmalloc (sizeof (struct ref ) + len); + ret = (struct ref *)tmp; +memset (tmp,0 ,sizeof (struct ref )); + tmp +=sizeof (struct ref ); +memcpy (tmp, str, len); +return ret; +}But why do we need an 8-line function to do it?
The only difference I can see over
struct ref *alloc_ref_from_str(const char *str)
{ unsigned len = strlen(str) + 1; struct ref *ret = alloc_ref(len); memcpy(ret->name, str, len); return ret;
Quoted 5 lines
}
is that we avoid memsetting the name portion of the struct to 0 before copying to it. It seems like an unproven micro-optimization that makes it a bit harder to read.
You're absolutely right - it's a micro-optimization and your version might be preferred for clarity. This is the first time I submit a patch to git so I don't have a good feel for what kind of treadoffs people find acceptable.
I should also mention that
static struct ref *try_explicit_object_name(const char *name)
{
unsigned char sha1[20];
struct ref *ref;
if (!*name) {
ref = alloc_ref(20);
strcpy(ref->name, "(delete)");
hashclr(ref->new_sha1);
return ref;
}...
could also be replaced with alloc_ref_str() - I just wasn't 100% sure if overallocating 10 bytes (20 - strlen("(delete)")) was just sloppy code or does other code relies on that (which is unlikely and if true then it wouldn't be good).
Regards,
-- kjk
Quoted 8 lines · Krzysztof Kowalczyk
Quoted 5 lines
http-push.c static void one_remote_ref(char *refname) · line 1764
Quoted 3 lines
http-push.c +1
-2- ref =alloc_ref (strlen (refname) +1 ); -strcpy (ref->name, refname); + ref =alloc_ref_from_str (refname);So this turns a 2-line construct into a 1-line construct...
And avoids future prossible mistakes with not terminating the string, like the one just commited.
Yes. Please don't interpret my comment as "this change isn't worth it"; I meant it as "yes, it is good to be simplifying and making this part less error prone; I just want to nitpick your exact implementation".
Quoted 4 lines
You're absolutely right - it's a micro-optimization and your version might be preferred for clarity. This is the first time I submit a patch to git so I don't have a good feel for what kind of treadoffs people find acceptable.
OK. Ultimately it is up to Junio. I think your version is a bit complex, but at the very least it contains that complexity neatly in one function.
Actually, the version I posted actually has an optimization, too (it remembers the strlen calculation to reuse it). I think the simplest would just be:
struct ref *alloc_ref_from_str(cons char *str)
{
struct ref *ret = alloc_ref(strlen(str) + 1);
strcpy(ret->name, str);
return ret;
}But really my main worry is that now we have _two_ functions which allocate refs, so if "struct ref" ever grows a new field that needs initializing, it has to go in two places (whereas if alloc_ref_from_str calls alloc_ref, it works automatically).
Quoted 12 lines
I should also mention that
static struct ref *try_explicit_object_name(const char *name){ unsigned char sha1[20]; struct ref *ref; if (!*name) { ref = alloc_ref(20); strcpy(ref->name, "(delete)"); hashclr(ref->new_sha1); return ref; }
Quoted 6 lines
...
could also be replaced with
alloc_ref_str()- I just wasn't 100% sure if overallocating 10 bytes (20 - strlen("(delete)")) was just sloppy code or does other code relies on that (which is unlikely and if true then it wouldn't be good).
It looks like just slop, and should probably be fixed at the same time. There are also quite a number of:
ret = alloc_ref(strlen(name) + 6); sprintf(ret->name, "refs/%s", name);
which are error-prone and owuld be nice to fix. However, I don't think there is an easy way short of making ref->name a strbuf. And now it seems we are getting into a lot of code churn for a relatively small benefit.
-Peff
Quoted 4 lines · Jeff King
But really my main worry is that now we have _two_ functions which allocate refs, so if "struct ref" ever grows a new field that needs initializing, it has to go in two places (whereas if
alloc_ref_from_strcallsalloc_ref, it works automatically).
This is a very good point. We really do not want a micro-optimization in a way that hurts maintainability.
Krzysztof's patch has the new function with the duplicated allocation implementation between the base allocator function and the existing copy_ref() function which also has yet another duplicated allocation implementation. When somebody needs to modify one, it is likely to be noticed that these three go hand-in-hand, hopefully ;-).
By the way, why isn't alloc_ref() doing xcalloc(), I have to wonder...
Quoted 10 lines · Junio C Hamano
Quoted 4 lines · Jeff King
But really my main worry is that now we have _two_ functions which allocate refs, so if "struct ref" ever grows a new field that needs initializing, it has to go in two places (whereas if
alloc_ref_from_strcallsalloc_ref, it works automatically).This is a very good point. We really do not want a micro-optimization in a way that hurts maintainability.
Quoted 7 lines
Krzysztof's patch has the new function with the duplicated allocation implementation between the base allocator function and the existing
copy_ref()function which also has yet another duplicated allocation implementation. When somebody needs to modify one, it is likely to be noticed that these three go hand-in-hand, hopefully ;-).By the way, why isn't
alloc_ref()doingxcalloc(), I have to wonder...
I've sent updated patch that incorporates the feedback. Changes wrt. to this patch:
* simpler version of alloc_ref_from_str() per Jeff's suggestion
* switches alloc_ref() to xcalloc() per Junio's suggestion
* uses alloc_ref_from_str() in one more place
* better (?) commit message
-- kjk
Thanks. I've already queued this instead, as it is not about "Optimization" but about refactoring.
I am actually very tempted to relabel it as "Fix underallocation in interpret_target()" and describe the refactoring just as a side-effect, but the commit is already buried deep in my outgoing queue for "master", and I do not think it is worth delaying today's pushout for rewording the commit log message again.
-- >8 --
From: Krzysztof Kowalczyk <[email protected]>
Date: Sat, 10 May 2008 16:26:58 -0700
Subject: [PATCH] alloc_ref_from_str(): factor out a common pattern of alloc_ref from string
Also fix an underallocation in walker.c::interpret_target().
--- builtin-fetch.c | 6 ++---- http-push.c | 6 ++---- remote.c | 23 ++++++++++------------- remote.h | 2 ++ transport.c | 6 ++---- walker.c | 3 +-- 6 files changed, 19 insertions(+), 27 deletions(-)
builtin-fetch.c +2 -4
@@ -508,10 +508,8 @@ static void find_non_local_tags(struct transport *transport, will_fetch(head, ref->old_sha1))) {path_list_insert (ref_name, &new_refs); - rm =alloc_ref (strlen (ref_name) +1 ); -strcpy (rm->name, ref_name); - rm->peer_ref =alloc_ref (strlen (ref_name) +1 ); -strcpy (rm->peer_ref->name, ref_name); + rm =alloc_ref_from_str (ref_name); + rm->peer_ref =alloc_ref_from_str (ref_name);hashcpy (rm->old_sha1, ref_sha1); **tail = rm;
http-push.c +2 -4
@@ -1761,8 +1761,7 @@ static void one_remote_ref(char *refname)struct ref *ref;struct object *obj; - ref =alloc_ref (strlen (refname) +1 ); -strcpy (ref->name, refname); + ref =alloc_ref_from_str (refname);if (http_fetch_ref (remote->url, ref) !=0 ) { fprintf(stderr,
@@ -1894,8 +1893,7 @@ static void add_remote_info_ref(struct remote_ls_ctx *ls)char *ref_info;struct ref *ref; - ref =alloc_ref (strlen (ls->dentry_name) +1 ); -strcpy (ref->name, ls->dentry_name); + ref =alloc_ref_from_str (ls->dentry_name);if (http_fetch_ref (remote->url, ref) !=0 ) { fprintf(stderr,
remote.c +10 -13
@@ -691,6 +691,13 @@ struct ref *alloc_ref(unsigned namelen)return ret; } +struct ref *alloc_ref_from_str (const char * str) +{ +struct ref *ret =alloc_ref (strlen (str) +1 ); +strcpy (ret->name, str); +return ret; +} +static struct ref *copy_ref (const struct ref *ref) {struct ref *ret =xmalloc (sizeof (struct ref ) +strlen (ref->name) +1 );
@@ -797,7 +804,6 @@ static struct ref *try_explicit_object_name(const char *name) {unsigned char sha1[20 ];struct ref *ref; -int len;if (!*name) { ref =alloc_ref (20 );
@@ -807,21 +813,14 @@ static struct ref *try_explicit_object_name(const char *name) }if (get_sha1 (name, sha1))return NULL ; - len =strlen (name) +1 ; - ref =alloc_ref (len); -memcpy (ref->name, name, len); + ref =alloc_ref_from_str (name);hashcpy (ref->new_sha1, sha1);return ref; }static struct ref *make_linked_ref (const char *name,struct ref ***tail) { -struct ref *ret; -size_t len; - - len =strlen (name) +1 ; - ret =alloc_ref (len); -memcpy (ret->name, name, len); +struct ref *ret =alloc_ref_from_str (name);tail_link_ref (ret, tail);return ret; }
@@ -1125,9 +1124,7 @@ static struct ref *get_local_ref(const char *name)return NULL ;if (!prefixcmp (name,"refs/" )) { - ret =alloc_ref (strlen (name) +1 ); -strcpy (ret->name, name); -return ret; +return alloc_ref_from_str (name); } if (!prefixcmp(name, "heads/") ||
remote.h +2 -0
@@ -54,6 +54,8 @@ struct refspec {struct ref *alloc_ref (unsigned namelen); +struct ref *alloc_ref_from_str (const char * str); +struct ref *copy_ref_list (const struct ref *ref);int check_ref_type (const struct ref *ref,int flags);
transport.c +2 -4
@@ -504,8 +504,7 @@ static struct ref *get_refs_via_curl(struct transport *transport)strbuf_release (&buffer); - ref =alloc_ref (strlen ("HEAD" ) +1 ); -strcpy (ref->name,"HEAD" ); + ref =alloc_ref_from_str ("HEAD" );if (!walker->fetch_ref (walker, ref) && !resolve_remote_symref (ref, refs)) { ref->next = refs;
@@ -546,9 +545,8 @@ static struct ref *get_refs_from_bundle(struct transport *transport)die ("Could not read bundle '%s'." , transport->url);for (i =0 ; i < data->header.references.nr; i++) {struct ref_list_entry *e = data->header.references.list + i; -struct ref *ref =alloc_ref (strlen (e->name) +1 ); +struct ref *ref =alloc_ref_from_str (e->name);hashcpy (ref->old_sha1, e->sha1); -strcpy (ref->name, e->name); ref->next = result; result = ref; }
walker.c +1 -2
@@ -190,8 +190,7 @@ static int interpret_target(struct walker *walker, char *target, unsigned char *if (!get_sha1_hex (target, sha1))return 0 ;if (!check_ref_format (target)) { -struct ref *ref =alloc_ref (strlen (target)); -strcpy (ref->name, target); +struct ref *ref =alloc_ref_from_str (target);if (!walker->fetch_ref (walker, ref)) {hashcpy (sha1, ref->old_sha1);free (ref);
Signature · 1 line
1.5.5.1.294.g7b425
Hi,
Quoted 5 lines · On Sat, 10 May 2008, [email protected] wrote:
builtin-fetch.c +0
-0
builtin-fetch.c static void find_non_local_tags(struct transport *transport, · line 512
Quoted 9 lines
builtin-fetch.c +2
-4@@ -508,10 +508,8 @@ static void find_non_local_tags(struct transport *transport, will_fetch(head, ref->old_sha1))) {path_list_insert (ref_name, &new_refs); - rm =alloc_ref (strlen (ref_name) +1 ); -strcpy (rm->name, ref_name); - rm->peer_ref =alloc_ref (strlen (ref_name) +1 ); -strcpy (rm->peer_ref->name, ref_name); + rm =alloc_ref_from_str (ref_name); + rm->peer_ref =alloc_ref_from_str (ref_name);
Thanks. This was on my ever-growing TODO list (actually, fix alloc_ref() to take the string length instead of the size to be allocated).
Together with the comments from Peff I am very positive on this patch.
Thanks,
Dscho