Skip to content

Adopt malloc_type for allocating Swift objects from runtime #63226

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 3, 2023

Conversation

yln
Copy link
Contributor

@yln yln commented Jan 25, 2023

rdar://98998492

@yln yln self-assigned this Jan 25, 2023
Comment on lines +107 to +108
void *swift::swift_slowAllocTyped(size_t size, size_t alignMask,
MallocTypeId typeId) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

At first, I had the calls to the old and new malloc APIs in one function and let the untyped one forward to the typed one:

void *swift_slowAlloc(size_t size, size_t alignMask) {
  return swift_slowAllocTyped(size, alignMask, /*typeId=*/0);
}

But the #if became very unwieldy, so I decided to turn it around (Typed variant doing it's thing and having the untyped one as a fallback) and accept a bit of code duplication.

Comment on lines +76 to +80
static size_t computeAlignment(size_t alignMask) {
return (alignMask == ~(size_t(0))) ? _swift_MinAllocationAlignment
: alignMask + 1;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Extract this helper to reduce code duplication. Also: relevant comment is now right next to the code.

@@ -115,12 +118,77 @@ static HeapObject *_swift_tryRetain_(HeapObject *object)
return _ ## name ## _ args; \
} while(0)

#if defined(_MALLOC_TYPE_ENABLED)
static malloc_type_id_t computeTypeId(const HeapMetadata *heapMetadata) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I want to cache the malloc_type_descriptor_t (32 bit) value computed here. Any suggestions on how to best do this? We will require one value per heap-allocated type.

Copy link
Contributor

@grynspan grynspan Jan 27, 2023

Choose a reason for hiding this comment

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

The cost of caching it would probably exceed the cost of doing the computation here (which is going to compile to a couple of few arithmetic instructions.)

I might factor it out into another function, but I don't think it's worth trying to cache unless profiling shows this computation is a bottleneck.

@yln yln force-pushed the PR-98998492_malloc_type-adoption branch 2 times, most recently from e060f8e to f4786fa Compare January 31, 2023 04:15
@yln yln force-pushed the PR-98998492_malloc_type-adoption branch from f4786fa to 93fc1e8 Compare January 31, 2023 21:30
@yln
Copy link
Contributor Author

yln commented Feb 1, 2023

@swift-ci Please test

@yln yln marked this pull request as ready for review February 2, 2023 19:43
Radar-Id: rdar://98998492
@yln
Copy link
Contributor Author

yln commented Feb 2, 2023

@swift-ci Please test

@yln
Copy link
Contributor Author

yln commented Feb 2, 2023

@swift-ci Please test macOS

@yln yln merged commit 5ad0331 into main Feb 3, 2023
@yln yln deleted the PR-98998492_malloc_type-adoption branch February 3, 2023 01:08
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