Skip to content

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

Merged
merged 5 commits into from
Mar 13, 2023

Conversation

q-ata
Copy link
Contributor

@q-ata q-ata commented Mar 7, 2023

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.

@q-ata q-ata marked this pull request as ready for review March 7, 2023 06:06
@prolativ prolativ requested a review from anatoliykmetyuk March 7, 2023 11:29
@@ -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
Copy link
Contributor

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

Copy link
Contributor

@liufengyun liufengyun left a 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.
*/
Copy link
Contributor

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.

@olhotak olhotak merged commit 585bbef into scala:main Mar 13, 2023
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.

4 participants