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
Avoid redundant field in TermName, reducing size 40->32 bytes #8177
Conversation
Before: ``` ➜ scala git:(topic/name-waste) ✗ java -Djdk.attach.allowAttachSelf=true -cp $(coursier fetch -q -p 'org.openjdk.jol:jol-cli:0.9') org.openjdk.jol.Main internals -cp $(scala-classpath $(scala-ref-version 2.13.x)) 'scala.reflect.internal.Names$TermName' Failed to find matching constructor, falling back to class-only introspection. scala.reflect.internal.Names$TermName object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N/A 12 4 scala.reflect.api.Names NameApi.$outer N/A 16 4 int Name.index N/A 20 4 int Name.len N/A 24 4 java.lang.String Name.cachedString N/A 28 4 scala.reflect.internal.Names.TermName TermName.next N/A 32 4 java.lang.String TermName.cachedString N/A 36 4 (loss due to the next object alignment) Instance size: 40 bytes Space losses: 0 bytes internal + 4 bytes external = 4 bytes total ``` After: ``` ➜ scala git:(topic/name-waste) ✗ java -Djdk.attach.allowAttachSelf=true -cp $(coursier fetch -q -p 'org.openjdk.jol:jol-cli:0.9') org.openjdk.jol.Main internals -cp build/quick/classes/reflect 'scala.reflect.internal.Names$TermName' Failed to find matching constructor, falling back to class-only introspection. scala.reflect.internal.Names$TermName object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N/A 12 4 scala.reflect.api.Names NameApi.$outer N/A 16 4 int Name.index N/A 20 4 int Name.len N/A 24 4 java.lang.String Name.cachedString N/A 28 4 scala.reflect.internal.Names.TermName TermName.next N/A Instance size: 32 bytes Space losses: 0 bytes internal + 0 bytes external = 0 bytes total ``` Exposing `Name.cachedString` as a protected val makes it eligible for the parameter aliasing layout optimization.
7a42269
to
9f6f54f
Compare
Trivial change, merging without review. |
Where can I read up more about this? "parameter aliasing layout optimization" doesn't google me anything strictly relevant. |
The compiler detects if a constructor parameter field can be elided because there is a field in a superclass that is known to store the same value. The implementation is spread out between: scala/src/compiler/scala/tools/nsc/typechecker/Typers.scala Lines 54 to 59 in 6b4d32c
scala/src/compiler/scala/tools/nsc/typechecker/Typers.scala Lines 2192 to 2218 in 6b4d32c
scala/src/compiler/scala/tools/nsc/typechecker/Typers.scala Lines 6137 to 6165 in 6b4d32c
scala/src/compiler/scala/tools/nsc/typechecker/SuperAccessors.scala Lines 32 to 37 in 6b4d32c
scala/src/compiler/scala/tools/nsc/typechecker/SuperAccessors.scala Lines 325 to 333 in 6b4d32c
|
Ah right. I thought this was a JIT thing (silly, in retrospect). Thanks! |
Before:
After:
Exposing
Name.cachedString
as a protected val makes it eligible forthe parameter aliasing layout optimization.