Stream: compiler development

Topic: removing `Nat`


view this post on Zulip Richard Feldman (Oct 21 2023 at 01:28):

I did some Nat removals and realized that the implementation Dict uses Nat extensively, including in the record that represents it

view this post on Zulip Richard Feldman (Oct 21 2023 at 01:30):

@Brendan Hansknecht if we made the record itself opaque, do you think replacing the remaining usages of Nat in there is feasible?

view this post on Zulip Richard Feldman (Oct 21 2023 at 01:31):

or should we explore some way of trying to keep Nat around behind the scenes, and only used by Dict.roc internally?

view this post on Zulip Brendan Hansknecht (Oct 21 2023 at 01:31):

I think the record is already opaque

view this post on Zulip Brendan Hansknecht (Oct 21 2023 at 01:32):

Should be fine to just change everything to u64 since that is what list indices will use.

view this post on Zulip Richard Feldman (Oct 21 2023 at 01:33):

er sorry, I mean opaque and implemented behind the scenes, like how List is

view this post on Zulip Richard Feldman (Oct 21 2023 at 01:33):

like for example, this: https://github.com/roc-lang/roc/blob/cd7a031d09fcd0a2f95cb2933b675028cd0b7730/crates/compiler/builtins/roc/Dict.roc#L92-L111 includes dataIndices : List Nat

view this post on Zulip Richard Feldman (Oct 21 2023 at 01:33):

if we change that to List U64, then Dicts will take up another 4B on the stack on 32-bit targets for no benefit

view this post on Zulip Brendan Hansknecht (Oct 21 2023 at 01:35):

Oh yeah, it's an index map so it stores nats....

view this post on Zulip Brendan Hansknecht (Oct 21 2023 at 01:35):

I mean long term we need to deal with hashing differently on 32 bit systems among other things

view this post on Zulip Brendan Hansknecht (Oct 21 2023 at 01:36):

I think it would be fine for now to just make this a 64bit dict implementation and add a to-do for 32bit.

view this post on Zulip Richard Feldman (Oct 21 2023 at 01:37):

works for me! :thumbs_up:

view this post on Zulip Brendan Hansknecht (Oct 21 2023 at 01:39):

Also, long term, I'm not 100% sure we want to keep dict in roc. We may want to move it to zig. I tried a few times to port the final optimizations over, but kept getting perf regressions. I think the fine details of the dict impl may be a tiny bit low level to happily map to roc, but maybe a few more builtins or simd stuff could fix that....not fully sure.

view this post on Zulip Isaac Van Doren (Oct 21 2023 at 01:42):

Is the plan to remove Nat from the language entirely or just to stop using it internally?

view this post on Zulip Brendan Hansknecht (Oct 21 2023 at 01:53):

entirely

view this post on Zulip Brendan Hansknecht (Oct 21 2023 at 01:53):

well, will be used internally still like for list indices

view this post on Zulip Isaac Van Doren (Oct 21 2023 at 01:53):

Interesting, what's the motivation for that?

view this post on Zulip Brendan Hansknecht (Oct 21 2023 at 01:53):

It is one of the only target specific parts of the language

view this post on Zulip Brendan Hansknecht (Oct 21 2023 at 01:54):

Without it, theoretically all roc code (aside from when panics happen) will do the exact same thing no matter the target

view this post on Zulip Brendan Hansknecht (Oct 21 2023 at 01:54):

This means you can just run pure roc tests once instead of once per platform

view this post on Zulip Richard Feldman (Oct 21 2023 at 01:55):

previously discussed at https://roc.zulipchat.com/#narrow/stream/304641-ideas/topic/replace.20Nat

view this post on Zulip Brendan Hansknecht (Oct 21 2023 at 01:55):

There were others too

view this post on Zulip Brendan Hansknecht (Oct 21 2023 at 01:55):

:point_up: yeah, that has more

view this post on Zulip Isaac Van Doren (Oct 21 2023 at 02:01):

Ah yeah that sounds like a good change, cool!

view this post on Zulip Richard Feldman (Oct 21 2023 at 02:36):

anyone know why decoding tuples uses Nat? @Ayaz Hafiz maybe?

view this post on Zulip Brendan Hansknecht (Oct 21 2023 at 02:40):

looks like index into the tuple

view this post on Zulip Ayaz Hafiz (Oct 21 2023 at 03:05):

yep

view this post on Zulip Ayaz Hafiz (Oct 21 2023 at 03:06):

the choice of integer type there doesn't matter

view this post on Zulip Richard Feldman (Oct 21 2023 at 03:09):

nice, thanks!

view this post on Zulip Richard Feldman (Oct 21 2023 at 03:31):

got it compiling, but still plenty to do: https://github.com/roc-lang/roc/pull/5923


Last updated: Jul 06 2025 at 12:14 UTC