[Vm-dev] Primitive to crop a ByteArray?

Bert Freudenberg bert at freudenbergs.de
Thu Nov 8 15:38:30 UTC 2012

On 2012-11-08, at 16:22, Mariano Martinez Peck <marianopeck at gmail.com> wrote:

> Hi guys. I have the following scenario. I have a buffer (ByteArray)
> that I pass by FFI to a function of size N. This function puts data in
> the array and answers me the M number of bytes that it put. M <= N.
> Finally, I need to copy the array of size N to the accuare size M.
> To do that, I am using #copyFrom:to:. If the ByteArray is large (which
> could be the case), this function takes significant time because it
> needs to allocate space for the new large resulting array. So...is
> there a destructive primitive where I can "crop" the existing array,
> modify its size field and mark the remaining bytes as "free space for
> the heap".
> Do we have a primitive for that?

We do not have any primitive that changes an object's size.

However, if your problem is indeed the time for allocating the new array, then maybe there should be a primitive for that? E.g. one that copies a portion of one array to a new object. This would avoid having to initialize the memory of the new array - this is what's taking time, otherwise allocation is normally constant in time.

OTOH it seems unusual that you would have to use extremely large buffers where initialization time matters. E.g. if you were to use this for reading from a file or stream then it might make more sense to use many smaller buffers rather than a single huge one, no?

- Bert -

More information about the Vm-dev mailing list