Conversation
Add fast-paths for fully valid and printable strings to return [string] immediately, avoiding the chunk loop and allocations entirely for uniform inputs. In the fallback chunk loop: - Reuse the first decoded codepoint and classification, eliminating redundant decoding and the intermediate wrapper. - Replace repeated <> binary concatenation with zero-copy sub-binary slicing via binary_part/3, removing intermediate binary reallocations. Assisted-by: Gemini:3.8-Flash Assisted-by: Codex:GPT-6
|
Thanks! This is an incomplete benchmark because very early in the string we see it is invalid: We need to assert worst case scenarios like the invalid byte being at the end. In any case, I am sure this could be much faster:
Basically, I am saying we can likely remove the initial valid?/printable? check, anid that can be merged in the traversal only needs to compute the size of the next valid chunk. |
|
Thank you for the feedback @josevalim, I investigate this more then. |
@josevalim A bit off-topic, but I couldn't find an answer to this anywhere. Could the type system keep track of whether a string is known to be valid UTF-8—for example, if it was already validated at compile time or during deserialization? |
|
Yes, it could, but it is not a goal for now. For this pull request, I'd make it so chunking is close to a non-op until an invalid chunk is found, as proposed above. |
Assisted-by: Gemini:3.8-Flash
Assisted-by: Codex:GPT-6
Add fast-paths for fully valid and printable strings to return [string]
immediately, avoiding the chunk loop and allocations entirely for uniform
inputs.
In the fallback chunk loop:
redundant decoding and the intermediate wrapper.
slicing via binary_part/3, removing intermediate binary reallocations.
Bench:
Results: