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

Fix #1435: Cache instances of j.l.Class #1894

Conversation

WojciechMazur
Copy link
Contributor

Resolves #1435

This PR moves allocation of java.lang.Class instances to linker phase and introduces caching of them.
In previous implementation Class instances were created by compiler backend, which lead to multiple instances of this type in runtime and made impossible to cache them.
Current implementation introduces new Val literal type Val.ClassOf(n: Global) which replaces allocations from previous implementation. At lowering phase this literals are transformed into Object.getClass calls with argument of RTTI object (as previous). RTTI type was also extended to store reference of associated with it java.lang.Class. Inside Object.getClass it's checked whether RTTI class pointer is not empty and instantiated in otherwise. Newly created instance of j.l.Class is added to registry in order to prevent GC from collecting it.

Immix/Commix implementation was fixed to handle new RTTI layout.

@WojciechMazur
Copy link
Contributor Author

WojciechMazur commented Sep 14, 2020

Looks like this change introduced significant regression in performance. CI jobs takes in debug mode around 15-25% longer and in release-fast it take ~50% more time. Due to this 3 last CI jobs has timed out (there's a limit of 50 minutes). I'll take a look at this

@ekrich
Copy link
Member

ekrich commented Sep 14, 2020

Do you think it might be helpful to have some diagrams showing the runtime layout? Isn't one of the big advantages over the JVM is that the JVM carries a bunch of overhead in each object? I don't want to slow down the release maybe a follow on issue if you agree.

tools/src/main/scala/scala/scalanative/codegen/Lower.scala Outdated Show resolved Hide resolved
unit-tests/src/test/scala/java/lang/ClassSuite.scala Outdated Show resolved Hide resolved
unit-tests/src/test/scala/java/lang/ClassSuite.scala Outdated Show resolved Hide resolved
@WojciechMazur WojciechMazur force-pushed the feature/1435-cache_instances_of_j.l.Class branch from fb06602 to 54398eb Compare October 7, 2020 17:46
@WojciechMazur WojciechMazur force-pushed the feature/1435-cache_instances_of_j.l.Class branch from 54398eb to 2bcc818 Compare October 9, 2020 10:48
@WojciechMazur WojciechMazur force-pushed the feature/1435-cache_instances_of_j.l.Class branch from 2bcc818 to a6d1eea Compare October 13, 2020 16:23
@sjrd
Copy link
Collaborator

sjrd commented Oct 14, 2020

This probably needs a rebase on top of master, now that #1946 has been merged.

@WojciechMazur WojciechMazur force-pushed the feature/1435-cache_instances_of_j.l.Class branch 2 times, most recently from cdfb0ab to 640b5d1 Compare October 14, 2020 17:58
@WojciechMazur WojciechMazur force-pushed the feature/1435-cache_instances_of_j.l.Class branch from 640b5d1 to 1e9c825 Compare October 22, 2020 10:17
Copy link
Collaborator

@sjrd sjrd left a comment

Choose a reason for hiding this comment

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

Looks good. I only have two very small comments.

@WojciechMazur WojciechMazur force-pushed the feature/1435-cache_instances_of_j.l.Class branch from 1e9c825 to 4937ee3 Compare November 12, 2020 18:31
Copy link
Collaborator

@sjrd sjrd left a comment

Choose a reason for hiding this comment

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

@WojciechMazur could you rebase this on top of latest master, just to make sure it still works with the latest CI setup?

@sjrd sjrd changed the title Feature/1435 cache instances of j.l.Class Fix #1435: Cache instances of j.l.Class Nov 30, 2020
@WojciechMazur WojciechMazur force-pushed the feature/1435-cache_instances_of_j.l.Class branch from 4937ee3 to 6a49537 Compare November 30, 2020 11:44
@WojciechMazur WojciechMazur merged commit a87b932 into scala-native:master Nov 30, 2020
@WojciechMazur WojciechMazur deleted the feature/1435-cache_instances_of_j.l.Class branch February 15, 2021 09:08
ekrich pushed a commit to ekrich/scala-native that referenced this pull request May 21, 2021
* 1435 - Move j.l.Class allocations to linker/runtime. Introduce caching of j.l.Classes

* 1435 - Compare _Class instances using their rawPtr instead of Rt.Type ptr

* 1435 - Updated immix/commix RTTI structures to contain additional class pointer

* 1435 - Transform Vals in Lower with nir.Buffer in order to support Val.ClassOf -> Object.getClass conversion

* Added classOf / Object.getClass tests

* Use loadObject instead of loadrawPtr in Object.getClass

* Workaround Object.getClass when clsPtr does not contain j.l.Class instance

* Create registry for instances of j.l.Class to prevent GC from collecting them

* Remove commented code

* Lower.genMethodLookup uses original value to test targets (due to type conversion in genVal for Val.String)

* Replace ClassInstancesRegistry Array with UnrolledBuffer to store class instances

* Add Val.ClassOf support to NIR text parser

* Add Object.getClass to Lower dependencies

* Revert "Replace ClassInstancesRegistry Array with UnrolledBuffer to store class instances"

This reverts commit 58cc6c6.

* Move ClassInstancesRegistry to seperate file

* Styling and access fixes to ClassInstancesRegistry. Use System.arraycopy instead of Array.copy

* Move class caching and Rtti -> Class lookup to scalanative.runtime

* After rebase fixes

* Use same calling and styling convention for runtime.toClass as other Rt methods

* After rebase fix

* After rebase fix: Port new ClassSuite tests to JUnit

* Comments styling fix

* Simplify Class.is method

* Fix comment
WojciechMazur added a commit to WojciechMazur/scala-native that referenced this pull request Aug 25, 2021
* 1435 - Move j.l.Class allocations to linker/runtime. Introduce caching of j.l.Classes

* 1435 - Compare _Class instances using their rawPtr instead of Rt.Type ptr

* 1435 - Updated immix/commix RTTI structures to contain additional class pointer

* 1435 - Transform Vals in Lower with nir.Buffer in order to support Val.ClassOf -> Object.getClass conversion

* Added classOf / Object.getClass tests

* Use loadObject instead of loadrawPtr in Object.getClass

* Workaround Object.getClass when clsPtr does not contain j.l.Class instance

* Create registry for instances of j.l.Class to prevent GC from collecting them

* Remove commented code

* Lower.genMethodLookup uses original value to test targets (due to type conversion in genVal for Val.String)

* Replace ClassInstancesRegistry Array with UnrolledBuffer to store class instances

* Add Val.ClassOf support to NIR text parser

* Add Object.getClass to Lower dependencies

* Revert "Replace ClassInstancesRegistry Array with UnrolledBuffer to store class instances"

This reverts commit 58cc6c6.

* Move ClassInstancesRegistry to seperate file

* Styling and access fixes to ClassInstancesRegistry. Use System.arraycopy instead of Array.copy

* Move class caching and Rtti -> Class lookup to scalanative.runtime

* After rebase fixes

* Use same calling and styling convention for runtime.toClass as other Rt methods

* After rebase fix

* After rebase fix: Port new ClassSuite tests to JUnit

* Comments styling fix

* Simplify Class.is method

* Fix comment
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.

Cache instances of java.lang.Class
3 participants