Every call allocates a temporary buffer #11
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Wrappers that pass a struct by pointer allocate a one-element FFI array on every call. Wrappers
that accept an array allocate a copy on every call. In per-pixel and per-frame code this creates
constant garbage and GC pauses.
Current behavior
There are 71 such sites. 57 wrap an
Image, and theImageDraw*family runs inside per-pixelloops — one allocation per pixel.
Array conversion adds a second problem:
38 conversions copy every element into a fresh array. A 1000-point strip at 60 FPS is 16 KB of
garbage and 1000 struct copies per second per strip. The JIT cannot optimise the allocation away.
Proposal
1. Reuse one struct slot per type
Generated wrapper:
The depth counter makes re-entry safe when a callback calls another wrapper during a C call. The
normal path is depth 1, so the allocation happens once per type for the process lifetime.
2. Skip the copy-out for image draws
ImageDraw*andImageClearBackgroundchange pixels, not theImagefields. Return the inputinstead of
_o[0]. Everything else keeps the copy-out, because it may changewidth,height,formatordata.Match by function-name prefix with an explicit list, defaulting to "copy":
3. Reuse array buffers
Keep one buffer per type and depth, growing to the high-water mark and never shrinking:
Rules:
add a
RETAINS_PTRlist for exceptions and re-audit it on every raylib bump.n == 0passesniland allocates nothing.4. Document the fast path
Passing a cdata array already skips conversion. Users who care can allocate once:
Do not add per-element helper closures; they cost more than the array.
What stays the same
GetMousePosition) allocate one cdata per call. They areuser-visible values, so caching them would be wrong.
M.Cremains the escape hatch.Acceptance criteria
UpdateCamera200 000 times with GC stopped grows memory by less than 4 KiB.ImageDrawPixel1 000 000 times shows no per-call garbage.DrawLineStripwith 1000 points, 10 000 times, allocates only on the first call.make check-bindingspasses.Notes
#15