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.
Attempting an improvement on the (use of the) Trigram function.
We do this by moving the slice allocation outside the trigram function.
Trigrams
is only used byTokenize
, and that function can actually compute the result slice size while cleaning up the input. Thetrigram
function now takes the result slice and a location to start populating the output.Since this inherently changes the function signature, we unexport it, and export the
Trigram
method as a wrapper for this unexported function that implements the old function signature. Since the exported method must behave as per the old code, it still needs to do its own memory alloc, and as a result does not see much improvement. Most of the improvement here is seen in theTokenize
method.Benchmarks on
Tokenize
(on my machine) show ~1.5x improvementUPDATE: credit to @Merovius for this idea
Second optimization: remove the alloc for a []rune slice in the trigram function by walking the string with 3 variables to keep track of a start, middle, and end index, using the utf8 package DecodeRune function to find an appropriate increment each iteration without the need to allocate the rune slice.
This brings the final time down to ~2.6us per call to
Tokenize
, a ~4.2x improvment