Skip to content

Commit b6021c1

Browse files
committed
[benchmark] half Array.removeAll bench size
1 parent 6a1129f commit b6021c1

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

benchmark/single-source/ArrayRemoveAll.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class Slow {
2626
}
2727
}
2828

29-
let inputArray_Int: [Int] = Array(0..<1_000_000)
30-
let inputArray_Class: [Slow] = (0..<100_000).map(Slow.init(num:))
29+
let inputArray_Int: [Int] = Array(0..<500_000)
30+
let inputArray_Class: [Slow] = (0..<50_000).map(Slow.init(num:))
3131

3232
@inline(never)
3333
func removeAll<T>(_ arr: [T]) -> [T] {

stdlib/public/core/ArrayBuffer.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#if _runtime(_ObjC)
1919
import SwiftShims
20+
import Builtin
2021

2122
@usableFromInline
2223
internal typealias _ArrayBridgeStorage
@@ -201,10 +202,12 @@ extension _ArrayBuffer {
201202
if bufferIsUnique {
202203
// As an optimization, if the original buffer is unique, we can just move
203204
// the elements instead of copying.
205+
if
204206
let dest = newBuffer.firstElementAddress
205207
dest.moveInitialize(from: mutableFirstElementAddress,
206208
count: c)
207209
_native.mutableCount = 0
210+
}
208211
} else {
209212
_copyContents(
210213
subRange: 0..<c,

stdlib/public/core/Builtin.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,9 @@ internal func _isUnique<T>(_ object: inout T) -> Bool {
709709
return Bool(Builtin.isUnique(&object))
710710
}
711711

712+
@_silgen_name("_swift_reallocObject")
713+
internal func _reallocObject(_ object: UnsafeMutableRawPointer, _ newSizeInBytes: Int) -> UnsafeMutableRawPointer?
714+
712715
/// Returns `true` if `object` is uniquely referenced.
713716
/// This provides sanity checks on top of the Builtin.
714717
@_transparent

stdlib/public/runtime/SwiftObject.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,11 @@ const HashableWitnessTable *getNSStringHashableConformance();
9797
}
9898
}
9999

100+
namespace swift {
101+
102+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_SPI
103+
HeapObject *_swift_reallocObject(HeapObject *obj, size_t size);
104+
105+
}
106+
100107
#endif

stdlib/public/runtime/SwiftObject.mm

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ OBJC_EXPORT __attribute__((__weak_import__))
7979
#endif
8080
}
8181

82+
bool isObjCPinned(HeapObject *obj) {
83+
#if SWIFT_OBJC_INTEROP
84+
/* future: implement checking the relevant objc runtime bits */
85+
return true;
86+
#else
87+
return false;
88+
#endif
89+
}
90+
91+
// returns non-null if realloc was successful
92+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_SPI
93+
HeapObject *swift::_swift_reallocObject(HeapObject *obj, size_t size) {
94+
if (isObjCPinned(obj) || obj->refCounts.hasSideTable()) {
95+
return nullptr;
96+
}
97+
return (HeapObject *)realloc(obj, size);
98+
}
99+
82100
#if SWIFT_OBJC_INTEROP
83101
/// Replacement for ObjC object_isClass(), which is unavailable on
84102
/// deployment targets macOS 10.9 and iOS 7.

0 commit comments

Comments
 (0)