Repository navigation
Conversation
Creating an fmpz_mod_ctx ran a probable prime test on the modulus even though only a few operations (discrete log, polynomial and mpoly algorithms that require a field) use the result. For large moduli this dominated the cost of creating the context. The test is now run the first time is_prime() is called and the result is cached. Fixes flintlib#333. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fixes #333.
fmpz_mod_ctx.__init__ranfmpz_is_probabprimeon the modulus every time a context was created. Only a few operations use the result (discrete_log, and thefmpz_mod_poly/fmpz_mod_mpoly/fq_defaultpaths that need a field), so for large moduli most of the cost of creating a context was a primality test nobody asked for.As suggested in the issue, the test now runs the first time
is_prime()is called and the result is cached._is_primechanges frombinttoint, with-1meaning "not tested yet". Every internal user already goes throughis_prime(), so nothing else changes. Under free-threading two threads may both run the test on the same context; they write the same value, so the race is harmless.Context creation, Mersenne prime moduli (WSL, FLINT 3.6.0):
is_prime()afterTest:
test_fmpz_mod_ctx_lazy_is_primechecks that creating the context costs less than a tenth of the firstis_prime()call. It compares the two timings in the same run rather than using a fixed time limit. Before the change the firstis_prime()is a cached lookup, so the test fails (≈41 ms vs ≈6 µs). The test also checks that the cached answer is correct for prime, composite and tiny moduli.Checks run:
pytest --doctest-modules src/flint(1097 passed),python -m flint.test(all 3152 doctests passed),cython-lint src/(clean), Python 3.12, FLINT 3.6.0.AI use: Claude Code (Claude Opus 5.5) wrote this change and the description, working on behalf of @KainanP. It ran all the tests listed above locally.
🤖 Generated with Claude Code