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

Re: [PATCH] patch-ids: achieve const correctness in patch_id_neq()

From
Tian Yuchen <a3205153416@gmail.com>
Date
Mar 8, 2026, 14:42 UTC
Message-ID
<1a1ed5c6-8843-4bd5-9f57-187ef39497c3@gmail.com>
In-Reply-To
<xmqqseaasuph.fsf@gitster.g>
Hi Junio,
On 3/8/26 14:26, Junio C Hamano wrote:
Show 7 quoted lines
> The fact that `patch_id_neq` receives `const struct hashmap_entry *`
> parameters--which is a requirement of the `hashmap` API--is what is
> clashing with our lazy initialization here. The original code
> handled this by casting to a non-const `struct patch_id *` right at
> the beginning (via `container_of`). While this also "drops" the
> constness, the original is at least more honest about the fact that
> `a` and `b` will be modified.

Oops, This is something I hadn't considered before. I admit I didn't think things through carefully enough.

So, we actually find ourselves in this dilemma:
  - The Hashmap API specification mandates that input parameters should 
be const *in principle*.
  - The lazy loading mechanism requires us to write the results into 
memory; otherwise, there will be significant performance loss.
Am I correct?

I find that maintaining the current approach seems the most reasonable option. Computing all patch ids before putting objects into the hashmap appears to be a move that affects everything else and is not worth the effort. On the contrary, slightly breaking the Hashmap API conventions seems to be a more *cost-effective* approach...

Or perhaps it would be more reasonable to slightly modify this NEEDSWORK flag here?

Will send the next patch shortly.
Show 10 quoted lines
> If we truly wanted to achieve const-correctness here, we would
> likely need to avoid lazy initialization within the comparison
> function altogether, pre-calculating the full patch ID before it's
> needed for comparison. The NEEDSWORK comment should remain until a
> more fundamental solution is found, or if we should just admit that
> the current lazy evaluation pattern is what we want and document
> that (i.e., add a comment to justify why we strip away the constness
> here). As it stands, this patch doesn't "ensure" const correctness;
> it just masks the violation with an explicit cast at the location
> the pointer is used.
Regards,
Yuchen
Previous: Junio C HamanoNext: Tian Yuchen
Message 3 of 7 in “patch-ids: achieve const correctness in patch_id_neq()”
  1. patch-ids: achieve const correctness in patch_id_neq()Tian Yuchen, Mar 8, 2026
  2. Junio C HamanoMar 8, 2026
  3. Tian YuchenMar 8, 2026
  4. patch-ids: document intentional const-casting in patch_id_neq()Tian Yuchen, Mar 8, 2026
  5. Junio C HamanoMar 9, 2026
  6. cat@malon.devMar 9, 2026
  7. patch-ids: document intentional const-casting in patch_id_neq()Tian Yuchen, Mar 9, 2026

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.