Skip to content

[perf] Do stack allocs instead of heap allocs for small fixed-size allocations#3652

Open
MahdiBM wants to merge 2 commits into
apple:mainfrom
MahdiBM:mmbm-temp-stack-allocs
Open

[perf] Do stack allocs instead of heap allocs for small fixed-size allocations#3652
MahdiBM wants to merge 2 commits into
apple:mainfrom
MahdiBM:mmbm-temp-stack-allocs

Conversation

@MahdiBM

@MahdiBM MahdiBM commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

For small fixed-size allocations, we can use stack instead of heap.

Motivation:

Allocations are costly. Stack allocations are much cheaper than heap allocations.

Modifications:

Use withUnsafeTemporaryAllocation(of:capacity:_:) instead of allocating an Array.
withUnsafeTemporaryAllocation allocates on the stack for up to 1000 bytes.

Result:

Better performance.

@MahdiBM MahdiBM changed the title [perf] Do stack allocs of heap allocs for small fixed-size allocations [perf] Do stack allocs instead of heap allocs for small fixed-size allocations Jul 9, 2026

@Lukasa Lukasa 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.

Nice improvement, thanks @MahdiBM.

@Lukasa Lukasa added the 🔨 semver/patch No public API change. label Jul 9, 2026
@Lukasa

Lukasa commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

The allocation counts went down (success!) so we need to update the integration test thresholds to lock the win in.

@MahdiBM MahdiBM requested a review from Lukasa July 9, 2026 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants