git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 8/8] cache-tree: avoid path comparison loop when silent

From
René Scharfe <l.s.r@web.de>
Date
Jan 1, 2021, 13:30 UTC
Message-ID
<c9427a35-b2e0-0142-e5c2-4d1baf97315a@web.de>
In-Reply-To
<3ed37ae6-1f09-bd6b-c9d9-c8089da1af92@gmail.com>
Am 31.12.20 um 17:46 schrieb Derrick Stolee:
Show 21 quoted lines
> On 12/31/2020 7:34 AM, René Scharfe wrote:
>> diff --git a/cache-tree.c b/cache-tree.c
>> index a537a806c1..1105cfe6b7 100644
>> --- a/cache-tree.c
>> +++ b/cache-tree.c
>> @@ -187,10 +187,8 @@ static int verify_cache(struct cache_entry **cache,
>>  		 */
>>  		const char *this_name = cache[i]->name;
>>  		const char *next_name = cache[i+1]->name;
>> -		int this_len = strlen(this_name);
>> -		if (this_len < strlen(next_name) &&
>> -		    strncmp(this_name, next_name, this_len) == 0 &&
>> -		    next_name[this_len] == '/') {
>> +		const char *p;
>> +		if (skip_prefix(next_name, this_name, &p) && *p == '/') {
>>  			if (10 < ++funny) {
>>  				fprintf(stderr, "...\n");
>>  				break;
>
> This performs consistently worse than both cases (see performance test
> later in this message). I think the strlen is saving us some time here.

Thanks for checking! I was expecting skip_prefix to be faster, because it stops as soon as it reaches a non-matching character, while strlen has to always walk to the end. But consecutive entries of a sorted list of paths can share long prefixes, in particular if there are long directory names and directories contain lots of files.

Show 55 quoted lines
> In fact, I think the compiler is doing some magic to save our strlen
> results, as I get identical performance results when doing this:
>
> diff --git a/cache-tree.c b/cache-tree.c
> index c6c084463b..a076e7cec5 100644
> --- a/cache-tree.c
> +++ b/cache-tree.c
> @@ -156,6 +156,8 @@ static int verify_cache(struct cache_entry **cache,
>  {
>         int i, funny;
>         int silent = flags & WRITE_TREE_SILENT;
> +       const char *this_name;
> +       size_t this_len;
>
>         /* Verify that the tree is merged */
>         funny = 0;
> @@ -182,18 +184,21 @@ static int verify_cache(struct cache_entry **cache,
>          * and they should not exist unless a bug was introduced along
>          * the way.
>          */
> -       if (silent)
> -               return 0;
>         funny = 0;
> -       for (i = 0; i < entries - 1; i++) {
> +
> +       if (!entries)
> +               return 0;
> +       this_name = cache[0]->name;
> +       this_len = strlen(this_name);
> +
> +       for (i = 1; i < entries; i++) {
>                 /* path/file always comes after path because of the way
>                  * the cache is sorted.  Also path can appear only once,
>                  * which means conflicting one would immediately follow.
>                  */
> -               const char *this_name = cache[i]->name;
> -               const char *next_name = cache[i+1]->name;
> -               int this_len = strlen(this_name);
> -               if (this_len < strlen(next_name) &&
> +               const char *next_name = cache[i]->name;
> +               size_t next_len = strlen(next_name);
> +               if (this_len < next_len &&
>                     strncmp(this_name, next_name, this_len) == 0 &&
>                     next_name[this_len] == '/') {
>                         if (10 < ++funny) {
> @@ -203,6 +208,8 @@ static int verify_cache(struct cache_entry **cache,
>                         fprintf(stderr, "You have both %s and %s\n",
>                                 this_name, next_name);
>                 }
> +               this_name = next_name;
> +               this_len = next_len;
>         }
>         if (funny)
>                 return -1;
>

The compiler can do that because strlen() is a pure function; glibc declares it like this:

   extern size_t strlen (const char *__s)
        __THROW __attribute_pure__ __nonnull ((1));
Show 7 quoted lines
> HOWEVER, if we swap the order of the conditionals to be
>
> 		if (this_len < next_len &&
> 		    strncmp(this_name, next_name, this_len) == 0 &&
> 		    next_name[this_len] == '/') {
>
> then we _do_ get a measurable, consistent speedup.

That's the original order there, but I get it. The trailing slash has only a low probability of being present, so in the common case the strncmp call can be skipped. And I guess that check is easier to handle for the branch predictor as well.

Since we know the length of both strings we can use memcmp instead of strncmp. It can compare words instead of bytes, so I'd expect it to be faster. Checking the slash first should make that difference moot, though, as it probably eliminates most of the strncmp calls anyway.

René
Previous: Derrick StoleeNext: René Scharfe
Message 17 of 53 in “Cleanups around index operations”
  1. 0/8 Cleanups around index operationsDerrick Stolee via GitGitGadget, Dec 30, 2020
  2. 1/8 tree-walk: report recursion countsDerrick Stolee via GitGitGadget, Dec 30, 2020
  3. Elijah NewrenDec 30, 2020
  4. Derrick StoleeDec 30, 2020
  5. 3/8 cache-tree: use trace2 in cache_tree_update()Derrick Stolee via GitGitGadget, Dec 30, 2020
  6. 2/8 unpack-trees: add trace2 regionsDerrick Stolee via GitGitGadget, Dec 30, 2020
  7. Elijah NewrenDec 30, 2020
  8. 4/8 cache-tree: trace regions for I/ODerrick Stolee via GitGitGadget, Dec 30, 2020
  9. 6/8 index-format: update preamble to cached tree extensionDerrick Stolee via GitGitGadget, Dec 30, 2020
  10. Elijah NewrenDec 30, 2020
  11. 8/8 cache-tree: avoid path comparison loop when silentDerrick Stolee via GitGitGadget, Dec 30, 2020
  12. Elijah NewrenDec 30, 2020
  13. Junio C HamanoJan 6, 2021
  14. Derrick StoleeJan 6, 2021
  15. René ScharfeDec 31, 2020
  16. Derrick StoleeDec 31, 2020
  17. René ScharfeJan 1, 2021
  18. cache-tree: use ce_namelen() instead of strlen()René Scharfe, Jan 2, 2021
  19. Derrick StoleeJan 4, 2021
  20. Junio C HamanoJan 5, 2021
  21. René ScharfeJan 2, 2021
  22. 5/8 cache-tree: trace regions for prime_cache_treeDerrick Stolee via GitGitGadget, Dec 30, 2020
  23. Elijah NewrenDec 30, 2020
  24. Derrick StoleeDec 30, 2020
  25. 7/8 index-format: discuss recursion of cached-tree betterDerrick Stolee via GitGitGadget, Dec 30, 2020
  26. Elijah NewrenDec 30, 2020
  27. Derrick StoleeDec 30, 2020
  28. 0/9 Cleanups around index operationsDerrick Stolee via GitGitGadget, Jan 4, 2021
  29. 2/9 unpack-trees: add trace2 regionsDerrick Stolee via GitGitGadget, Jan 4, 2021
  30. 4/9 cache-tree: trace regions for I/ODerrick Stolee via GitGitGadget, Jan 4, 2021
  31. 1/9 tree-walk: report recursion countsDerrick Stolee via GitGitGadget, Jan 4, 2021
  32. 3/9 cache-tree: use trace2 in cache_tree_update()Derrick Stolee via GitGitGadget, Jan 4, 2021
  33. 5/9 cache-tree: trace regions for prime_cache_treeDerrick Stolee via GitGitGadget, Jan 4, 2021
  34. 6/9 index-format: update preamble to cached tree extensionDerrick Stolee via GitGitGadget, Jan 4, 2021
  35. Junio C HamanoJan 7, 2021
  36. Derrick StoleeJan 7, 2021
  37. Junio C HamanoJan 7, 2021
  38. Junio C HamanoJan 7, 2021
  39. 7/9 index-format: discuss recursion of cached-tree betterDerrick Stolee via GitGitGadget, Jan 4, 2021
  40. 9/9 cache-tree: speed up consecutive path comparisonsDerrick Stolee via GitGitGadget, Jan 4, 2021
  41. 8/9 cache-tree: use ce_namelen() instead of strlen()René Scharfe via GitGitGadget, Jan 4, 2021
  42. 00/10 Cleanups around index operationsDerrick Stolee via GitGitGadget, Jan 7, 2021
  43. 01/10 tree-walk: report recursion countsDerrick Stolee via GitGitGadget, Jan 7, 2021
  44. 02/10 unpack-trees: add trace2 regionsDerrick Stolee via GitGitGadget, Jan 7, 2021
  45. 03/10 cache-tree: use trace2 in cache_tree_update()Derrick Stolee via GitGitGadget, Jan 7, 2021
  46. 04/10 cache-tree: trace regions for I/ODerrick Stolee via GitGitGadget, Jan 7, 2021
  47. 06/10 index-format: use 'cache tree' over 'cached tree'Derrick Stolee via GitGitGadget, Jan 7, 2021
  48. 08/10 index-format: discuss recursion of cached-tree betterDerrick Stolee via GitGitGadget, Jan 7, 2021
  49. 09/10 cache-tree: use ce_namelen() instead of strlen()René Scharfe via GitGitGadget, Jan 7, 2021
  50. 10/10 cache-tree: speed up consecutive path comparisonsDerrick Stolee via GitGitGadget, Jan 7, 2021
  51. 05/10 cache-tree: trace regions for prime_cache_treeDerrick Stolee via GitGitGadget, Jan 7, 2021
  52. 07/10 index-format: update preamble to cache tree extensionDerrick Stolee via GitGitGadget, Jan 7, 2021
  53. Junio C HamanoJan 16, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.