Performance
Allocation budget, memory bounds per entry point, the benchmark suite, and ReaderConfig tuning — each figure with the test that asserts it.
On this page
Allocation per segment
from_bytes borrows every tag and value from the input and allocates only the
element vector:
let input: &[u8] = b"BGM+220+PO-4711+9'";
// One allocation (the element vector); text is borrowed.
let seg = edifact::from_bytes(input).next().unwrap().unwrap();
assert_eq!(seg.tag, "BGM");
For one segment with E data elements, X release-escaped values and V
non-empty component values:
| Path | Allocator calls |
|---|---|
from_bytes | [E ≥ 1] + X — the element vector, allocated once at its exact length, plus one owned string per escaped value |
from_reader | [E ≥ 1] + 1 + V — the element vector, the tag and every non-empty value are owned |
from_bytesadds one call when its reusable element buffer grows: at the first segment with elements, then once per doubling of the largest element count.- Components use
SmallVec<[_; 4]>(withunion): up to four components per element need no allocation of their own; a fifth spills to the heap. Element::repeatsstays unallocated unless the element actually repeats.- A value is
Cow::Ownedonly when a release escape had to be resolved (?+→+); otherwise it isCow::Borrowed.
Memory by entry point
| API | Holds at a time | Bound |
|---|---|---|
from_bytes | one segment | the input slice is yours; streaming peak ≤ 2 704 B on the 1 MB fixture |
from_bytes(..).collect(), deserialize, from_bytes_decoded | every segment | peak ≤ 25 217 536 B for the 964 250 B fixture (26.2 × input); ≤ 128 × input in the worst case (one-byte elements) |
from_reader, from_bufread | one segment and an 8 KiB read buffer | a segment is at most max_segment_bytes (64 KiB by default); streaming peak ≤ 9 347 B on the 1 MB fixture |
deserialize_each_from_reader | one segment and one T | as from_reader |
message_windows, message_windows_from_reader, deserialize_messages* | one UNH…UNT window | at most max_message_bytes of input (16 777 216 by default), ≤ 128 × that in heap — independent of stream length |
WriterEmitter<W> | nothing — each event goes straight to W | 0 allocations per event; it does not buffer, so wrap a file or socket in BufWriter |
to_bytes / to_edifact_string | the output | the returned buffer |
Every entry point is bounded by default: 65 536 bytes per segment,
16 777 216 per message, 268 435 456 in total
(Parsing → DoS hardening).
Heap grows per element, not per segment: a parsed window or a collected
interchange takes at most 128 × its input bytes (measured 110 × slice,
105 × reader for a message of one-byte elements). Iterate rather than
.collect() untrusted input, and tighten the budgets to your traffic:
use edifact::{ReaderConfig, message_windows_from_reader_with_config};
let config = ReaderConfig::default()
.max_message_bytes(1 << 20) // each window: ≤ 1 MiB input, ≤ 128 MiB heap
.max_input_bytes(64 << 20); // the whole stream
let input = std::io::Cursor::new(b"UNH+1+ORDERS:D:96A:UN'BGM+220+1'UNT+3+1'".to_vec());
for window in message_windows_from_reader_with_config(input, config) {
let window = window?;
assert_eq!(window.segments.len(), 3);
}
# Ok::<(), edifact::EdifactError>(())
WriterEmitter writes EDIFACT to any std::io::Write one event at a time:
use edifact::{EdifactEvent, EventEmitter, WriterEmitter};
let mut out = Vec::<u8>::new();
let mut emitter = WriterEmitter::new(&mut out);
emitter.emit(EdifactEvent::start("BGM"))?;
emitter.emit(EdifactEvent::element("220"))?;
emitter.emit(EdifactEvent::element("PO-4711"))?;
emitter.emit(EdifactEvent::EndSegment)?;
assert_eq!(out, b"BGM+220+PO-4711'");
# Ok::<(), edifact::EdifactError>(())
Where these numbers come from
The 1 MB fixture is one_mb() in benches/bench_data.rs (964 250 B, 44 011
segments). Peak figures are 64-bit and are asserted as ceilings: a test fails if
the measured value rises above the figure, or falls more than 20 % below it.
| Claim | Test |
|---|---|
| Per-segment allocator calls on both paths | allocation_budget: orders_fixture_slice_budget, orders_fixture_reader_budget, escaped_value_costs_one_allocation |
repeats unallocated, element vector at exact length | allocation_budget: repeats_unallocated_on_both_paths |
| Collect peak 25 217 536 B | allocation_budget: one_mb_peak_heap_slice_collect |
from_bytes streaming peak 2 704 B | allocation_budget: one_mb_peak_heap_slice_stream |
from_reader streaming peak 9 347 B (8 KiB BufReader included) | allocation_budget: one_mb_peak_heap_reader_stream |
WriterEmitter allocates nothing per event | allocation_budget: writer_emitter_allocates_nothing_per_event |
parse_unh, parse_ung, iter_messages_by_type allocate nothing | allocation_budget: parse_unh_allocates_nothing, parse_ung_allocates_nothing, messages_by_type_iter_allocates_nothing |
| Default limits 65 536 / 268 435 456 / 16 777 216 bytes on every entry point | resource_limits: default_limits_are_the_documented_constants, default_max_segment_bytes_is_64_kib, every_default_entry_point_stops_endless_input, every_default_window_stops_unterminated_message |
A window is bounded by max_message_bytes, its peak independent of stream length | resource_limits: max_message_bytes_boundaries, unterminated_message_message_windows, unterminated_message_deserialize_messages, unterminated_message_deserialize_each, per_message_cap_on_long_lived_stream, peak_heap_bounded_by_limits_unterminated, peak_heap_bounded_by_limits_long_lived |
| A window or collected interchange takes ≤ 128 × its input bytes | allocation_budget: worst_case_window_heap_within_factor |
max_input_bytes stops endless whitespace | resource_limits: endless_whitespace_reader_stops, whitespace_slice, peak_heap_bounded_by_limits_whitespace |
docs_claims checks that the figures on this page match the committed
constants in allocation_budget.
Benchmarks
divan for quick micro-benchmarks, Criterion for statistics and baselines:
cargo bench -p edifact --bench bench_core
cargo bench -p edifact --bench bench_criterion
Criterion writes its reports to target/criterion/report/index.html.
| Benchmark | Measures |
|---|---|
tokenizer/small, tokenizer/1mb | Tokenizer throughput |
parser/small, parser/1mb | from_bytes parse and collect |
reader/1mb | from_reader parse and collect |
reader/parse_reader_chunked | Reader path across read-buffer boundaries |
writer/sample_message | Serialising a message |
validation/context_dispatch_floor | Per-call ValidationContext dispatch over a no-op validator |
validation/validate_profile_custom_pack | One ProfileRulePack |
validation/validate_profile_composed_packs | Composed packs (extend_from) |
validation/parse_large_message, validation/validate_large_message | Parse and validate a 1 MB interchange |
grouping/direct_indices/*, grouping/resolve_ordered/* | Group traversal and ordered resolution at 10 k / 20 k / 40 k child groups |
How CI runs and compares them is described in
benches/README.md.
Property and fuzz testing
The bolero harness covers parse, write, validate,
grouping and CONTRL invariants. cargo test runs it in random-exploration
mode; with cargo-bolero it runs under a coverage-guided engine:
cargo bolero test -p edifact bolero_harness
cargo +nightly bolero test -p edifact bolero_harness --engine libfuzzer
Profiling
# flamegraph
cargo flamegraph -p edifact --bench bench_criterion -- \
--bench --profile-time=10 parser/1mb
# heap profile of the large-message probe
cargo build --release -p edifact --example bench_large_message_memory
heaptrack ./target/release/examples/bench_large_message_memory
Tuning ReaderConfig
use edifact::{ReaderConfig, from_bufread_with_config};
// Budgets you do not name keep their defaults.
let config = ReaderConfig::default().max_segment_bytes(256 * 1024);
let cursor = std::io::Cursor::new(b"...");
let _iter = from_bufread_with_config(cursor, config);
A max_segment_bytes below your partners' longest segment (a long FTX, say)
raises E020 SegmentTooLong; the other budgets raise E036 LimitExceeded.
max_segments and max_messages are unset by default; see
Parsing → DoS hardening.
Next steps
- Streaming — reader iterators and message windows
- Async Integration —
spawn_blockingpatterns - Error Reference —
E020andE036