Skip to content
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

Const enum variants no longer backed by functions #507

Merged
merged 1 commit into from
Nov 24, 2024

Conversation

kengorab
Copy link
Owner

Rather than having constant enum variants backed by a function call like how container enum variants work, store them as constant data values instead. Constant enum variants don't contain variables or data which could vary depending on construction, so this is a pretty straightforward (if minor) performance improvement.

The real main change hidden in this changeset is the solving of a sneaky and nefarious bug that's been hidden in here for a while - when storing Byte instances into a Pointer value using the intrinsic Pointer#store method, it would actually use a storel qbe instruction behind the scenes which would result in 4 bytes being written to the underlying memory. This had the evil side effect of overwriting the neighboring memory segment's value, most likely to be 0. This was most apparent in Array#toString which I had actually already noticed a while ago but never got around to actually digging in to figure out what was wrong. This solution was the result of many hours' work (unfortunately) so it's no wonder why I punted on it for so long. Oh well, it was fun.

Rather than having constant enum variants backed by a function call like
how container enum variants work, store them as constant `data` values
instead. Constant enum variants don't contain variables or data which
could vary depending on construction, so this is a pretty
straightforward (if minor) performance improvement.

The real main change hidden in this changeset is the solving of a sneaky
and nefarious bug that's been hidden in here for a while - when storing
Byte instances into a Pointer value using the intrinsic `Pointer#store`
method, it would actually use a `storel` qbe instruction behind the
scenes which would result in _4_ bytes being written to the underlying
memory. This had the evil side effect of overwriting the neighboring
memory segment's value, most likely to be `0`. This was most apparent in
`Array#toString` which I had actually already noticed a while ago but
never got around to actually digging in to figure out what was wrong.
This solution was the result of many hours' work (unfortunately) so it's
no wonder why I punted on it for so long. Oh well, it was fun.
@kengorab kengorab added bug Something isn't working enhancement New feature compiler labels Nov 24, 2024
func _callMalloc(self, sizeVal: Value, localName: String? = None): Result<Value, CompileError> {
val mem = try self._currentFn.block.buildCall(Callable.Function(self._malloc), [sizeVal], localName) else |e| return qbeError(e)

// val labelIsZero = self._currentFn.block.addLabel("malloc_is_zero")
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can uncomment this if I ever want to introduce this kind of runtime check.

// // Array#flatMap
// (() => {
// val arr = [1, 2, 3, 4]
// Array#flatMap
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty nice that this test passes, now that I finally addressed that nefarious bug

@kengorab kengorab merged commit 33e0518 into master Nov 24, 2024
1 check passed
@kengorab kengorab deleted the improve-constant-enum-expressions branch November 24, 2024 02:18
@kengorab
Copy link
Owner Author

Closes #506

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler enhancement New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant