Skip to content

Chunked array growth repeatedly copies whole buffers in socket reads and CFFI appends #287

Closed

Description

@dimensionscape

Chunk appends use exact-size array growth, so accumulating a stream repeatedly copies everything received so far. This affects native whole-socket reads and CFFI buffers. A geometric capacity policy removes most of the cost without knowing the final length in advance.

Measured at d734565 on Windows x64, Haxe 4.3.7, MSVC 19.44.35213, release HXCPP_M64. No runtime implementation changes were made.

Native socket reader

_hx_std_socket_read receives into a 16 KB stack buffer, then calls result->memcpy(result->length, buf, len). That calls EnsureSize and ultimately ArrayBase::Realloc when capacity is exhausted. Increments larger than 16 elements take reserve(inSize), which records exactly the requested capacity. The next receive generally has to grow the buffer again. InternalRealloc allocates a new buffer and copies the existing contents.

I compared cpp.NativeSocket.socket_read with receiver.input.readAll() over loopback. Both read until EOF without knowing the final size. The sender wrote a prebuilt payload and closed its write side. Construction and full byte-for-byte verification were outside timing. Timings include receiving and accumulating the stream, one warmup and the median of five samples for each path. The native primitive was called directly to exclude subsequent String conversion.

Payload Native socket reader input.readAll control
4 MiB 104.62 ms 11.75 ms
8 MiB 359.54 ms 15.58 ms
16 MiB 1,446.17 ms 33.37 ms

At 16 MiB the native reader was about 43 times slower. Network timings vary locally; this is a focused comparison, not a general throughput claim. The scaling and source path point to accumulation rather than extra recv syscalls as the remaining problem.

CFFI buffers

buffer_append and buffer_append_sub call __SetSize(oldLength + addedLength), then copy the new chunk. Repeated 1 KB appends take the same exact-growth path. Unlike append_char, they do not receive the array's geometric push growth.

The CFFI measurement called the actual APIs through hx_cffi function pointers. Each sample created an empty buffer and appended a prebuilt 1 KB block. One control preallocated the final size; a second control reserved capacity geometrically as each append needed more space. The geometric control did not use the final length to choose capacity, and still called the same CFFI append API. Allocation/reserving and all appends were timed. Full byte-for-byte verification, including embedded NULs, was outside timing. One warmup, median of five samples, full GC before each timed sample.

Result size Ordinary appends Final-size preallocation Geometric reserve control
1 MiB 135.26 ms 0.1645 ms 0.2610 ms
2 MiB 386.94 ms 0.2676 ms 0.4406 ms
4 MiB 1,391.85 ms 0.5840 ms 0.9634 ms

With exact 1 KB increments, building 4 MiB requests about 8 GiB of copying of prior contents alone. The geometric control is roughly 1,445 times faster in this narrow buffer-building case. This does not predict the speedup of a complete native extension.

Runtime scope

Use amortized capacity growth for accumulation paths such as socket_read and CFFI append operations. Keep explicit reserve/resize and logical length behavior intentional, preserve allocation overflow checks, and cover GC modes and ownership. Changing all explicit resize calls to overallocate may be unnecessary; an append-capacity helper could keep the policy at the relevant call sites.

Source locations:

  • src/Array.cpp: ArrayBase::Realloc and reserve.
  • include/Array.h: Array_obj::memcpy and EnsureSize.
  • src/hx/gc/Immix.cpp: InternalRealloc.
  • src/hx/CFFI.cpp: buffer_append and buffer_append_sub.
  • src/hx/libs/std/Socket.cpp: _hx_std_socket_read.

This can be addressed entirely within libhxcpp. No Haxe compiler or stdlib change is required.

Local conventional regression coverage was added first:

  • test/regression/CffiChunkAppend covers chunk contents including NUL, length, empty appends, mixed append APIs and appending after shrinking.
  • test/regression/SocketChunkRead covers an empty stream and a 1 MiB stream spanning multiple receive chunks, with full byte verification.

Both have Main.hx, build.hxml and stdout.txt, pass on the current runtime and contain no timing assertions. Add repeatable performance cases alongside the functional coverage when fixing this.

Activity

  1. added a commit that references this issue on Oct 27, 2026
    9b92bfc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions