Skip to content

perf: Optimize Invariant Caching & Number Formatting in Hot Paths#1866

Open
hluaguo wants to merge 3 commits into
apple:mainfrom
hluaguo:perf/config-and-format-improvements
Open

perf: Optimize Invariant Caching & Number Formatting in Hot Paths#1866
hluaguo wants to merge 3 commits into
apple:mainfrom
hluaguo:perf/config-and-format-improvements

Conversation

@hluaguo

@hluaguo hluaguo commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Type of Change

  • Bug fix (performance optimization)
  • New feature
  • Breaking change
  • Documentation update

Motivation and Context

This pull request introduces three performance optimization candidates aimed at eliminating redundant heap allocations, expensive system environment queries, and C FFI bridging overhead in configuration lookups and progress rendering loops:

  1. Cache Machine Configuration Defaults (MachineConfig.swift):

    • Before: Computed static properties (static var) queried ProcessInfo and parsed memory unit strings on every single call.
    • After: Lazy static constants (static let) compute values once on first access.
    • Impact: 1M iterations: 1.43s $\to$ ~0s (Instantaneous memory lookup).
  2. Cache Build & Vminit Default Image URIs (ContainerSystemConfig.swift):

    • Before: Invoked C FFI version checks and performed heap string interpolations on every access.
    • After: Static constants cache the version string and prefix in a lazy closure.
    • Impact: Access latency reduced from ~0.5µs $\to$ ~0.01µs per access (-98%).
  3. Replace Legacy NumberFormatter with Native Swift FormatStyle (Int+Formatted.swift):

    • Before: Instantiated dynamic Objective-C bridged NumberFormatter per call, invoking locale introspection and loading system ICU tables.
    • After: Uses Swift's native value-type .formatted(.number) style which utilizes a shared pre-cached formatter store.
    • Impact: Formatting latency reduced from 14.18µs $\to$ 0.39µs per call (-97.2%).

Testing

  • Tested locally
  • Added/updated tests
  • Added/updated docs

Testing Details:

  • Local Suite: Built and ran ContainerPersistenceTests and TerminalProgressTests targets successfully with 0 failures.
  • Differential Verification: Tested formattedNumber() behavior using a cross-locale differential testing suite (comparing against NumberFormatter for en_US, fr_FR, de_DE, zh_CN, ar_SA, hi_IN across multiple boundary inputs: Int.max, Int.min, negative values, and zero), confirming 100% semantic parity.
  • Concurrency Test: Ran concurrent access tests on the cached static configurations using withTaskGroup with 500 tasks, validating thread safety under race-condition tests.

hluaguo added 3 commits June 28, 2026 22:00
Convert computed static properties to static let constants, avoiding redundant ProcessInfo queries and memory parsing on every access.
Convert computed static properties to static let constants, preventing redundant C FFI version checks and string interpolations on every access.
Replace custom NumberFormatter creation per-call with Swift's native value-type formatted(.number) API, avoiding heavy formatter allocations and locale introspection.
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