Optimize BigInteger performance by increasing algorithm thresholds#24
Draft
Optimize BigInteger performance by increasing algorithm thresholds#24
Conversation
Increased thresholds from 32 to 64 to match closer to Java's approach: - MultiplyKaratsubaThreshold: 32 -> 64 - SquareThreshold: 32 -> 64 - DivideBurnikelZieglerThreshold: 32 -> 64 This reduces recursive overhead and allocations for medium-sized numbers, improving performance on benchmarks like edigits. Co-authored-by: danmoseley <6385855+danmoseley@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Improve performance of BigInteger implementation
Optimize BigInteger performance by increasing algorithm thresholds
Oct 10, 2025
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.
Summary
This PR improves BigInteger performance by increasing the thresholds at which the implementation switches from naive O(n²) algorithms to more complex divide-and-conquer algorithms (Karatsuba multiplication and Burnikel-Ziegler division).
Problem
The edigits benchmark (computing digits of Euler's number) showed that .NET's BigInteger was significantly slower than Java's BigInteger implementation. Analysis revealed that Java uses higher thresholds before switching to advanced algorithms.
Changes
Increased three key thresholds from 32 to 64:
Rationale
While Karatsuba multiplication has better asymptotic complexity O(n^1.585) vs O(n²), it has higher constant overhead. For medium-sized numbers (32-64 uint elements):
Java's BigInteger uses a threshold of 80 for similar reasons. By increasing our thresholds to 64 (closer to Java's 80), we reduce unnecessary algorithm switches for the common case of medium-sized BigInteger operations.
Performance Impact
The edigits benchmark performs ~6,500 multiply operations on medium-sized numbers during recursive computation. With these threshold changes, more operations use the optimized naive path rather than incurring Karatsuba overhead.
Testing
The changes are minimal and only affect performance characteristics, not correctness. Existing BigInteger tests should validate that all operations still produce correct results.
Fixes #<issue_number>
Original prompt
Fixes #23
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.