Stream: beginners

Topic: Comparison of List U8


view this post on Zulip Luke Boswell (Jul 21 2023 at 06:43):

Random question, but how do we do equality for a List U8?

If I have a large List U8 and then take lots of smaller sublists, is it super efficient to compare these smaller List U8's against each other if I pass them into functions etc? I guess I'm asking if these would be just a comparison of the slice or reference, or is it something deeper?

view this post on Zulip Luke Boswell (Jul 21 2023 at 06:44):

Would it be inefficient to use a List U8 as a key to a Dict?

view this post on Zulip Luke Boswell (Jul 21 2023 at 07:07):

Or could I use something like Box (List U8) if I want it to be a reference?

view this post on Zulip Folkert de Vries (Jul 21 2023 at 07:49):

dicts use a hash, which means the whole list is traversed. That might be a problem for very large lists

view this post on Zulip Folkert de Vries (Jul 21 2023 at 07:50):

really depends on the specifics how good/bad of an idea this is

view this post on Zulip Luke Boswell (Jul 21 2023 at 08:43):

I'm thinking of a use case when decoding JSON, of using a List U8 input bytes as an index into another data structure.

view this post on Zulip Luke Boswell (Jul 21 2023 at 08:44):

I feel like if the List U8 is actually stored on the heap and is only a reference, then equality may be very fast.

view this post on Zulip Folkert de Vries (Jul 21 2023 at 08:52):

well yes but not with hashing.

view this post on Zulip Folkert de Vries (Jul 21 2023 at 08:53):

for equality we first compare the lengths, then the pointers, and if the pointers are not the same we compare elements

view this post on Zulip Folkert de Vries (Jul 21 2023 at 08:53):

but a hash will always look at all the elements

view this post on Zulip Luke Boswell (Jul 23 2023 at 00:06):

If I have a record such as {field : List U8, value : List U8} and the List U8 are slices from another larger List U8 is this record a fixed size? I guess I am concerned that putting these lists in record fields will make unnecessary allocations.

view this post on Zulip Brendan Hansknecht (Jul 23 2023 at 00:32):

The record is always fixed sized, the data being allocation free depends on how you use it

view this post on Zulip Brendan Hansknecht (Jul 23 2023 at 00:32):

If they are always referencing a larger list and never edited, they would be seamless slices and never allocate

view this post on Zulip Luke Boswell (Jul 23 2023 at 00:34):

Thanks Brendan that makes sense


Last updated: Jul 06 2025 at 12:14 UTC