Skip to content

perf: resolve the culture once when getting the formatting number format - #2013

Open
g4lb wants to merge 2 commits into
Humanizr:mainfrom
g4lb:perf/formatting-number-format
Open

g4lb wants to merge 2 commits into
Humanizr:mainfrom
g4lb:perf/formatting-number-format

Conversation

@g4lb

@g4lb g4lb commented Nov 21, 2026 •

Copy link
Copy Markdown

GetFormattingNumberFormat resolved the culture three times on each call. It also allocated a closure and a key string on each call. ByteSize, ToMetric, Ordinalize and Fractionalize use this method.

This change resolves the culture one time and uses a separate cache with TryGetValue. A cache hit does not allocate. The output does not change. A new test makes sure that a culture with no override keeps its own NumberFormat.

BenchmarkDotNet, Apple M2 Max (ARM64). Old code against old code moved by 0.5% or less.

Benchmark (en-US unless shown) net8.0 net10.0 net11.0 Allocated (net10.0)
ByteSize.Humanize() -35% -36% -41% 1280 B → 888 B
ByteSize.Humanize(), fi-FI -37% -38% -41% 2008 B → 888 B
ByteSize.Humanize(), invariant -29% -33% -35% 1280 B → 888 B
ByteSize.ToString(format, culture) -25% -25% -30% 1280 B → 888 B
ToMetric(decimals: 2) -33% -33% -38% 968 B → 520 B
ToMetric, 5 cultures in turn -28% -33% -33% 1856 B → 1096 B

No benchmark became slower. I did not measure x64.

Tests pass on net8.0, net10.0 and net11.0. Old and new code gave the same output for 1.2M random inputs in 10 cultures.

CI can fail because of the System.Security.Cryptography.Pkcs advisory on main (see #1987).

Summary by CodeRabbit

  • Bug Fixes
    • Byte-size values now use the supplied culture’s number formatting, including decimal separators and negative signs.
    • Recognized cultures apply their available decimal, negative-sign, and group-separator formatting overrides. Cultures that aren’t recognized retain their original number format.
    • Formatting results are cached separately from parsing formats, so displaying byte-size values respects culture-specific settings without changing parsing behavior.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T12:40:12.816449Z 45d6282 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The generator now resolves locale-specific number-format overrides through a separate formatting cache. Byte-size tests cover custom and Finnish culture formatting. The locale YAML reference records an updated fingerprint for the generator file.

Changes: Culture-specific number formatting

Layer / File(s) Summary
Resolve and test formatting overrides
src/Humanizer.SourceGenerators/Generators/LocaleRegistryInput.cs, tests/Humanizer.Tests/Bytes/ToStringTests.cs, website/docs/contributing/locale-yaml-reference.mdx
Formatting resolves locale overrides and stores cloned formats in a separate cache keyed by culture name. Tests check a custom en-US decimal separator and Finnish formatting of a negative value. The locale YAML reference records an updated fingerprint for the generator file.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Suggested reviewers: clairernovotny

Merge Risk: Minimal · up to 3e2a4

This change reduces allocations when number formats are resolved for byte-size and related formatting. Output is intended to stay the same, and the added tests cover custom and Finnish culture formats. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "perf: resolve the culture once when getting the formatting number format" matches the main change. The raw summary shows GetFormattingNumberFormat now resolves the culture through Generated…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "perf: resolve the culture once when getting the formatting number format" matches the main change. The raw summary shows GetFormattingNumberFormat now resolves the culture through GeneratedCultureResolver once and caches formatting overrides in a separate cache keyed by culture name. The PR objectives confirm the goal is to resolve the culture once and use a separate cache to avoid per-call closure and key-string allocations. The title omits the separate cache and the new test, which is acceptable because it does not need to cover every detail.

Finishing Touches

Simplify code

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d598b3a0-f8d0-44a7-9492-d4f603e09486
Commits

Reviewing files that changed from the base of the PR and between 45d6282 and 3e2a443.

Files selected for processing: 1

  • website/docs/contributing/locale-yaml-reference.mdx

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@mergify

mergify Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Merge Protections

🔴 1 of 2 protections blocking · waiting on 🤖 CI

Protection Waiting on
🔴 Ready to land on main 🤖 CI
🟢 🚦 Auto-merge —

🔴 Ready to land on main

Waiting for

  • check-success = @azure-pipelines/Humanizer-CI
This rule is failing.

CI green, authored or approved by a maintainer, review threads resolved, not held.

  • all of:
    • check-success = @azure-pipelines/Humanizer-CI
  • #changes-requested-reviews-by = 0
  • #review-threads-unresolved = 0
  • -label = do-not-merge
  • any of:
    • #approved-reviews-by >= 1
    • author = clairernovotny

Show 1 satisfied protection

🟢 🚦 Auto-merge

When all merge protections are satisfied and these conditions match, this pull request will be merged automatically.

  • base = main

This branch has not been deployed

No deployments
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.

1 participant