Stream: beginners

Topic: Is List and Dict access ref-counted?


view this post on Zulip Jared Cone (Nov 08 2024 at 17:12):

If you read an element from a List or Dict, is Roc's ref-counting able to avoid having to copy the data?

doSomething = \list ->
    result = List.get list 0
    list2 =
        when result is
            Ok item -> List.set list 0 {item & value : 0}
            _ -> list
    list2

will the above have to create a copy of item on get and on set, or will it all happen in-place?

view this post on Zulip Brendan Hansknecht (Nov 08 2024 at 20:39):

More or less same answer as the other thread. For recounted type (list, str, box, recursive tag), it will just take a reference. For the rest it will be a copy.

view this post on Zulip Brendan Hansknecht (Nov 08 2024 at 20:42):

As a note there is technically some room for improvement here if we analyze that some list data is guaranteed to be unmodified during a section of code. In that case we could take a raw reference to the data. This would mostly see gains in the gigantic record/tuple case. For other cases, the copy is cheap and it isn't even worth considering skipping it. We also already do this analysis for refcounted types to reduce the number of refcount increments and decrements.


Last updated: Jul 06 2025 at 12:14 UTC