Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions metals/src/main/scala/scala/meta/internal/metals/Configs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,28 @@ object Configs {
}
}

final case class MbtConfig(
importGeneratedSources: Boolean,
semanticdbCacheEnabled: Boolean,
semanticdbCacheMaxSize: Int,
)

object MbtConfig {
val defaultMaxSize: Int = Int.MaxValue
def default: MbtConfig =
MbtConfig(false, false, defaultMaxSize)
def fromConfig(
importGeneratedSources: Option[Boolean],
semanticdbCacheEnabled: Option[Boolean],
semanticdbCacheMaxSize: Option[Int],
): MbtConfig =
MbtConfig(
importGeneratedSources.getOrElse(false),
semanticdbCacheEnabled.getOrElse(false),
semanticdbCacheMaxSize.getOrElse(defaultMaxSize),
)
}

final case class ProtoOutlineProviderConfig(val value: String) {
require(List("v1", "v2").contains(value), value)
def isV1: Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ object Directories {
RelativePath(".metals").resolve("rules")
def explainedDiagnostics: RelativePath =
RelativePath(".metals").resolve("explained-diagnostics")
def semanticdbCache: RelativePath =
RelativePath(".metals").resolve("semanticdb-cache")

val stacktraceFilename = "stacktrace.scala"
val dependenciesName = "dependencies"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,8 @@ abstract class MetalsLspService(
languageClient,
workDoneProgress,
metrics,
folder,
() => userConfig,
)

val referencesProvider: ReferenceProvider = new ReferenceProvider(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import scala.meta.internal.metals.Configs.FallbackClasspathConfig
import scala.meta.internal.metals.Configs.FallbackSourcepathConfig
import scala.meta.internal.metals.Configs.JavaSymbolLoaderConfig
import scala.meta.internal.metals.Configs.JavacServicesOverrides
import scala.meta.internal.metals.Configs.MbtConfig
import scala.meta.internal.metals.Configs.ProtoOutlineProviderConfig
import scala.meta.internal.metals.Configs.ProtobufLspConfig
import scala.meta.internal.metals.Configs.RangeFormattingProviders
Expand Down Expand Up @@ -123,7 +124,7 @@ case class UserConfiguration(
defaultShell: Option[String] = None,
startMcpServer: Boolean = false,
mcpClient: Option[String] = None,
importGeneratedSourcesMbt: Boolean = false,
mbtConfig: MbtConfig = MbtConfig.default,
) {

def isMbtDefinitionProviderEnabled: Boolean =
Expand Down Expand Up @@ -368,8 +369,12 @@ case class UserConfiguration(
optStringField("mcpClient", mcpClient),
Some(
(
"importGeneratedSourcesMbt",
importGeneratedSourcesMbt,
"mbt",
Map(
"importGeneratedSources" -> mbtConfig.importGeneratedSources,
"semanticdbCacheEnabled" -> mbtConfig.semanticdbCacheEnabled,
"semanticdbCacheMaxSize" -> mbtConfig.semanticdbCacheMaxSize,
).asJava,
)
),
Some(
Expand Down Expand Up @@ -931,7 +936,7 @@ object UserConfiguration {
|""".stripMargin,
),
UserConfigurationOption(
"import-generated-sources-mbt",
"mbt.import-generated-sources",
"false",
"true",
"Import Generated Sources In MBT Builds",
Expand All @@ -940,6 +945,28 @@ object UserConfiguration {
|""".stripMargin,
isBoolean = true,
),
UserConfigurationOption(
"mbt.semanticdb-cache-enabled",
"false",
"true",
"Enable MBT Semanticdb Cache",
"""|If enabled, Metals will persist semanticdb documents to disk in the
|`.metals/semanticdb-cache` directory. This can improve performance for
|find references and implementations operations in MBT mode by avoiding
|recalculating semanticdb when files haven't changed.
|""".stripMargin,
isBoolean = true,
),
UserConfigurationOption(
"mbt.semanticdb-cache-max-size",
Int.MaxValue.toString(),
"2000",
"MBT Semanticdb In-Memory Cache Size Limit",
"""|Maximum number of semanticdb documents to keep in the in-memory cache.
|When this limit is exceeded, the least recently used documents are evicted.
|A higher value uses more memory but can improve performance for large projects.
|""".stripMargin,
),
)

def listOptions: String =
Expand Down Expand Up @@ -1029,9 +1056,15 @@ object UserConfiguration {
)

def getBooleanKey(key: String): Option[Boolean] =
getBooleanKeyOnObj(key, json)

def getBooleanKeyOnObj(
key: String,
currentObject: JsonObject,
): Option[Boolean] =
getKey(
key,
json,
currentObject,
{ value =>
Try(value.getAsBoolean())
.fold(
Expand All @@ -1043,8 +1076,12 @@ object UserConfiguration {
)
},
)

def getIntKey(key: String): Option[Int] =
getStringKey(key).flatMap { value =>
getIntKeyOnObj(key, json)

def getIntKeyOnObj(key: String, currentObject: JsonObject): Option[Int] =
getStringKeyOnObj(key, currentObject).flatMap { value =>
Try(value.toInt) match {
case Failure(_) =>
errors += s"Not a number: '$value'"
Expand Down Expand Up @@ -1470,8 +1507,18 @@ object UserConfiguration {

val mcpClient = getStringKey("mcp-client")

val importGeneratedSourcesMbt =
getBooleanKey("import-generated-sources-mbt").getOrElse(false)
val mbtSubKey = getSubKey("mbt")
val mbtConfig = MbtConfig.fromConfig(
mbtSubKey
.flatMap(getBooleanKeyOnObj("import-generated-sources", _))
.orElse(getBooleanKey("import-generated-sources-mbt")),
mbtSubKey
.flatMap(getBooleanKeyOnObj("semanticdb-cache-enabled", _))
.orElse(getBooleanKey("mbt-semanticdb-cache")),
mbtSubKey
.flatMap(getIntKeyOnObj("semanticdb-cache-max-size", _))
.orElse(getIntKey("mbt-semanticdb-cache-max-size")),
)

if (errors.isEmpty) {
Right(
Expand Down Expand Up @@ -1537,7 +1584,7 @@ object UserConfiguration {
defaultShell,
startMcpServer,
mcpClient,
importGeneratedSourcesMbt,
mbtConfig,
)
)
} else {
Expand Down
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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Comment thread
tgodzik marked this conversation as resolved.
)

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
Comment thread
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
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we add a newline between loadFromDisk and saveToDisk?

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")
Expand All @@ -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) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ abstract class BazelMbtImporter(
targetsXmlDump = new BazelTargetsXmlDump(targetsXmlQueryOutput)
srcs = targetsXmlDump.getLabels("srcs")
(genSrcOutputsByTarget, genSrcLabels) <-
if (userConfig().importGeneratedSourcesMbt)
if (userConfig().mbtConfig.importGeneratedSources)
queryGenSrcOutputsByTarget(srcs)
else
Future.successful(
Expand Down
Loading
Loading