Skip to content

Optimize Decimal::sqrt using ilog10#2604

Open
mk0walsk wants to merge 1 commit intoCosmWasm:mainfrom
mk0walsk:optimization02
Open

Optimize Decimal::sqrt using ilog10#2604
mk0walsk wants to merge 1 commit intoCosmWasm:mainfrom
mk0walsk:optimization02

Conversation

@mk0walsk
Copy link

Resolves the TODO in Decimal::sqrt by replacing the iterative precision search with a direct calculation using ilog10.

Changes:

  • Implements Uint128::ilog10 (delegating to u128::checked_ilog10).
  • Updates Decimal::sqrt to calculate optimal precision in O(1) time using the derived formula: min(9, (38 - log10(atomics)) / 2).

Impact:

  • Significantly reduces gas costs for large numbers by eliminating the loop that performed expensive isqrt and multiplication checks.
  • Maintains correctness and robustness with a fallback mechanism for edge-case boundary overflows.

@DariuszDepta DariuszDepta added this to the 3.0.3 milestone Jan 26, 2026
@DariuszDepta DariuszDepta modified the milestones: 3.0.3, 3.0.4, 3.0.5 Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants