-
Notifications
You must be signed in to change notification settings - Fork 438
improvement: Save semanticdb to disk under setting #8762
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
Open
tgodzik
wants to merge
1
commit into
scalameta:main-v2
Choose a base branch
from
tgodzik:add-caching-mechanismt
base: main-v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+281
−25
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| package scala.meta.internal.metals.mbt | ||
|
|
||
| import java.nio.file.Files | ||
| import java.time.Duration | ||
| import java.util.Collections | ||
| import java.util.concurrent.CancellationException | ||
| import java.util.concurrent.TimeoutException | ||
| import java.util.{LinkedHashMap => JLinkedHashMap} | ||
|
|
||
| import scala.annotation.tailrec | ||
| import scala.collection.concurrent.TrieMap | ||
| import scala.collection.mutable | ||
| import scala.collection.mutable.Buffer | ||
| import scala.concurrent.Await | ||
|
|
@@ -20,6 +22,7 @@ import scala.meta.internal.metals.AdjustRange | |
| import scala.meta.internal.metals.Buffers | ||
| import scala.meta.internal.metals.BuildTargets | ||
| import scala.meta.internal.metals.Compilers | ||
| import scala.meta.internal.metals.Directories | ||
| import scala.meta.internal.metals.EmptyCancelToken | ||
| import scala.meta.internal.metals.MetalsEnrichments._ | ||
| import scala.meta.internal.metals.ReferencesResult | ||
|
|
@@ -28,6 +31,7 @@ import scala.meta.internal.metals.Synthetics | |
| import scala.meta.internal.metals.TaskProgress | ||
| import scala.meta.internal.metals.Time | ||
| import scala.meta.internal.metals.Timer | ||
| import scala.meta.internal.metals.UserConfiguration | ||
| import scala.meta.internal.metals.WorkDoneProgress | ||
| import scala.meta.internal.metals.clients.language.MetalsLanguageClient | ||
| import scala.meta.internal.metals.noAdjustRange | ||
|
|
@@ -55,8 +59,14 @@ class MbtReferenceProvider( | |
| languageClient: MetalsLanguageClient, | ||
| workDoneProgress: WorkDoneProgress, | ||
| metrics: MonitoringClient, | ||
| workspace: AbsolutePath, | ||
| userConfig: () => UserConfiguration, | ||
| )(implicit ec: ExecutionContext) { | ||
| private val cache = new TextDocumentCache() | ||
| private val cache = new TextDocumentCache( | ||
| workspace.resolve(Directories.semanticdbCache), | ||
| () => userConfig().mbtConfig.semanticdbCacheEnabled, | ||
| () => userConfig().mbtConfig.semanticdbCacheMaxSize, | ||
| ) | ||
|
|
||
| // When looking for usages of a method, we don't visit supermethods from these | ||
| // types because it would result in a ton of noisy results. This list should | ||
|
|
@@ -683,8 +693,67 @@ class MbtReferenceProvider( | |
| // presentation compilers when MBT is available and provides all sources and | ||
| // dependencies through the fallback compiler. In BSP mode, SemanticDBs are | ||
| // tried first and per-target presentation compilers are used as a fallback. | ||
| private class TextDocumentCache { | ||
| private val cache = TrieMap.empty[AbsolutePath, s.TextDocument] | ||
| private class TextDocumentCache( | ||
| cacheDir: AbsolutePath, | ||
| isPersistenceEnabled: () => Boolean, | ||
| maxCacheSize: () => Int, | ||
| ) { | ||
| private val cache: java.util.Map[AbsolutePath, s.TextDocument] = | ||
| Collections.synchronizedMap( | ||
| new JLinkedHashMap[AbsolutePath, s.TextDocument]( | ||
| 16, // initial capacity | ||
| 0.75f, // load factor | ||
| true, // accessOrder - enables LRU ordering | ||
| ) { | ||
| override def removeEldestEntry( | ||
| eldest: java.util.Map.Entry[AbsolutePath, s.TextDocument] | ||
| ): Boolean = size() > maxCacheSize() | ||
| } | ||
| ) | ||
|
|
||
| private def diskCachePath(path: AbsolutePath): AbsolutePath = { | ||
| val relativePath = path.parent.toRelative(workspace) | ||
| val semanticdbPath = cacheDir.resolve(relativePath) | ||
| val name = path.filename + ".semanticdb" | ||
| semanticdbPath.resolve(name) | ||
| } | ||
|
|
||
| private def loadFromDisk( | ||
| path: AbsolutePath, | ||
| md5: String, | ||
| ): Option[s.TextDocument] = { | ||
| if (!isPersistenceEnabled()) None | ||
| else { | ||
| val cachePath = diskCachePath(path) | ||
| if (cachePath.exists) { | ||
| try { | ||
| val bytes = Files.readAllBytes(cachePath.toNIO) | ||
| val doc = s.TextDocument.parseFrom(bytes) | ||
| if (doc.md5 == md5) Some(doc) | ||
| else None | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } catch { | ||
| case e: Exception => | ||
| scribe.debug(s"Failed to load cached document for $path", e) | ||
| None | ||
| } | ||
| } else None | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a newline between |
||
| private def saveToDisk( | ||
| path: AbsolutePath, | ||
| doc: s.TextDocument, | ||
| ): Unit = { | ||
| if (isPersistenceEnabled()) | ||
| try { | ||
| val cachePath = diskCachePath(path) | ||
| cachePath.parent.createDirectories() | ||
| Files.write(cachePath.toNIO, doc.toByteArray) | ||
| } catch { | ||
| case e: Exception => | ||
| scribe.debug(s"Failed to save cached document for $path", e) | ||
| } | ||
| } | ||
|
|
||
| def indexSingle(path: AbsolutePath): s.TextDocument = { | ||
| index(Seq(path)).documents.headOption.getOrElse { | ||
| scribe.warn(s"references: no document found for $path") | ||
|
|
@@ -700,12 +769,19 @@ class MbtReferenceProvider( | |
| }.toMap | ||
|
|
||
| val toIndex = pathsWithMd5.filter { case (path, md5) => | ||
| cache.get(path) match { | ||
| Option(cache.get(path)) match { | ||
| case Some(doc) if doc.md5 == md5 => | ||
| docs += doc | ||
| false | ||
| case _ => | ||
| true | ||
| loadFromDisk(path, md5) match { | ||
| case Some(doc) => | ||
| docs += doc | ||
| cache.put(path, doc) | ||
| false | ||
| case None => | ||
| true | ||
| } | ||
| } | ||
| } | ||
| if (toIndex.isEmpty) { | ||
|
|
@@ -719,6 +795,7 @@ class MbtReferenceProvider( | |
| val docAdjusted = doc.withUri(path.toURI.toString) | ||
| docs += docAdjusted | ||
| cache.put(path, docAdjusted) | ||
| saveToDisk(path, docAdjusted) | ||
| case _ => | ||
| needCompiler += path | ||
| } | ||
|
|
@@ -753,13 +830,12 @@ class MbtReferenceProvider( | |
| } | ||
| } | ||
| val result = fetchDocs(maxRetries = 3) | ||
| result.foreach { doc => | ||
| doc.uri.toAbsolutePathSafe match { | ||
| case Some(path) => | ||
| cache.put(path, doc) | ||
| case None => | ||
| scribe.warn(s"references: no path found for ${doc.uri}") | ||
| } | ||
| for { | ||
| doc <- result | ||
| path <- doc.uri.toAbsolutePathSafe | ||
| } { | ||
| cache.put(path, doc) | ||
| saveToDisk(path, doc) | ||
| } | ||
| docs ++= result | ||
| s.TextDocuments(documents = docs.toSeq) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.