-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add optimization to reduce extra iterations of the safe init checker. #17057
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
Conversation
@@ -99,7 +105,9 @@ class Cache[Config, Res]: | |||
*/ | |||
def cachedEval(config: Config, expr: Tree, cacheResult: Boolean, default: Res)(eval: Tree => Res): Res = | |||
this.get(config, expr) match | |||
case Some(value) => value | |||
case Some(value) => | |||
cacheUsed = true |
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 the init checker, if we read from global cache, we can think it's still not used. Maybe move it to the method get
:
def get(config: Config, expr: Tree): Option[Res] =
val res = current.get(config, expr)
cacheUsed = cacheUsed || res.nonEmpty
res
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.
Otherwise, LGTM
/** Whether any value in the cache was accessed after being added. | ||
* If no cached values are used after they are added for the first time | ||
* then another iteration of analysis is not needed. | ||
*/ |
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.
Consider make it more precise in the comment: Whether any value in the output cache (this.current
).
Minor: the comment *
is not aligned.
If the cache is never accessed on a certain iteration of safe initializing checking for a class, then another iteration is not needed. Implementing this optimization and compiling Dotty, we observe the following improvements:
From 4158 total iterations to 4004.
From 460 extra iterations to 306.
Running the initialization tests in
tests/init
we have the following improvements:From 790 total iterations to 708.
From 139 extra iterations to 57.