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

Re: [PATCH v2 24/25] sparse-index: expand_to_path()

From
Elijah Newren <newren@gmail.com>
Date
Apr 5, 2021, 19:32 UTC
Message-ID
<CABPp-BE5xLMsVdamAb=MgiBnXKQrYDPJXa1TWutDK+nVYRPyRQ@mail.gmail.com>
In-Reply-To
<d52c72b4a7b9a3ae10bd2daae85c1bd1bafcc2f3.1617241803.git.gitgitgadget@gmail.com>

On Wed, Mar 31, 2021 at 6:50 PM Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com> wrote:

Show 110 quoted lines
>
> From: Derrick Stolee <dstolee@microsoft.com>
>
> Some users of the index API have a specific path they are looking for,
> but choose to use index_file_exists() to rely on the name-hash hashtable
> instead of doing binary search with index_name_pos(). These users only
> need to know a yes/no answer, not a position within the cache array.
>
> When the index is sparse, the name-hash hash table does not contain the
> full list of paths within sparse directories. It _does_ contain the
> directory names for the sparse-directory entries.
>
> Create a helper function, expand_to_path(), for intended use with the
> name-hash hashtable functions. The integration with name-hash.c will
> follow in a later change.
>
> The solution here is to use ensure_full_index() when we determine that
> the requested path is within a sparse directory entry. This will
> populate the name-hash hashtable as the index is recomputed from
> scratch.
>
> There may be cases where the caller is trying to find an untracked path
> that is not in the index but also is not within a sparse directory
> entry. We want to minimize the overhead for these requests. If we used
> index_name_pos() to find the insertion order of the path, then we could
> determine from that position if a sparse-directory exists. (In fact,
> just calling index_name_pos() in that case would lead to expanding the
> index to a full index.) However, this takes O(log N) time where N is the
> number of cache entries.
>
> To keep the performance of this call based mostly on the input string,
> use index_file_exists() to look for the ancestors of the path. Using the
> heuristic that a sparse directory is likely to have a small number of
> parent directories, we start from the bottom and build up. Use a string
> buffer to allow mutating the path name to terminate after each slash for
> each hashset test.
>
> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
> ---
>  sparse-index.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++
>  sparse-index.h | 13 +++++++++
>  2 files changed, 85 insertions(+)
>
> diff --git a/sparse-index.c b/sparse-index.c
> index 95ea17174da3..8a1223041296 100644
> --- a/sparse-index.c
> +++ b/sparse-index.c
> @@ -283,3 +283,75 @@ void ensure_full_index(struct index_state *istate)
>
>         trace2_region_leave("index", "ensure_full_index", istate->repo);
>  }
> +
> +/*
> + * This static global helps avoid infinite recursion between
> + * expand_to_path() and index_file_exists().
> + */
> +static int in_expand_to_path = 0;
> +
> +void expand_to_path(struct index_state *istate,
> +                   const char *path, size_t pathlen, int icase)
> +{
> +       struct strbuf path_mutable = STRBUF_INIT;
> +       size_t substr_len;
> +
> +       /* prevent extra recursion */
> +       if (in_expand_to_path)
> +               return;
> +
> +       if (!istate || !istate->sparse_index)
> +               return;
> +
> +       if (!istate->repo)
> +               istate->repo = the_repository;
> +
> +       in_expand_to_path = 1;
> +
> +       /*
> +        * We only need to actually expand a region if the
> +        * following are both true:
> +        *
> +        * 1. 'path' is not already in the index.
> +        * 2. Some parent directory of 'path' is a sparse directory.
> +        */
> +
> +       if (index_file_exists(istate, path, pathlen, icase))
> +               goto cleanup;
> +
> +       strbuf_add(&path_mutable, path, pathlen);
> +       strbuf_addch(&path_mutable, '/');
> +
> +       /* Check the name hash for all parent directories */
> +       substr_len = 0;
> +       while (substr_len < pathlen) {
> +               char temp;
> +               char *replace = strchr(path_mutable.buf + substr_len, '/');
> +
> +               if (!replace)
> +                       break;
> +
> +               /* replace the character _after_ the slash */
> +               replace++;
> +               temp = *replace;
> +               *replace = '\0';
> +               if (index_file_exists(istate, path_mutable.buf,
> +                                     path_mutable.len, icase)) {
> +                       /*
> +                        * We found a parent directory in the name-hash
> +                        * hashtable, which means that this entry could
> +                        * exist within a sparse-directory entry. Expand
> +                        * accordingly.

"this entry" is a bit unclear; it might be worth referring to the "path" variable instead. I think it'd also help to explain the reasoning for using the '/' character, because while it's clear when looking at the series, just running across it in the code it might be easy to forget or miss. Perhaps:

                    * We found a parent directory in the name-hash
                    * hashtable, because only sparse directory entries
                    * have a trailing '/' character.  Since "path" wasn't
                    * in the index, perhaps it exists within this
                    * sparse-directory.  Expand accordingly.
Show 39 quoted lines
> +                        */
> +                       ensure_full_index(istate);
> +                       break;
> +               }
> +
> +               *replace = temp;
> +               substr_len = replace - path_mutable.buf;
> +       }
> +
> +cleanup:
> +       strbuf_release(&path_mutable);
> +       in_expand_to_path = 0;
> +}
> diff --git a/sparse-index.h b/sparse-index.h
> index 0268f38753c0..1115a0d7dd98 100644
> --- a/sparse-index.h
> +++ b/sparse-index.h
> @@ -4,6 +4,19 @@
>  struct index_state;
>  int convert_to_sparse(struct index_state *istate);
>
> +/*
> + * Some places in the codebase expect to search for a specific path.
> + * This path might be outside of the sparse-checkout definition, in
> + * which case a sparse-index may not contain a path for that index.
> + *
> + * Given an index and a path, check to see if a leading directory for
> + * 'path' exists in the index as a sparse directory. In that case,
> + * expand that sparse directory to a full range of cache entries and
> + * populate the index accordingly.
> + */
> +void expand_to_path(struct index_state *istate,
> +                   const char *path, size_t pathlen, int icase);
> +
>  struct repository;
>  int set_sparse_index_config(struct repository *repo, int enable);
>
> --
> gitgitgadget
Previous: Derrick Stolee via GitGitGadgetNext: Derrick Stolee
Message 74 of 111 in “Sparse Index: API protections”
  1. 00/27 Sparse Index: API protectionsDerrick Stolee via GitGitGadget, Mar 16, 2021
  2. 02/27 read-cache: expand on query into sparse-directory entryDerrick Stolee via GitGitGadget, Mar 16, 2021
  3. 03/27 sparse-index: API protection strategyDerrick Stolee via GitGitGadget, Mar 16, 2021
  4. 04/27 cache: move ensure_full_index() to cache.hDerrick Stolee via GitGitGadget, Mar 16, 2021
  5. 01/27 *: remove 'const' qualifier for struct index_stateDerrick Stolee via GitGitGadget, Mar 16, 2021
  6. Junio C HamanoMar 19, 2021
  7. Derrick StoleeMar 20, 2021
  8. Junio C HamanoMar 20, 2021
  9. Derrick StoleeMar 30, 2021
  10. 13/27 merge-index: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  11. 12/27 ls-files: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  12. 08/27 commit: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  13. 05/27 add: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  14. Elijah NewrenMar 17, 2021
  15. Matheus Tavares BernardinoMar 17, 2021
  16. Derrick StoleeMar 17, 2021
  17. 07/27 checkout: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  18. 09/27 difftool: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  19. 15/27 sparse-checkout: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  20. Elijah NewrenMar 18, 2021
  21. Derrick StoleeMar 23, 2021
  22. 14/27 rm: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  23. 10/27 fsck: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  24. 11/27 grep: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  25. 27/27 name-hash: use expand_to_path()Derrick Stolee via GitGitGadget, Mar 16, 2021
  26. 16/27 update-index: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  27. 06/27 checkout-index: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  28. Elijah NewrenMar 17, 2021
  29. Derrick StoleeMar 17, 2021
  30. Elijah NewrenMar 17, 2021
  31. Derrick StoleeMar 17, 2021
  32. Elijah NewrenMar 17, 2021
  33. Derrick StoleeMar 18, 2021
  34. 18/27 dir: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  35. 17/27 diff-lib: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  36. Elijah NewrenMar 18, 2021
  37. Derrick StoleeMar 23, 2021
  38. 26/27 sparse-index: expand_to_path()Derrick Stolee via GitGitGadget, Mar 16, 2021
  39. 23/27 read-cache: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  40. 24/27 resolve-undo: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  41. 21/27 merge-recursive: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  42. 19/27 entry: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  43. 25/27 revision: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  44. 20/27 merge-ort: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  45. Elijah NewrenMar 18, 2021
  46. Derrick StoleeMar 23, 2021
  47. 22/27 pathspec: ensure full indexDerrick Stolee via GitGitGadget, Mar 16, 2021
  48. Elijah NewrenMar 17, 2021
  49. Elijah NewrenMar 18, 2021
  50. 00/25 Sparse Index: API protectionsDerrick Stolee via GitGitGadget, Apr 1, 2021
  51. 01/25 sparse-index: API protection strategyDerrick Stolee via GitGitGadget, Apr 1, 2021
  52. 03/25 read-cache: expand on query into sparse-directory entryDerrick Stolee via GitGitGadget, Apr 1, 2021
  53. 04/25 cache: move ensure_full_index() to cache.hDerrick Stolee via GitGitGadget, Apr 1, 2021
  54. 05/25 add: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  55. 02/25 *: remove 'const' qualifier for struct index_stateDerrick Stolee via GitGitGadget, Apr 1, 2021
  56. 10/25 fsck: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  57. 06/25 checkout-index: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  58. 08/25 commit: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  59. 11/25 grep: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  60. 07/25 checkout: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  61. 17/25 dir: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  62. 12/25 ls-files: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  63. 15/25 stash: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  64. 14/25 rm: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  65. 09/25 difftool: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  66. 19/25 merge-recursive: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  67. 20/25 pathspec: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  68. 18/25 entry: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  69. 13/25 merge-index: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  70. 21/25 read-cache: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  71. 23/25 revision: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  72. 22/25 resolve-undo: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  73. 24/25 sparse-index: expand_to_path()Derrick Stolee via GitGitGadget, Apr 1, 2021
  74. Elijah NewrenApr 5, 2021
  75. Derrick StoleeApr 6, 2021
  76. 25/25 name-hash: use expand_to_path()Derrick Stolee via GitGitGadget, Apr 1, 2021
  77. Elijah NewrenApr 5, 2021
  78. 16/25 update-index: ensure full indexDerrick Stolee via GitGitGadget, Apr 1, 2021
  79. Junio C HamanoApr 1, 2021
  80. Derrick StoleeApr 1, 2021
  81. Elijah NewrenApr 5, 2021
  82. 00/26 Sparse Index: API protectionsDerrick Stolee via GitGitGadget, Apr 12, 2021
  83. 01/26 sparse-index: API protection strategyDerrick Stolee via GitGitGadget, Apr 12, 2021
  84. 03/26 read-cache: expand on query into sparse-directory entryDerrick Stolee via GitGitGadget, Apr 12, 2021
  85. 02/26 *: remove 'const' qualifier for struct index_stateDerrick Stolee via GitGitGadget, Apr 12, 2021
  86. 08/26 commit: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  87. 06/26 checkout-index: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  88. 07/26 checkout: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  89. 05/26 add: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  90. 04/26 cache: move ensure_full_index() to cache.hDerrick Stolee via GitGitGadget, Apr 12, 2021
  91. 09/26 difftool: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  92. 12/26 ls-files: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  93. 13/26 merge-index: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  94. 14/26 rm: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  95. 15/26 stash: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  96. 16/26 update-index: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  97. 17/26 dir: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  98. 18/26 entry: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  99. 19/26 merge-recursive: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  100. 24/26 name-hash: don't add directories to name_hashDerrick Stolee via GitGitGadget, Apr 12, 2021
  101. 23/26 revision: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  102. 22/26 resolve-undo: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  103. 21/26 read-cache: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  104. 20/26 pathspec: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  105. 26/26 name-hash: use expand_to_path()Derrick Stolee via GitGitGadget, Apr 12, 2021
  106. 25/26 sparse-index: expand_to_path()Derrick Stolee via GitGitGadget, Apr 12, 2021
  107. 10/26 fsck: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  108. 11/26 grep: ensure full indexDerrick Stolee via GitGitGadget, Apr 12, 2021
  109. Elijah NewrenApr 13, 2021
  110. Junio C HamanoApr 14, 2021
  111. Derrick StoleeApr 15, 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.