Skip to content

Commit ab9e07d

Browse files
authored
Merge pull request #71789 from apple/revert-70532-david/asking-the-right-questions
Revert "Switch to malloc_good_size instead of malloc_size"
2 parents 26a4077 + ce0a043 commit ab9e07d

File tree

3 files changed

+16
-40
lines changed

3 files changed

+16
-40
lines changed

stdlib/public/SwiftShims/swift/shims/LibcShims.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,6 @@ static inline __swift_bool _swift_stdlib_has_malloc_size() {
129129
return HAS_MALLOC_SIZE != 0;
130130
}
131131

132-
static inline __swift_size_t _swift_stdlib_malloc_good_size(__swift_size_t sz) {
133-
#if defined(__APPLE__)
134-
extern __swift_size_t malloc_good_size(__swift_size_t);
135-
return malloc_good_size(sz);
136-
#else
137-
return (sz + 15) & ~15; //round up to the nearest 16 byte alignment, at least
138-
#endif
139-
}
140-
141132
// Math library functions
142133
static inline SWIFT_ALWAYS_INLINE
143134
float _stdlib_remainderf(float _self, float _other) {

stdlib/public/core/Shims.swift

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,3 @@ internal let _fastEnumerationStorageMutationsPtr =
4141
internal func _mallocSize(ofAllocation ptr: UnsafeRawPointer) -> Int? {
4242
return _swift_stdlib_has_malloc_size() ? _swift_stdlib_malloc_size(ptr) : nil
4343
}
44-
45-
/*
46-
Invariant:
47-
malloc_size(malloc(malloc_good_size(size))) >= malloc_good_size(size)
48-
49-
Usually:
50-
malloc_size(malloc(malloc_good_size(size))) == malloc_good_size(size)
51-
*/
52-
@_effects(readnone) @inline(__always)
53-
internal func _mallocGoodSize(for size: Int) -> Int {
54-
precondition(size >= 0)
55-
// Not all allocators will see benefits from rounding up to 16/32 byte aligned
56-
// but it'll never cause misbehavior, and many reasonable ones will benefit
57-
if (size <= 128) {
58-
return (size &+ 15) & ~15;
59-
}
60-
if (size <= 256) {
61-
return (size &+ 31) & ~31;
62-
}
63-
return _mallocGoodSizeLarge(for: size)
64-
}
65-
66-
@_effects(readnone)
67-
internal func _mallocGoodSizeLarge(for size: Int) -> Int {
68-
let goodSize = _swift_stdlib_malloc_good_size(size)
69-
precondition(goodSize >= size)
70-
return goodSize
71-
}

stdlib/public/core/StringStorage.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,14 @@ fileprivate func _allocate<T: AnyObject>(
181181
) -> (T, realNumTailBytes: Int) {
182182
_internalInvariant(getSwiftClassInstanceExtents(T.self).1 == numHeaderBytes)
183183

184+
func roundUp(_ x: Int) -> Int { (x + 15) & ~15 }
185+
184186
let numBytes = numHeaderBytes + numTailBytes
185187

186188
let linearBucketThreshold = 128
187189
if _fastPath(numBytes < linearBucketThreshold) {
188190
// Allocate up to the nearest bucket of 16
189-
let realNumBytes = _mallocGoodSize(for: numBytes)
191+
let realNumBytes = roundUp(numBytes)
190192
let realNumTailBytes = realNumBytes - numHeaderBytes
191193
_internalInvariant(realNumTailBytes >= numTailBytes)
192194
let object = tailAllocator(realNumTailBytes)
@@ -200,10 +202,21 @@ fileprivate func _allocate<T: AnyObject>(
200202
growTailBytes = numTailBytes
201203
}
202204

203-
let total = _mallocGoodSize(for: numHeaderBytes + growTailBytes)
205+
let total = roundUp(numHeaderBytes + growTailBytes)
204206
let totalTailBytes = total - numHeaderBytes
205207

206-
return (tailAllocator(totalTailBytes), totalTailBytes)
208+
let object = tailAllocator(totalTailBytes)
209+
if let allocSize = _mallocSize(ofAllocation:
210+
UnsafeRawPointer(Builtin.bridgeToRawPointer(object))) {
211+
_internalInvariant(allocSize % MemoryLayout<Int>.stride == 0)
212+
213+
let realNumTailBytes = allocSize - numHeaderBytes
214+
_internalInvariant(realNumTailBytes >= numTailBytes)
215+
216+
return (object, realNumTailBytes)
217+
} else {
218+
return (object, totalTailBytes)
219+
}
207220
}
208221

209222
fileprivate func _allocateStringStorage(

0 commit comments

Comments
 (0)