Skip to content

Optimize fun_apply - #16148

Merged
josevalim merged 6 commits into
mainfrom
jv-fun-apply-fast
Nov 21, 2026
Merged

josevalim merged 6 commits into
mainfrom
jv-fun-apply-fast

Conversation

@josevalim

Copy link
Copy Markdown
Member

Commits can be reviewed individually.

When applying an intersection of arrows, an arrow whose domain is
disjoint from the input does not apply. Refining the return type with
it only computes a subset of the result obtained by skipping it, yet
each such arrow doubled the number of paths explored.
Compute the meets of all elements upfront when checking tuple
emptiness, so negative tuples disjoint on a later element are skipped
before splitting the line on earlier elements.
Check the disjointness of all fields upfront when checking map
emptiness, so negative maps disjoint on a later field are skipped
before splitting the line on earlier fields. Unlike tuples, meets are
computed lazily, only on the fields we split, as most map lines are
not empty and their checks finish early.
Instead of encoding the input of function application as a tuple,
keep it as a list of disjoint products of arguments. This is valid as
application distributes over unions. Subtracting an arrow's domain is
then done argument by argument, reusing the tuple meets, which tells
upfront if the arrow is disjoint and avoids tuple differences and
emptiness checks.
When refining the return type with an arrow, every result reachable
from that branch is a subset of the refined return. If it is already
contained in the accumulated result, the branch cannot add anything.
@josevalim
josevalim requested a review from gldubc November 18, 2026 15:50
Comment thread lib/elixir/lib/module/types/descr.ex Outdated
# - input: The input type being applied to the function
# - products: The input being applied to the function, as a list of disjoint
# products, each product being a list of non-empty static arguments.
# This is valid because application distributes over unions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
# This is valid because application distributes over unions

Incorrect comment. This is just an equivalent representation of the same set, no need for distribution.

@josevalim
josevalim merged commit cabe333 into main Nov 21, 2026
24 checks passed
@josevalim
josevalim deleted the jv-fun-apply-fast branch November 21, 2026 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants