Repository navigation
Description
Formatting a deeply nested enum repeatedly copies complete child strings into new parent strings. Output size grows linearly with nesting, but copied data and temporary allocations grow quadratically.
Confirmed on fe67557, Windows x64, Haxe 4.3.7, MSVC 19.44, default collector:
enum Chain { End; Link(next:Chain); }
var value = End;
for (i in 0...depth) value = Link(value);
var text = Std.string(value);The result is Link(Link(...End...)), adding six characters per level. At depth 2,048 the output is only 12,291 characters, but one formatting call creates about 22.5 MiB of large temporary buffers.
| Depth | Output characters | Std.string median | Explicit StringBuf control | Large allocation growth: enum / control |
|---|---|---|---|---|
| 1,024 | 6,147 | 1.0291 ms | 0.0505 ms | 3,646,720 / 159,688 bytes |
| 2,048 | 12,291 | 4.9130 ms | 0.0413 ms | 22,545,664 / 329,656 bytes |
Method: 32 single-call samples per path, first discarded, medians of the remaining 31, alternating order, full GC before each sample, automatic GC enabled. Every output is checked against the complete expected string outside timing. No collection occurred inside any timed enum call, verified by the mark ID. MEM_INFO_LARGE growth is read before and after the call; with no intervening collection it includes the temporary large conversion/concatenation buffers, rather than only the final live result. Small temporary allocations are not included in that figure.
The explicit control appends the known Link( prefixes, End and closing parentheses to StringBuf. It is a simple lower-cost way to build this particular expected output, not an implemented general enum formatter or an application-wide speedup estimate. The source-level repeated copying establishes the complexity; allocator thresholds and policies also affect the timing jump between these sizes.
In src/Enum.cpp, EnumBase_obj::toString recursively formats parameters and uses string concatenation around each completed child result. Its one-parameter case constructs the entire child string before adding the parent tag and parentheses. Recursive traversal also grows the native call stack with nesting.
Consider flattening enum formatting through a builder and an explicit traversal stack, so each piece of output is appended once. Preserve formatting syntax, Unicode, nulls, shared values at each occurrence, and the order and number of custom toString calls. Any traversal state holding Haxe objects must remain rooted and correctly handled by moving collection, including when custom formatting invokes GC. This can be done inside libhxcpp without compiler or standard-library changes.
Added a local conventional regression before implementation work, test/regression/EnumFormattingShapes. It passes on the current runtime and covers complete depth-2,048 output, zero/null parameters, Unicode, multi-parameter nesting, repeated shared enum children, and stateful custom formatting that forces collection. No timing assertion or runtime implementation change is included.
Activity