I'm writing my own super slim standard library for microcontrollers. The first version is functioning but does not have Arrays, Dictionaries or Strings. I'm adding them one at a time and trying to seriously squeeze down what goes in to the bare minimum I need. My next target is adding arrays.
I am not supporting user defined classes in my product and I am not supporting objective c. I only want c array like semantics for storing value types. At least for now.
From the documentation in swift/docs and looking over the stdlib code, I can see that arrays seem to have a fairly small runtime but the standard library code for them is very large. In particular there seems to be this russian dolls effect with many structures nested within each other.
A few questions first to make sure I'm not talking nonsense. My understanding is...
In the simpler case of ContiguousArray
, it contains a _ContiguousArrayBuffer
that contains a field _storage: __ContiguousArrayStorageBase
, which acts a pointer to an underlying memory buffer with a header followed by elements. The code for buffer
makes use of pointer arithmetic and undocumented builtins such as Builtin.allocWithTailElems_1
and Builtin.projectTailElems
to manipulate the memory buffer. It's unclear to me how these work and I can't find documentation. How is the header size determined?
This is described in docs as being handled by ManagedBufferPointer<_ArrayBody,T>
. It looks like that's not strictly true but the code is nearly identical, maybe copy/pasted at one time and diverged?
The header is an instance of _ContiguousArrayStorage
, which is a __ContiguousArrayStorageBase
, which is a __SwiftNativeNSArrayWithContiguousStorage
, which is an empty class on platforms that don't support objective C or cocoa. But in reality, these header instances aren't standard heap allocated class instances as I understand it, they could be allocated in stack memory, using the magic described above, is that right?
The structure for Array
itself is even more complex, allowing for Cocoa/NSArray
bridging.
I'd like to radically trim down this hierarchy in my version.
-
What would be the danger/downside in my case in just implementing effectively
ContinuousArray
, if necessary renaming itArray
to get rid of all theNSArray
/Cocoa/objc compatibility that I don't need? -
In my version, what would be the harm of replacing
__ContiguousArrayStorageBase
with_ArrayBody
as the header?