Skip to content

lowering: equality-section list scans and match-consumed lookups - #93

Merged
simontreanor merged 2 commits into
mainfrom
perf/stdlib-callsite-peepholes
Aug 30, 2026
Merged

lowering: equality-section list scans and match-consumed lookups#93
simontreanor merged 2 commits into
mainfrom
perf/stdlib-callsite-peepholes

Conversation

@simontreanor

Copy link
Copy Markdown
Owner

Two call-site peepholes found on the Super Scrabble move generator, both extending the DESIGN.md §5.2 lever (inlining fully applied stdlib members).

Equality predicates (#86)

A fully applied List.findIndex / List.find / List.exists whose predicate is the section (==) x (or the lambda fun y -> x == y / fun y -> y == x with y not free in x) now lowers to the C-level scan Python answers fastest:

def idx(l, rack):
    return _pf_index_of(l, rack)     # try: return Some(xs.index(x)) except ValueError: return None_()
def fd(l, rack):
    return _pf_find_eq(l, rack)      # Some(xs[xs.index(x)]): the element found, as the helper returns it
def ex(l, rack):
    return l in rack

x is evaluated once, before xs, the argument order of the helper call it replaces. Any other predicate, and any partial application (List.findIndex ((==) l) as a value), keeps the generic _pf_find_index scan.

Match-consumed lookups (#88)

A match whose scrutinee is a fully applied Map.tryFind k m, List.head xs, or List.get i xs, with exactly the unguarded arms Some v / None (v a name, _, or a tuple of those), lowers to the if a person would write, in return and value position alike:

def allowsLetter(checks, rc, l):
    if rc in checks:
        s = checks[rc]
        return l in s
    else:
        return True

No Option is constructed, so a program whose only Option was this one emits no Some/None_ classes at all. Operands are bound to temps unless they are names or literals (evaluated once); List.get keeps the helper's exact bounds test 0 <= i < len(xs). A refutable payload, a guard, extra arms, or a non-lookup scrutinee fall through to the ordinary match.

Tests

tests/compile.rs: snapshot tests for every rewrite, the partial-application and non-equality cases that must keep the helper, the non-atomic operand that hoists to a temp, value-position match inside an expression, and e2e runs against the helper semantics (falsy present values ""/[], negative index out of range, 0.0 vs -0.0 returning the element found).

Docs: DESIGN.md §5.2 and INTERNALS.md (lowering passes).

Closes #86
Closes #88

List.findIndex/find/exists with an equality-section predicate route to
list.index / in (C-level scans) instead of a Python-level scan driving a
lambda per element. A match whose scrutinee is a fully applied
Map.tryFind, List.head, or List.get and whose arms are exactly Some v /
None lowers to the if a person would write, with no Option in between.

Extends the DESIGN.md 5.2 lever; mechanics in INTERNALS.md.
simontreanor added a commit that referenced this pull request Aug 30, 2026
…after #90, #91 and #93

Resolves the overlap between the lookup peephole (#93) and the Option/Result
ladder (#94): both hooks run in the match lowering, lookup first since it
needs no Option at all; the two new list helpers return the _None_ singleton;
tests whose scrutinee both passes rewrote now expect the combined output.
@simontreanor
simontreanor merged commit c1b7530 into main Aug 30, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant