-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
Conversation
void *swift::swift_slowAllocTyped(size_t size, size_t alignMask, | ||
MallocTypeId typeId) { |
There was a problem hiding this comment.
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.
static size_t computeAlignment(size_t alignMask) { | ||
return (alignMask == ~(size_t(0))) ? _swift_MinAllocationAlignment | ||
: alignMask + 1; | ||
} | ||
|
There was a problem hiding this comment.
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.
stdlib/public/runtime/HeapObject.cpp
Outdated
@@ -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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
e060f8e
to
f4786fa
Compare
Radar-Id: rdar://98998492
f4786fa
to
93fc1e8
Compare
@swift-ci Please test |
Radar-Id: rdar://98998492
@swift-ci Please test |
@swift-ci Please test macOS |
rdar://98998492