Conversation
- Add shrink_to_fit() in decoder_decode_chunk_impl to release excess String capacity before Rustler copies into BEAM binary. Without this, streaming workloads over-allocate up to 3x per chunk due to max_utf8_buffer_length worst-case estimates. - Replace write_all() with copy_from_slice() in encode_impl and encode_batch, removing the std::io::Write import. copy_from_slice is infallible when lengths match (guaranteed by prior allocation). - Eliminate unnecessary String heap allocations in error paths: use String::new() in decode_impl/decode_batch (Elixir side discards the value), return &'static str from canonical_name and detect_bom since encoding_rs::Encoding::name() already returns &'static str. - Remove unused atoms (unknown_encoding, encode_error, decode_error, no_bom) from the atoms! macro. https://claude.ai/code/session_01Mg82oZeWczDyGDe5eoGXXj
…hreshold - Extract empty_binary() helper to de-duplicate OwnedBinary::new(0) logic across encode_impl and encode_batch, with accurate doc comment explaining that enif_alloc_binary(0) is not zero-cost - Standardize error-path comments to reference Elixir normalize_result/1 - Add 4KB threshold to shrink_to_fit() in streaming decoder to avoid realloc overhead on small chunks where savings are negligible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR optimizes the encoding_rs Elixir bindings by reducing unnecessary heap allocations and simplifying error handling patterns. The changes focus on using stack-allocated strings where appropriate and leveraging infallible operations instead of fallible ones.
Key Changes
unknown_encoding,encode_error,decode_error, andno_bomatom definitions that were no longer being usedString::new()) instead of heap-allocated error messages, since the Elixir side discards these values anywaycanonical_namefunction: Changed return type from(Atom, String)to(Atom, &'static str)to avoid unnecessary string allocations by returning static string references directly fromencoding_rsdetect_bomfunction: Similarly changed return type to use&'static strinstead ofStringfor the encoding namewrite_allwithcopy_from_slice: In bothencode_implandencode_batch, replaced the falliblewrite_alloperation with the infalliblecopy_from_slicesince the buffer is pre-allocated to the exact required sizeuse std::io::Writewhich is no longer needed after replacingwrite_alloutput.shrink_to_fit()indecoder_decode_chunk_implto release excess capacity before passing the string to Rustler, preventing oversized buffers from being held in memory until garbage collectionImplementation Details
The changes maintain API compatibility while improving performance:
&'static str) are returned where the encoding_rs library already provides them, eliminating unnecessary allocationshttps://claude.ai/code/session_01Mg82oZeWczDyGDe5eoGXXj