Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ByteBuffer: one fewer allocs to go to Data #1839

Merged
merged 3 commits into from
Nov 26, 2024

Conversation

weissi
Copy link
Member

@weissi weissi commented Apr 30, 2021

Motivation:

When getting a Data from a ByteBuffer we currently allocate twice
(__DataStorage) and the closure for Data.Deallocator.

Modifications:

We can optimise that by making the closure capture exactly one
AnyObject which is already a reference counted object. The compiler
realises that (on Linux) and saves us an alloc.

Thanks @Lukasa for the suggestion here: #1836 (comment)

Result:

Fewer allocs.

@Lukasa Lukasa added the 🔨 semver/patch No public API change. label Apr 30, 2021
@@ -25,37 +26,39 @@ func run(identifier: String) {
@inline(never)
func doWrites(buffer: inout ByteBuffer) {
/* these ones are zero allocations */
// buffer.writeBytes(foundationData) // see SR-7542
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this gone on purpose?

buffer.writeBytes([0x41])
buffer.writeBytes("A".utf8)
buffer.writeString("A")
buffer.writeStaticString("A")
buffer.writeInteger(0x41, as: UInt8.self)

/* those down here should be one allocation each (on Linux) */
buffer.writeBytes(dispatchData) // see https://bugs.swift.org/browse/SR-9597
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly, is this gone on purpose?

@weissi weissi force-pushed the jw-reduce-data-alloc branch from 2f6d36d to ada6eec Compare April 30, 2021 15:51
@weissi
Copy link
Member Author

weissi commented Aug 17, 2023

We should resurrect this

@weissi weissi force-pushed the jw-reduce-data-alloc branch from aadf0d2 to e7666f6 Compare November 23, 2024 23:07
@weissi weissi enabled auto-merge (squash) November 23, 2024 23:08
Motivation:

When getting a `Data` from a `ByteBuffer` we currently allocate twice
(`__DataStorage`) and the closure for `Data.Deallocator`.

Modifications:

We can optimise that by making the closure capture exactly one
`AnyObject` which is already a reference counted object. The compiler
realises that (on Linux) and saves us an alloc.

Thanks @Lukasa for the suggestion here: apple#1836 (comment)

Result:

Fewer allocs.
@weissi weissi force-pushed the jw-reduce-data-alloc branch from e7666f6 to a2f1a8f Compare November 23, 2024 23:12
@weissi weissi merged commit d6be946 into apple:main Nov 26, 2024
42 of 43 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants