Skip to content

perf(parser): add collection capacity hints to known-size loops - #1791

Merged
jglogan merged 4 commits into
apple:mainfrom
hluaguo:opt/capacity-hints
Jun 26, 2026
Merged

perf(parser): add collection capacity hints to known-size loops#1791
jglogan merged 4 commits into
apple:mainfrom
hluaguo:opt/capacity-hints

Conversation

@hluaguo

@hluaguo hluaguo commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Type of Change

  • Bug fix

Motivation and Context

Methods in Parser.swift and Utility.swift build arrays and dictionaries in loops where the final size is known from the input parameter count, but none pre-allocate storage. Without a capacity hint, Array.append grows the backing store exponentially (2x), causing O(log n) reallocation+copies per call. These methods execute on every container run/create command, so the savings compound at scale.

Testing

  • Tested locally
  • Added/updated tests

Added 7 new tests with large inputs (50-100 entries) that exercise the capacity-allocated code paths. All 183 existing tests pass with zero regressions.

@hluaguo
hluaguo force-pushed the opt/capacity-hints branch from 3685f95 to 7bc6c76 Compare June 23, 2026 18:25

@jglogan jglogan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hluaguo thank you for these changes. Can you have a look at the comments on the collections that are deduped prior to processing? I think we can specify the actual capacity more precisely for those.

Comment thread Sources/Services/ContainerAPIService/Client/Parser.swift
Comment thread Sources/Services/ContainerAPIService/Client/Parser.swift
@hluaguo

hluaguo commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

Oh, the dedupe can shrink the input, so reserving before it over-allocates. I'll shift reserveCapacity after the dedupe in both tmpfsMounts and mounts.

@hluaguo
hluaguo force-pushed the opt/capacity-hints branch from 5d6c635 to 7c90357 Compare June 24, 2026 11:56
@hluaguo
hluaguo force-pushed the opt/capacity-hints branch from 7c90357 to 071614e Compare June 24, 2026 11:57
@hluaguo
hluaguo requested a review from jglogan June 24, 2026 13:34
@github-actions

Copy link
Copy Markdown

Code Coverage

Tier Line Coverage
Unit 33.6%
Integration 22.97%
Combined 55.72%

@jglogan
jglogan merged commit 0c95007 into apple:main Jun 26, 2026
5 of 6 checks passed
@jglogan

jglogan commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

@hluaguo Merged, thank you for the optimization!

saehejkang pushed a commit to saehejkang/container that referenced this pull request Jul 16, 2026
…e#1791)

- For result sets with known sizes, it's more efficient
  to supply the size as a capacity hint.
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.

3 participants