refactor: drop redundant intermediate allocations#650
Open
Mimori256 wants to merge 3 commits into
Open
Conversation
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
Remove unnecessary heap allocations in three independent locations. The most impactful is
engine_serialize, which was cloning the entire serialized payload before handing it to the JS layer; the other two eliminate redundant intermediateStrings on code paths that are correct without them.src/blocker.rsPass
QParamdirectly toitertools::joininstead of mapping through.to_string().QParamalready implementsDisplay, anditertools::joinformats elements viaDisplay, so the per-parameter intermediateStringwas unnecessary. Saves one allocation per query parameter on every removeparam URL rewrite.src/cosmetic_filter_utils.rsReplace
key += &char.to_string()withkey.push(char)when appending a decoded\XXXXescape codepoint.String::pushwrites the char's UTF-8 bytes directly into the existing buffer, removing the per-escape-sequenceStringallocation.js/src/lib.rsEngine::serialize()already returns an ownedVec<u8>(seesrc/engine.rsandserialize_dat_fileinsrc/data_format/mod.rs, which builds a freshVecto prepend the magic+hash header). The extra.to_vec()was producing a full duplicate of the entire serialized engine. Remove it and letJsArrayBuffer::copy_from_slicedo the only required copy (Rust heap → JS heap). TheMutexGuardis still released at the end of theif letarm; this is safe because the returnedVec<u8>owns its data.