-
-
Notifications
You must be signed in to change notification settings - Fork 759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compile-time evaluate constant BitArray int segments on JavaScript #3798
Conversation
4ce3ed8
to
4c7aaa0
Compare
c00767f
to
c1593c6
Compare
c1593c6
to
158ad3d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oooh very nice!!!
I've left some minor notes inline.
When you are ready for a review please un-draft this PR. Thank you!
<<4000:16>> | ||
<<80_000:16>> | ||
<<-80_000:16>> | ||
<<-1:48>> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only test one thing per test please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
1dec85b
to
453dc1b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic! Thank you!
Seeing as this is tricky runtime behaviour could you add some integrations tests please 🙏
gleam/test/language/test/language_test.gleam
Line 954 in 31966a0
fn bit_array_tests() -> List(Test) { |
I've updated the existing language tests so each test is run both as a constant value that is compile-time evaluated, and as a dynamic value that is runtime evaluated. |
80bc4f3
to
4a50481
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing work again!!! 💜
Implements #3724.
Details
Int
s in bit array expressions with a known value and size at compile time are now evaluated to bytes instead of usingsizedInt()
at runtime.Int
s in bit array patterns with a known value and size at compile time are now evaluated then matched byte-wise instead of usingintFromSlice()
at runtime.The above is currently limited to
Int
sizes <= 48 bits in order to be under the JS safe integer limit, because otherwise:<<0:8192>>
would evaluate to[0,0,0,0, ...]
to 1024 entries, which seems undesirable (though I'm not sure if such code is commonly encountered).let assert <<0xFFFFFFFFFFFFFFFF:64>> == <<255, 255, 255, 255, 255, 255, 255, 255>>
would change behaviour, albeit in a correct direction that would match Erlang. This may be desirable, but I've taken the conservative route initially.Performance
Evaluating
<<-1:32>>
: 10-11x faster.Evaluating a large lookup table of 16-bit integers: 26x faster.
Pattern matching a 32-bit signed integer: 2.7-7.1x faster, depending on how early a non-matching byte occurs.
Generated JavaScript code size is up to ~70% smaller. There are a few cases when using 40-bit and 48-bit wide integers that can see code size increases of ~20% with certain values, but the average case still sees a code size reduction on the wider integers.