Skip to content

Remove two unnecessary memory copies on Any. - #1985

Merged
thomasvl merged 1 commit into
apple:mainfrom
Lukasa:cb-dont-copy-data
Feb 20, 2026
Merged

thomasvl merged 1 commit into
apple:mainfrom
Lukasa:cb-dont-copy-data

Conversation

@Lukasa

@Lukasa Lukasa commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

Two parts of Any will construct a Data unnecessarily, triggering a memory copy. One is used to serialize a JSON body (stored as an Array of UInt8) into a Data, by way of an intermediate Array. This intermediate Array is not necessary: we should go directly to the final Data.

The second is accessing an already serialized value, which already has a Data in hand, and calls Data(thatData). This initializer looks innocuous, but Data does not actually have a special-case for this, so this ends up hitting the initializer that takes a Sequence, so does a bulk copy operation instead.

This patch changes both of these to remove extra memory copies when using Any.

Two parts of `Any` will construct a `Data` unnecessarily, triggering
a memory copy. One is used to serialize a JSON body (stored as an
Array of UInt8) into a Data, by way of an intermediate Array. This
intermediate Array is not necessary: we should go directly to the
final Data.

The second is accessing an already serialized value, which already
has a Data in hand, and calls `Data(thatData)`. This initializer
looks innocuous, but `Data` does not actually have a special-case
for this, so this ends up hitting the initializer that takes a
`Sequence`, so does a bulk copy operation instead.

This patch changes both of these to remove extra memory copies when
using Any.
@Lukasa Lukasa added the 🔨 semver/patch No public API change. label Feb 20, 2026

@thomasvl thomasvl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice find. Happen to just notice it or did this get caught in some performance case?

@thomasvl
thomasvl merged commit bb661fa into apple:main Feb 20, 2026
12 of 13 checks passed
@Lukasa
Lukasa deleted the cb-dont-copy-data branch February 20, 2026 20:39
@Lukasa

Lukasa commented Feb 20, 2026

Copy link
Copy Markdown
Contributor Author

It got caught by an internal team who ended up spending 8% of their runtime in one of these copies 😱

@thomasvl

Copy link
Copy Markdown
Collaborator

It got caught by an internal team who ended up spending 8% of their runtime in one of these copies 😱

Wow. Should we cut a release for the fix?

@Lukasa

Lukasa commented Feb 21, 2026

Copy link
Copy Markdown
Contributor Author

Sometime soon probably, yeah. Right now they can prototype using main to confirm this fully resolves their issue.

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.

3 participants