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

Re: [PATCH v2 5/6] cache API: add a "INDEX_STATE_INIT" macro/function, add release_index()

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Jan 12, 2023, 16:19 UTC
Message-ID
<230112.86zganzpwk.gmgdl@evledraar.gmail.com>
In-Reply-To
<270ef93c-cfdc-d119-5740-f704d7f2a029@github.com>
On Thu, Jan 12 2023, Derrick Stolee wrote:
Show 18 quoted lines
> On 1/12/2023 7:55 AM, Ævar Arnfjörð Bjarmason wrote:
>> As we'll see in a subsequent commit we'll get advantages from always
>> initializing the "repo" member of the "struct index_state". To make
>> that easier let's introduce an initialization macro & function.
>> 
>> The various ad-hoc initialization of the structure can then be changed
>> over to it, and we can remove the various "0" assignments in
>> discard_index() in favor of calling index_state_init() at the end.
>
>> -	memset(&o->result, 0, sizeof(o->result));
>> +	index_state_init(&o->result);
>>  	o->result.initialized = 1;
>
> It's interesting that 'struct index_state' has an 'initialized'
> member that we aren't setting in index_state_init(). Perhaps it's
> only being used in special cases like this, and means something
> more specific than "index_state_init() was run"? Or maybe we
> could add it to INDEX_STATE_INIT and drop this line?

It's unrelated, and doing that would be a bug. It's a bit unfortunately named, a better name might be "read_index_data" or something.

It was added in 913e0e99b6a (unpack_trees(): protect the handcrafted in-core index from read_cache(), 2008-08-23), which shows the use-case, i.e. it's for avoiding re-reading the index file itself (or in that case, to trust our hand-crafted faked-up version of it).

I opted not to mention it in the commit message, after being sufficiently convinced that it was unrelated, which was probably a mistake :)

Just as a sanity check, we do have really good test coverage of the difference, at least 1/2 of the tests I bothered to wait for failed when I tried this on top:

diff --git a/cache.h b/cache.h
index 4bf14e0bd94..1f8e5f4e823 100644
--- a/cache.h
+++ b/cache.h
@@ -371,6 +371,7 @@ struct index_state {
  * "r" argument to index_state_init() in that case.
  */
 #define INDEX_STATE_INIT(r) { \
+	.initialized = 1, \
 	.repo = (r), \
 }
 void index_state_init(struct index_state *istate, struct repository *r);
Previous: Derrick StoleeNext: Derrick Stolee
Message 21 of 32 in “cache API: always have a "istate->repo"”
  1. 0/5 cache API: always have a "istate->repo"Ævar Arnfjörð Bjarmason, Jan 10, 2023
  2. 1/5 builtin/difftool.c: { 0 }-initialize rather than using memset()Ævar Arnfjörð Bjarmason, Jan 10, 2023
  3. 2/5 sparse-index.c: expand_to_path() can assume non-NULL "istate"Ævar Arnfjörð Bjarmason, Jan 10, 2023
  4. 3/5 sparse-index API: fix TODO, BUG() out on NULL ensure_full_index()Ævar Arnfjörð Bjarmason, Jan 10, 2023
  5. Philip OakleyJan 10, 2023
  6. Ævar Arnfjörð BjarmasonJan 10, 2023
  7. Derrick StoleeJan 10, 2023
  8. 4/5 read-cache.c: refactor set_new_index_sparsity() for subsequent commitÆvar Arnfjörð Bjarmason, Jan 10, 2023
  9. 5/5 treewide: always have a valid "index_state.repo" memberÆvar Arnfjörð Bjarmason, Jan 10, 2023
  10. Ævar Arnfjörð BjarmasonJan 10, 2023
  11. Jeff HostetlerJan 10, 2023
  12. Derrick StoleeJan 10, 2023
  13. Derrick StoleeJan 10, 2023
  14. 0/6 cache API: always have a "istate->repo"Ævar Arnfjörð Bjarmason, Jan 12, 2023
  15. 1/6 builtin/difftool.c: { 0 }-initialize rather than using memset()Ævar Arnfjörð Bjarmason, Jan 12, 2023
  16. 2/6 sparse-index.c: expand_to_path() can assume non-NULL "istate"Ævar Arnfjörð Bjarmason, Jan 12, 2023
  17. 3/6 sparse-index API: BUG() out on NULL ensure_full_index()Ævar Arnfjörð Bjarmason, Jan 12, 2023
  18. 4/6 read-cache.c: refactor set_new_index_sparsity() for subsequent commitÆvar Arnfjörð Bjarmason, Jan 12, 2023
  19. 5/6 cache API: add a "INDEX_STATE_INIT" macro/function, add release_index()Ævar Arnfjörð Bjarmason, Jan 12, 2023
  20. Derrick StoleeJan 12, 2023
  21. Ævar Arnfjörð BjarmasonJan 12, 2023
  22. Derrick StoleeJan 12, 2023
  23. 6/6 treewide: always have a valid "index_state.repo" memberÆvar Arnfjörð Bjarmason, Jan 12, 2023
  24. Derrick StoleeJan 12, 2023
  25. Derrick StoleeJan 12, 2023
  26. Junio C HamanoJan 13, 2023
  27. Junio C HamanoJan 13, 2023
  28. Ævar Arnfjörð BjarmasonJan 16, 2023
  29. Philip OakleyJan 16, 2023
  30. Junio C HamanoJan 16, 2023
  31. treewide: always have a valid "index_state.repo" memberÆvar Arnfjörð Bjarmason, Jan 17, 2023
  32. Derrick StoleeJan 17, 2023

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.