let arr: [Int] = []
Does Swift not allocate any buffer in this instance because the array literal is empty?
Looking at the source code of the standard library, we can find a class named __EmptyArrayStorage
.
/// Class used whose sole instance is used as storage for empty
/// arrays. The instance is defined in the runtime and statically
/// initialized. See stdlib/runtime/GlobalObjects.cpp for details.
/// Because it's statically referenced, it requires non-lazy realization
/// by the Objective-C runtime.
///
/// NOTE: older runtimes called this _EmptyArrayStorage. The two must
/// coexist, so it was renamed. The old name must not be used in the new
/// runtime.
@_fixed_layout
@usableFromInline
@_objc_non_lazy_realization
internal final class __EmptyArrayStorage
: __ContiguousArrayStorageBase {
So there is still a part of the heap allocated for storing this instance of __EmptyArrayStorage
, but all the empty Swift arrays will share the same storage.