sthir icon indicating copy to clipboard operation
sthir copied to clipboard

Evaluate the iife produced by `pa` macro when used with `p`

Open devanshj opened this issue 3 years ago • 2 comments

import { p, pa } from "@sthir/macro"

pa(x, p("!==", undefined))

Gets transformed to...

(t => t !== undefined)(x)

This is correct because pa could have even be used with some other predicate not just p...

pa(x, somePredicate)

But in cases where it's used with p the iife can be further evaluated....

x !== undefined

devanshj avatar Jan 18 '22 16:01 devanshj

We can't do the proposed if x has a call expression anywhere in the subtree, or even property accesses because of possibility of effectful getters and proxies. (That is to say an expression can be impure and evaluating the iife can evaluate the expression more than once). So given we'd have to do a lot more work to micro-optimize only a small set of cases, I think the current behavior is fine.

devanshj avatar May 02 '22 18:05 devanshj

I think the feature is attractive and we can evalute only when x is an IdentifierExpression (and maybe even MemberExpression). Or perhaps develop a heuristic that checks if an expression is most likely pure or not. Actually we can support impure expressions too if it's only required once in the predicate expression.

devanshj avatar Jul 26 '22 10:07 devanshj