Skip to content

pjullrich/f_enum

Back Eliminate unnecessary NIF calls and optimize Elixir-side operations

Commit details

Eliminate unnecessary NIF calls and optimize Elixir-side operations

Description

Rust optimizations: - Eliminate FxHashMap → std HashMap rehash in frequencies: implement custom Encoder for FrequencyMap that calls Term::map_from_term_arrays directly, skipping ~1M SipHash computations. frequencies(binary) improved from 94ms → 78ms (17% faster). - Pre-allocate uniq result Vec instead of filter().collect() which starts at zero capacity and reallocates ~20 times for 1M elements. - Add panic = "abort" to eliminate unwind tables. - Remove empty-slice branch from as_i64_slice! macro. Elixir optimizations: - Skip nif_length NIF call for chain operations that preserve length (sort, reverse, concat, slice, take, drop). Output length computed in pure Elixir arithmetic instead. - Replace binary at/slice/take/drop/count/concat with pure Elixir: binary_part for O(1) sub-binary references, pattern matching for at, byte_size arithmetic for count, << >> for concat. No NIF boundary crossing for any of these. - Remove O(n) length(list) call from new/1 — use nif_length (O(1) field read) instead. - Collapse duplicate range_to_start_count clauses. - Optimize at/2 binary positive index path to avoid div. Add scaling benchmark suite (bench/scaling_bench.exs) for testing speedups across input sizes from 100 to 1M.

Metadata

Author
Peter Ullrich PJUllrich@users.noreply.github.com
Committed
Commit

Contributors

  • Peter Ullrich PJUllrich@users.noreply.github.com Author
  • Claude Opus 4.6 (1M context) noreply@anthropic.com Co-author