Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vendor org.graalvm.collections #795

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

odenix
Copy link
Contributor

@odenix odenix commented Nov 10, 2024

Motivation:
Package org.graalvm.collections is used throughout the Pkl codebase. It is most heavily used in the Truffle interpreter, which requires putting most collection methods behind @TruffleBoundary. At the moment, this is done by wrapping collection methods with static methods declared in classes EconomicMaps and EconomicSets. However, static wrapper methods are inconvenient to use, decrease code readability, add some overhead, and are easy to forget about.

Changes:

  • vendor package org.graalvm.collections into org.pkl.core.collection
  • inline and remove EconomicMaps/Sets wrapper methods
  • replace usages of EconomicMaps.equals/hashCode with EconomicMapUtil.equals/hashCode
  • fix ProjectDeps.hashCode()

Result:
Cleaner, safer, and more efficient code.

Motivation:
Package org.graalvm.collections is used throughout the Pkl codebase.
It is most heavily used in the Truffle interpreter,
which requires putting most collection methods behind @TruffleBoundary.
At the moment, this is done by wrapping collection methods
with static methods declared in classes EconomicMaps and EconomicSets.
However, static wrapper methods are inconvenient to use,
decrease code readability, add some overhead, and are easy to forget about.

Changes:
- vendor package org.graalvm.collections into org.pkl.core.collection
  - org.graalvm.collections is licensed under:
    The Universal Permissive License (UPL), Version 1.0
  - vendored version:
    https://github.com/oracle/graal/tree/graal-23.0.3/sdk/src/org.graalvm.collections/src/org/graalvm/collections
  - add package-info.java with original license
  - annotate most public methods with @TruffleBoundary
  - add @nullable annotations
    - switching to JSpecify will enable more accurate nullability checks
  - remove prefix tree classes (not used in Pkl)
  - make no other code changes to simplify review/updates
- inline and remove EconomicMaps/Sets wrapper methods
- replace usages of EconomicMaps.equals/hashCode with EconomicMapUtil.equals/hashCode
- fix ProjectDeps.hashCode()

Result:
Cleaner, safer, and more efficient code.
@odenix odenix force-pushed the vendor-collections branch from a5f3237 to 7a12edd Compare November 14, 2024 01:18
@bioball
Copy link
Contributor

bioball commented Nov 20, 2024

I am not sure we want to do this. This makes it hard to upgrade the collections library, and I'm not sure how much of a performance gain we get out of this.

and are easy to forget about

Thankfully, our CI builds (on main) will catch these errors if they are forgotten.

Curious about @holzensp and @stackoverflow's thoughts

@odenix
Copy link
Contributor Author

odenix commented Nov 23, 2024

Owning the collection library(s) used by a Truffle language implementation makes a lot of sense:

  • it is desirable for performance reasons (can optimize for partial evaluation, etc.)
  • it avoids ugly wrapper code (invocations)

org.graalvm.collections is a tiny (essentially one class, namely EconomicMapImpl) and very mature library. It hasn't had significant changes in years, except for some recent additions (LockFreePool, etc.) that aren't used in Pkl.

Vendoring EconomicMap(Impl) feels natural to me, even just to improve code readability/writability.

@odenix
Copy link
Contributor Author

odenix commented Nov 28, 2024

I just stumbled upon the following conversation in the GraalVM Slack, where a Truffle committer argues for owning the collection library used in the Pkl interpreter. He's talking about Paguro, but it's the same thing, except that Paguro, or its future replacement, is way more challenging to own than EconomicMap(Impl).

Christian Humer Apr 5th at 7:36 AM
Looking at the Pkl types a bit. I think RrbTree could be a good candidate to make partial evaluatable. Atm it looks as if there all just boundaries, which hinders optimizations. Do you have any experience trying this?

yeah you need to manually profile and specialize for recursions and things like that. that typically involves owning the library you are using.

By the way, his latest announcement could be huge for Pkl performance, but is years away unless Pkl changes its versioning strategy:

Christian Humer 3:21 PM
At long last the Bytecode DSL was merged and will be shipped with 24.2 as experimental.

In short, the Bytecode DSL allows you to generate bytecode interpreters from an AST-like specification.
This makes building bytecode interpreters as simple probably even simpler than building AST interpreters in Truffle.
The generated interpreters automatically come with batteries included:
[...]

@stackoverflow
Copy link
Contributor

I wasn't too keen on this change initially. But the library is tiny and allows us to further improve on performance (not only partial evaluation).
Was discussing some other day with @holzensp exactly about EconomicMaps and how we could improve the performance of some specific use cases, if only we could patch the implementation.
So, I'm for it.

@holzensp
Copy link
Contributor

holzensp commented Dec 2, 2024

I also see the benefits (and the low risk, due to the stability of this library). It would require a further update to NOTICE.txt, btw.

@odenix
Copy link
Contributor Author

odenix commented Dec 2, 2024

It would require a further update to NOTICE.txt, btw.

done

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.

4 participants