Skip to content

Commit

Permalink
improve code generate for list pattern matching on js
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri authored and lpil committed Feb 21, 2025
1 parent 504f20b commit d292718
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
be byte aligned, and the `bits` segment type is now supported in patterns.
([Richard Viney](https://github.com/richard-viney))

- The code generated for list pattern matching on the JavaScript target is now
more efficient. Gleam code that relies heavily on list pattern matching can
now be up to twice as fast.
([yoshi~](https://github.com/yoshi-monster))

### Build tool

- The build tool now supports Git dependencies. For example:
Expand Down
24 changes: 12 additions & 12 deletions compiler-core/templates/prelude.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,27 @@ export class List {

// @internal
atLeastLength(desired) {
for (let _ of this) {
if (desired <= 0) return true;
desired--;
}
return desired <= 0;
let current = this;
while (desired-- > 0 && current) current = current.tail;
return current !== undefined;
}

// @internal
hasLength(desired) {
for (let _ of this) {
if (desired <= 0) return false;
desired--;
}
return desired === 0;
let current = this;
while (desired-- > 0 && current) current = current.tail;
return desired === -1 && current instanceof Empty;
}

// @internal
countLength() {
let current = this;
let length = 0;
for (let _ of this) length++;
return length;
while (current) {
current = current.tail;
length++;
}
return length - 1;
}
}

Expand Down

0 comments on commit d292718

Please sign in to comment.