Refactor to avoid multiple string heap allocations - #13897
Open
haydonryan wants to merge 1 commit 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.
Heya,
I'm experimenting with building llm prompts to do code review to improve size and speed and it found an interesting one pop up. Resulting change is faster, results in a slightly smaller binary and less heap allocations.
This change is already covered by integration test suite, but let me know if you want me to add unit tests.
format_tex_fieldescaped TeX special characters by mapping each characterthrough
tex_mapper, collecting the results into aVec<String>, and joiningthem:
tex_mapperreturns aStringper character. Becausestd::Stringhas nosmall-string optimization (it is a
Vec<u8>), every character produced a heapallocation, plus one more for the
Vecitself and one for thejoin. Thisruns once per field on the
ptx -Toutput path, so a single line with severalfields caused dozens of allocations.
Rewrote
format_tex_fieldto push each escaped character directly into onepreallocated
String, and removedtex_mapper:The escaping rules are identical; only the intermediate
Vec<String>and theper-character allocations are gone. This removes roughly one allocation per
field (down from roughly one per character) and deletes a whole helper
function.
ptx)cmpequal)