Skip to content

Avoid clearing large atomic copy destinations immediately before overwriting them #315

Closed

Description

@dimensionscape

Several fully initialized copy paths clear a large destination buffer and then overwrite every byte. This adds a full memory-write pass to large strings and atomic array copies, even though the zeroed contents are never observable.

In src/hx/gc/Immix.cpp, InternalNew routes large allocations through AllocLarge(inSize, true), which clears the allocation. Relevant callers include NewGCBytes/NewGCPrivate when inData is supplied, String GCStringDup/NewString before copying known contents, and Array_obj::fromData/copy through ArrayBase construction. This report concerns atomic data such as byte arrays and strings, not a blanket removal of initialization from object containers.

A focused comparison uses existing runtime paths without modifying the allocator. Each iteration transfers ownership from an old buffer to a replacement containing the same bytes:

// Clear, then fully overwrite:
void *next = hx::InternalNew(bytes, false);
memcpy(next, data, bytes);
hx::InternalReleaseMem(data);
data = next;

// Existing equal-size realloc path skips clearing the copied range:
data = hx::InternalRealloc(bytes, data, bytes);

InternalRealloc allocates through AllocLarge(inSize, false), copies the entire equal-size input, then releases the input. Both controls perform the allocation, copy and release. Its different source-ownership contract means InternalRealloc itself is not a replacement for Array.copy or fromData, whose source must stay valid. The comparison isolates an existing way to avoid the unnecessary clear, rather than implementing a new copy path.

Measured on Windows x64, MSVC 19.44.35213, Haxe 4.3.7 and libhxcpp fe67557, release HXCPP_M64, default collector, telemetry disabled:

Buffer size 64 replacements: clear then copy 64 replacements: equal-size realloc copy
1 MiB 1.3528 ms 0.8910 ms
8 MiB 31.1291 ms 23.1844 ms
32 MiB 141.2078 ms 91.0441 ms

Each result is the median of seven samples after a warm-up. Test order alternates between controls on successive samples. Initial allocation, cache warm-up, source filling and complete content verification are outside the timer. A joined worker seeds recycle capacity before testing, and a retained anchor keeps the setup stable. GC stays enabled, but gByteMarkID is checked across each timed loop and no collection occurs inside it. The result is about 1.3-1.6x faster for this focused replacement workload, not an application-wide speedup or a measurement of an implemented Array/String optimization.

Consider a runtime-internal allocation/copy facility for callers that can guarantee full initialization before publication. Keep ordinary zero-initialized allocation unchanged. Preserve string terminators and header/hash-slot flags, empty/null input behavior, source lifetime through allocation, moving/generational GC, write barriers where applicable, and any unused array capacity that can later become visible. Avoid exposing uncleared data if validation, initialization or allocation fails. Start with atomic copies rather than general pointer-containing containers.

Conventional local coverage was added first at test/regression/AtomicCopyInitialization/{Main.hx,build.hxml,stdout.txt}. It passes on the current runtime and checks every byte/unit in odd-sized large byte/string copies, verifies source/destination independence, collects between operations, and confirms ordinary large Bytes.alloc remains zero-filled. It contains no timing assertions. Add actual public copy/string construction benchmarks and allocation-failure/collector-mode coverage when implementing a specialized path.

Related: #196 removed an extra allocation/output copy from UTF-8 decoding and is closed. This is the separate initial clear before fully overwriting a large destination. No Haxe compiler or standard library changes are needed. No runtime implementation change has been made.

Activity

  1. added a commit that references this issue on Oct 27, 2026
    7154374
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