-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
make private[this] if possible #6656
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In terms of the actual diff, I don't see any problems - it looks straightforward and unlikely to break anything to me. I'd be interested to hear what some of the core team think about always using private[this]
in general though.
FYI, there is a similar pull request in Dotty last year. |
This seems to be an optimisation that the compiler can perform automatically Private fields can only be accessed by companions, and companions are required to be in the same compilation unit, so the compiler is aware if there is an accessor required. The same optimisation can be applied to final vars, or effectively final vars (vars that are not declared as final in the code, but cannot be overridden, as they are in object, sealing etc.) There is also interplay with the inliner and gitter, so we will need to have a performance run to ensure/fix any performance degradation, or (hopefully) claim the benefit. Internally (in my day job) we have discussed making a scalafix rule to do this. Didn't get further than discussing it though 😞 |
I have long wanted As an optimization, it breaks reflective access. |
@som-snytt where is the definition of the reflexive it breaks? I agree that it changes the methods generated(but that is sort of the point), but other patterns also generate methods for private[this] fields in some circumstances, or don't generate fields for some declared vals, etc |
@mkeskells I just meant that a tool using reflection expecting an accessor will break. Dealing with fields introduces the funky space in the name, so filtering fields for those not the target of an accessor, etc, would make me sympathetic to a tool operating on accessors.
I think special accessors as implementation details are name-mangled? |
@som-snytt my confusion grows. Why is there a 'funky space' apart from just trying to confuse those that forgot to check for that? I dont think this affects the discussion, but TIL :-) My understanding is that the reflection information is derived from the pickled form stored in the scala attribute of the top level class, so this scala reflection view is unrelated to the java methods and fields that are generated or not. I am proposing that the the emitted class makes the optimisation, not that the scala reflexive view is changed Not that I understand the result below (another confusion - why final is treated differently) but IMO the scala view and java view of the reflected class are very different. Is i2 optimized away completely? As these are all unused constants, and not visible, then maybe it should generate no java fields or methods!! Anyway back to the current views of the class from java and scala
|
Yes, |
@som-snytt what I meant is that a
|
0dafe20
to
a0f04e4
Compare
private (not private[this]) val and var generate accessor methods. This change reduce class file size
@@ -22,7 +22,7 @@ final class MatchError(@transient obj: Any) extends RuntimeException { | |||
/** There's no reason we need to call toString eagerly, | |||
* so defer it until getMessage is called or object is serialized | |||
*/ | |||
private lazy val objString = { | |||
private[this] lazy val objString = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For lazy vals it doesn't change anything in the bytecode, but we can keep them in anyway.
private
(notprivate[this]
)val
andvar
generate accessor methods.This change reduce class file size