Skip to content

Elide capturing of closures in functions and objects - #1869

Draft
bioball wants to merge 1 commit into
apple:mainfrom
bioball:optimize-lambdas
Draft

bioball wants to merge 1 commit into
apple:mainfrom
bioball:optimize-lambdas

Conversation

@bioball

@bioball bioball commented Sep 21, 2026

Copy link
Copy Markdown
Member

This implements an optimization where functions and objects don't retain a closure if they don't need it.

During parse time (inside AstBuilder), we determine whether a function or an object needs its enclosing scope.

This then propagates to the truffle node that creates the runtime objects.
If an object does not need to capture its closure, its enclosing frame does not get materialized.

@bioball
bioball marked this pull request as draft September 21, 2026 20:59

@HT154 HT154 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Very slick! Any idea what, if any, real world perf impact this has?

Comment thread pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java
@bioball

bioball commented Sep 22, 2026 •

Copy link
Copy Markdown
Member Author

Very slick! Any idea what, if any, real world perf impact this has?

Right now, I'm not seeing any performance impact when testing against existing codebases, which I'm really surprised by. I ran a test against a project that gave me about an 83% capture rate (17% of objects skipped capturing their closure), and the eval time is basically identical (tested using both Graal JDK and native image).

Not really sure why, this change should help the truffle inline much more code. But, I still have a lot of PE fixes incoming, so maybe this change won't affect anything until that's applied.

This implements an optimization where functions and objects don't
retain a closure if they don't need it.

During parse time (inside AstBuilder), we determine whether a function
or an object needs its enclosing scope.

This then propagates to the truffle node that creates the runtime
objects.
If an object does not need to capture its closure, its enclosing frame
does not get materialized.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants