Stream: compiler development

Topic: appendScalar


view this post on Zulip Brendan Hansknecht (Jan 29 2024 at 19:34):

So I know that we removed Str.appendScalar. Now you have to do something like this:

Str.toUtf8 string
    |> List.append byte
    |> Str.fromUtf8

Do we think this will be at all common? If so, we should consider how this can be done with better performance. This version has a pretty large cost compared to the old Str.appendScalar. This require an allocation for all small strings. Cause there is no small list optimization. On top of that, it will recheck the entire utf8 instead of just checking the very last byte.

view this post on Zulip Brendan Hansknecht (Jan 29 2024 at 19:38):

In fact, it would be better to do:

bytesStr <- Str.fromUtf8 [byte] |> Result.try
Str.concat string byteStr

But I don't think many users would think of that. It also still requires allocating unnecessarily. Cause you allocate a list with a single byte just to make it into a small string and deallocate it. Of course, this will be a big perf win for large strings cause you are only checking 1 byte is valid utf8, not rechecking the entire string.

view this post on Zulip Richard Feldman (Jan 29 2024 at 20:19):

yeah so my hypothesis is that in practice if you want something like that, you probably want to be working directly with List U8 and not Str anyway

view this post on Zulip Richard Feldman (Jan 29 2024 at 20:19):

and if someone encounters a specific use case where that turns out not to be the case, we can discuss!


Last updated: Jul 06 2025 at 12:14 UTC