-
-
Notifications
You must be signed in to change notification settings - Fork 788
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
Unaligned bit arrays on the JavaScript target #3946
base: main
Are you sure you want to change the base?
Unaligned bit arrays on the JavaScript target #3946
Conversation
2496b81
to
bb69dd9
Compare
b347126
to
9aa5446
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.
Wow, what a fantastic bit of work! Thank you!
I've not digested these changes properly yet, but I have some initial questions from my first review.
There's a lot of public functions in the API of the bit array class now, but generally the aim is to have none. What is the motivation for having these?
Similar for the deprecated functions, we can remove them.
There's quite a lot of code in the class. Could we move them to free-standing functions and have the generated code only use them if absolutely necessary? That would help with JavaScript bundlers performing dead code elimination.
Some existing tests have been removed or changed, why is this? New features shouldn't alter existing tests, it makes it harder to review and to prevent regressions.
Thanks again!
544c327
to
0f6fbdb
Compare
Thanks for taking a look!
These have been reduced down to the following:
I've removed all except the
Done.
The diff on the tests looked a bit more complex in places than it actually was so I've moved some things around to make it easier to digest. Some existing tests did need to be tweaked or removed, e.g. those that were testing for compilation errors if unaligned bit arrays were used on the JS target. |
9c7c0dc
to
5561409
Compare
da179df
to
0847df6
Compare
40047c3
to
945ed06
Compare
945ed06
to
7651919
Compare
This PR has been updated to make slicing bit arrays an O(1) operation under all circumstances, matching Erlang's performance characteristics. The The main write-up has been updated for this change. |
4988b97
to
c23f5a4
Compare
71a13d5
to
9928d33
Compare
9928d33
to
30be6f1
Compare
Summary 📘
This PR adds support for unaligned bit arrays on the JavaScript target.
In expressions:
bits
segments:In patterns:
int
segments:bits
segments:There is a compiler warning if the above features are used when
gleam.toml
specifies a version < v1.8.0.Implementation Details 🛠️
BitArray
class in the prelude now hasbitSize
,byteSize
,bitOffset
, andrawBuffer
properties.bitSize
,byteSize
,bitOffset
,rawBuffer
,byteAt(index)
.get buffer()
,get length()
. Using these emits a deprecation warning at runtime, and throws an exception if the bit array is unaligned (i.e.bitOffset
isn't zero orbitSize
isn't a multiple of 8).// @ts-check
to the top of the file.Implications for
@external
JavaScript code 🌍BitArray.length
andBitArray.buffer
.Implications for
gleam/stdlib
🤝gleam/stdlib
ready, mostly affectinggleam/bit_array
. It can only be merged once this PR goes in as its tests won't run on stable Gleam. It may be necessary to run the new stdlib tests on nightly for a short period, with them segregated into their own file so they can be included/excluded depending on the active Gleam version. I'll sort that out once this PR makes it through review.Testing 🧪
There's certainly some complexity and tricky bitwise operations here, mostly in the JavaScript prelude. The following has been done to ensure correctness:
language_tests.gleam
, andtest/javascript_prelude
.✨✨✨