Non-allocating Uint8Array access
This change adds a new Uint8Array type that mirrors the V8's type. It is a readonly ref struct. The use case is to retrieve it and pass it around inside the calling code for some small distance until its contents can be copied out to a byte[] array.
By itself, this change does no avoid the allocation of a V8ObjectImpl as that still happens in V8FastArgs.ctor.
Hi @AnsisMalins,
Some observations:
- Is this a complete change? The new code in V8FastArgs.cs appears to call a new
V8FastArgImplmethod, but the commit doesn't include V8FastArgImpl.cs. - We strongly believe in API consistency and uniformity, whereas this change seems to be a minimal one tailored specifically for your use case. For example, it only supports a minimal set of operations, only for byte arrays, and only in
V8FastArgs(as opposed to the singularV8FastArg). - This API is fragile, as instances of the new type are valid only during the call and could crash if saved and used later.
We do like this idea in general, as ClearScript already provides special treatment for typed arrays elsewhere. Typed arrays were designed for performance-critical scenarios. Can you tell us anything about yours?
Thanks!
Yes, I missed a file there. Oops!
The specific case I'm trying to solve is, JavaScript code sends new byte arrays to C# side many times per frame, 60 frames per second. As is, for every JavaScript object a V8ObjectImpl is created on the C# side, and I'm trying to bring allocations per frame down to zero. I have limited control over the JavaScript side, so I can't ensure it reuses a small amount JavaScript objects.