Stream: contributing

Topic: PSA: Seamless Slice


view this post on Zulip Brendan Hansknecht (Mar 17 2023 at 17:28):

TLDR

We have new feature that optimizes strings and lists. Please download a nightly release from 2023-03-17 or later and run any string/list related code you have with it. If possible, also run the output executable with valgrind. If the results are incorrect or there is a memory leak, please file an issue.

Details

Recently, we added a new feature to Roc, seamless slices. Seamless slices are made to avoid unnecessary allocations and copying when getting subsets of strings and lists. Instead, we seamlessly convert them into slices behind the scenes. This can give more performance without anything changing in end user code.

This comes to play in a few different locations. As a simple example of the performance gains. Splitting the text of Alice and Wonderland (repeated many times to give reasonable results) by spaces is approximately 50% faster. That said, the real gains come in any algorithm that might drop the first n elements of a List, or similarly create sublists. For example, parsers can benefit greatly from this. This function removes backslash newlines from a string in roc:

removeBackslashNewlines = \src ->
    helper = \in, out ->
        when in is
            ['\\', '\r', '\n', ..] ->
                helper (List.drop in 3) out
            ['\\', '\n', ..] ->
                helper (List.drop in 2) out
            [x, ..] ->
                nextOut = List.append out x
                helper (List.drop in 1) nextOut
            [] ->
                out

    helper src (List.withCapacity (List.len src))

Before, this function would have been mindbogglingly slow. Every call to List.drop would lead to shifting the entire input list. Instead we have slices. The performance is now great.

Please Help Test

Given this is a new feature that has underlying memory ramifications, it would be great to get more people testing their code now that the new feature is out. Please download a nightly release from 2023-03-17 or later and run any string/list related code you have with it. If possible, also run the output executable with valgrind. If the results are incorrect or there is a memory leak, please file an issue.

view this post on Zulip Brendan Hansknecht (Mar 17 2023 at 17:32):

Also, please feel free to ask any questions here.


Last updated: Jul 06 2025 at 12:14 UTC