Repository navigation
perf: hash all bits of large Nat literals in Expr - #16146
Queued
JasonGross wants to merge 2 commits into
Queued
JasonGross wants to merge 2 commits into
JasonGross wants to merge 2 commits into
Conversation
This PR fixes a quadratic slowdown in the kernel when a term contains many `Nat` literals that are equal modulo `2^64`, such as the powers `65536 ^ i`. The `Expr` hash of a `Nat` literal used `hash n`, which is `n % 2^64`. Thus all multiples of `2^64` had the same hash. Each lookup in the kernel caches then compared these literals one by one. This PR adds `Literal.natHash`. For `n < 2^64`, it returns `n`, so the hash of these literals does not change. For larger `n`, it combines all 64-bit words of `n` with `mixHash`. `Hashable Nat` does not change. Closes leanprover#15686 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The new hash of large `Nat` literals changes the hashes stored in `.olean` files. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
|
!bench |
|
Benchmark results for 29ccdbe against b21ac79 are in. There are significant results. @nomeata
Large changes (7🟥)
Medium changes (1✅)
Small changes (4✅, 4🟥)
|
Collaborator
|
nice, it even shows up in our test suite: |
Collaborator
|
@JasonGross is this intentionally a draft? |
Author
|
The agents were following the policy of nonmaintainers opening PRs as draft when the issue being fixed hasn't had attention from maintainers, or something like that. Otherwise I believe this is ready. |
This was referenced Nov 12, 2026
nomeata
approved these changes
Nov 12, 2026
nomeata
left a comment
Collaborator
There was a problem hiding this comment.
The “Large changes“ regressions are tests with large literals, so it's just the feature working as expected. I don’t see much to improve here (and Claude agrees).
nomeata
marked this pull request as ready for review
November 12, 2026 16:32
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a quadratic slowdown in the kernel when a term contains many
Natliterals that are equal modulo2^64, such as the powers65536 ^ i.The
Exprhash of aNatliteral usedhash n, which isn % 2^64. Thus all multiples of2^64had the same hash. For the 4,000 literals65536 ^ iin #15686, there were only 5 different hashes. Each lookup in the kernel caches then compared these literals one by one.This PR adds
Literal.natHash. Forn < 2^64, it returnsn, so the hash of these literals does not change. For largern, it combines all 64-bit words ofnwithmixHash. The result does not depend on the GMP limb size.Hashable Natdoes not change.Kernel time (
type checkingin the profiler) forpowers% 65536 nfrom #15686:The code generator also uses these hashes. For the full example in #15686, the total time goes from 3.0 s to 1.9 s.
The change alters the hashes stored in
.oleanfiles, so this PR checks the box instage0/src/stdlib_flags.h, as in #16107.Closes #15686
🤖 Generated with Claude Code