Skip to content

Commit de9bf2f

Browse files
committed
perf(js): convert flat arrays to lists without tree slices
1 parent e5527d5 commit de9bf2f

3 files changed

Lines changed: 58 additions & 6 deletions

File tree

‎bend2/comp.ts‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type Name = Bend.Name;
5454
type File = {
5555
book: Bend.Book;
5656
js: boolean;
57+
to_lists: Set<Name> | null;
5758
bangs: Set<Name>;
5859
sites: Map<Name, number>;
5960
hot: Set<Name>;
@@ -245,6 +246,7 @@ const OPERATIONS: Record<string, Intr> = Object.setPrototypeOf({
245246
string_append: { JS: "($0 + $1)" },
246247
string_contains: { JS: "string_contains($0, $1)" },
247248
string_length: { JS: "[...str_out($0)].length" },
249+
array_to_list: { JS: "array_to_list($0)" },
248250
...Object.fromEntries(Object.entries({
249251
new: "array_new($0, $1)", set: "($0[$1 % $0.length] = $2, $0)",
250252
get: "{$: \"Tuple\", fst: $0, snd: $0[$1 % $0.length]}",
@@ -842,8 +844,15 @@ function dom_live([q]: Dom): boolean {
842844

843845
function intr_of(k: Name, js = false): Intr | undefined {
844846
const tld = FL.book.tlds[k];
845-
const it = tld?.$ === "Def" && tld.i === undefined && tld.b
846-
? OPERATIONS[op_name(k)] : undefined;
847+
if (tld?.$ !== "Def" || tld.i !== undefined) {
848+
return undefined;
849+
}
850+
// Specializations of a Base template do not carry its Base flag.
851+
const list = FL.to_lists !== null && FL.to_lists.has(k);
852+
if (!tld.b && !list) {
853+
return undefined;
854+
}
855+
const it = OPERATIONS[list ? "array_to_list" : op_name(k)];
847856
return it && (js || it.C !== undefined) && (!FL.js || it.JS)
848857
? it : undefined;
849858
}
@@ -1416,9 +1425,13 @@ export function io_run(book: Bend.Book, args: string[]): number {
14161425

14171426
function file_new(book: Bend.Book, js: boolean): File {
14181427
PROBES.length = 1;
1428+
const lists = js && book.tlds["Array.to_list"]?.b === true
1429+
? book.tmps["Array.to_list"] : undefined;
14191430
return {
14201431
book,
14211432
js,
1433+
to_lists: lists === undefined || lists.size === 0
1434+
? null : new Set(lists.values()),
14221435
bangs: new Set(),
14231436
sites: new Map(),
14241437
hot: new Set(),
@@ -6057,6 +6070,14 @@ function array_new(d, v) {
60576070
return Array(2 ** d).fill(v);
60586071
}
60596072
6073+
function array_to_list(a) {
6074+
let xs = {$: "Nil"};
6075+
for (let i = a.length - 1; i >= 0; i -= 1) {
6076+
xs = {$: "Con", head: a[i], tail: xs};
6077+
}
6078+
return xs;
6079+
}
6080+
60606081
function array_node(a, b) {
60616082
if (a.length !== b.length) {
60626083
throw "bend: ${ERRS[2]}";

‎guide/GUIDE.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ write followed by another statement re-binds its array: `a[5] <- 42` on its own
190190
line is `a = a[5] <- 42`. As the last statement it is the written array. The
191191
slot count after `*` is a power of two; `[0 : U32^3n]` names the depth instead.
192192

193-
The `a[i]` sugar assumes `Array<U32>`. For other element types, call
194-
`Array.get` (`Data` elements; else `Array.swap`) and `Array.set` directly, and
195-
`Array.clone` when you need two copies. Read Bend's Base for reference. This
196-
will be generalized soon!
193+
`a[i]` assumes `Array<U32>`. Otherwise use `Array.get` (`Data` elements;
194+
else `Array.swap`), `Array.set`, and `Array.clone` for copies.
195+
`Array.to_list` consumes in index order; JS walks flat slots.
196+
Generic indexing sugar is planned; see Base for reference.
197197

198198
### Quantities
199199

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Base
2+
3+
type Item is Data:
4+
Item{value: U32}
5+
6+
def digits(xs: List<U32>, acc: U32) -> U32:
7+
match xs:
8+
case Nil{}:
9+
acc
10+
case Con{head, tail}:
11+
digits(tail, U32.add(U32.mul(acc, 10), head))
12+
13+
def item_digits(xs: List<Item>, acc: U32) -> U32:
14+
match xs:
15+
case Nil{}:
16+
acc
17+
case Con{Item{value}, tail}:
18+
item_digits(tail, U32.add(U32.mul(acc, 10), value))
19+
20+
def numbers(a: Array<U32>) -> List<U32>:
21+
f = Array.to_list(~U32)
22+
f(a)
23+
24+
def main() -> Bool:
25+
Bool.and(
26+
U32.is_eq(digits(numbers(ANode{ANode{ALeaf{1}, ALeaf{2}},
27+
ANode{ALeaf{3}, ALeaf{4}}}), 0), 1234),
28+
U32.is_eq(item_digits(Array.to_list(~Item,
29+
ANode{ALeaf{Item{5}}, ALeaf{Item{7}}}), 0), 57))
30+
31+
#|True{}

0 commit comments

Comments
 (0)