Skip to content

Make lazy vals thread-safe by default #6579

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

Merged
merged 4 commits into from
Jun 6, 2019

Conversation

smarter
Copy link
Member

@smarter smarter commented May 28, 2019

Previously this required using @volatile but that would not be
consistent with Scala 2. The old behavior of non-volatile lazy vals can
be recovered by using the newly-introduced @threadUnsafe.

@smarter smarter force-pushed the volatile-by-default branch 4 times, most recently from 9f11894 to 54ea912 Compare May 28, 2019 17:40
@smarter
Copy link
Member Author

smarter commented May 28, 2019

test performance please

@dottybot
Copy link
Member

performance test scheduled: 2 job(s) in queue, 1 running.

@dottybot
Copy link
Member

Performance test finished successfully:

Visit http://dotty-bench.epfl.ch/6579/ to see the changes.

Benchmarks is based on merging with master (6ede814)

@smarter smarter force-pushed the volatile-by-default branch from 54ea912 to de46982 Compare May 29, 2019 12:19
@smarter smarter marked this pull request as ready for review May 29, 2019 12:20
@smarter smarter force-pushed the volatile-by-default branch from de46982 to 0b46b97 Compare May 29, 2019 17:40
@odersky
Copy link
Contributor

odersky commented May 29, 2019

test performance please

@dottybot
Copy link
Member

performance test scheduled: 3 job(s) in queue, 1 running.

@dottybot
Copy link
Member

Performance test finished successfully:

Visit http://dotty-bench.epfl.ch/6579/ to see the changes.

Benchmarks is based on merging with master (2984e57)

@smarter smarter force-pushed the volatile-by-default branch from 0b46b97 to 322487b Compare May 30, 2019 12:55
@odersky
Copy link
Contributor

odersky commented May 30, 2019

I am still concerned that the compile time trend here is upwards. If lazy vals in Definitions are not the problem, where else would they be one?

@odersky
Copy link
Contributor

odersky commented May 30, 2019

My first suspect would be

    lazy val typeParams: List[LambdaParam] =
      paramNames.indices.toList.map(new LambdaParam(this, _))

in Types.scala. Then the lazy vals in Applications.scala and Implicits.scala, as well as the lazy vals defined in the Backend. Maybe make all of these @ThreadUnsafe as well?

Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

The changes look good to me, but let's try to tweak performance a bit more before merging.

@odersky odersky assigned smarter and unassigned odersky May 30, 2019
@odersky
Copy link
Contributor

odersky commented May 31, 2019

test performance please

@dottybot
Copy link
Member

performance test scheduled: 1 job(s) in queue, 0 running.

@dottybot
Copy link
Member

Performance test finished successfully:

Visit http://dotty-bench.epfl.ch/6579/ to see the changes.

Benchmarks is based on merging with master (c165638)

@nafg
Copy link

nafg commented Jun 2, 2019

It seems unfortunate to allocate a general-sounding name like threadUnsafe exclusively for use on lazy vals. What about something more specific, like simpleLazy or unsafeLazy?

@smarter
Copy link
Member Author

smarter commented Jun 2, 2019

Nothing prevents us from reusing it to work on other things than lazy vals. It's documented as only working on lazy vals since it's the only place where it does something currently.

@nafg
Copy link

nafg commented Jun 3, 2019 via email

@smarter
Copy link
Member Author

smarter commented Jun 3, 2019

Nope

@odersky odersky mentioned this pull request Jun 3, 2019
6 tasks
@smarter
Copy link
Member Author

smarter commented Jun 3, 2019

@odersky OK to merge ?

@odersky
Copy link
Contributor

odersky commented Jun 4, 2019

@smarter It's OK to merge. I still would have liked to go to the bottom of the performance regressions. E.g. Re2 is clearly worse. I wonder where that could come from. But it's not urgent, we can do it later

smarter added 4 commits June 6, 2019 13:29
Otherwise we might end up trying to initialize UnitType recursively,
which happens to work with the current implementation of non-volatile
lazy vals, but won't work after the next commit.
Previously this required using `@volatile` but that would not be
consistent with Scala 2. The old behavior of non-volatile lazy vals can
be recovered by using the newly-introduced `@threadUnsafe`.
There does not seem to be a noticeable performance difference compared
to  thread-safe lazy vals but there is a noticeable impact on bytecode size
(Definitions.class goes from 210 to 168K).
@smarter smarter force-pushed the volatile-by-default branch from 989c95d to a73b830 Compare June 6, 2019 11:39
@smarter smarter merged commit 87de2d9 into scala:master Jun 6, 2019
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.

5 participants