Move caching to after all configuration refinements and ...
move compilation cache setting to the scripting host configuration The cache implementation can now take into account all external dependencies that may appear during refinement. Also change initial refinement location accordingly and rearrange caching interfaces to make it available in scripting plugin.
This commit is contained in:
+3
-1
@@ -13,12 +13,14 @@ import kotlin.script.experimental.api.ScriptCompilationConfiguration
|
||||
import kotlin.script.experimental.api.SourceCode
|
||||
import kotlin.script.experimental.api.hostConfiguration
|
||||
import kotlin.script.experimental.host.ScriptingHostConfiguration
|
||||
import kotlin.script.experimental.jvm.CompiledJvmScriptsCache
|
||||
import kotlin.script.experimental.jvm.baseClassLoader
|
||||
import kotlin.script.experimental.jvm.impl.KJvmCompiledScript
|
||||
import kotlin.script.experimental.jvm.impl.createScriptFromClassLoader
|
||||
import kotlin.script.experimental.jvm.jvm
|
||||
|
||||
open class CompiledScriptJarsCache(val scriptToFile: (SourceCode, ScriptCompilationConfiguration) -> File?) : CompiledJvmScriptsCache {
|
||||
open class CompiledScriptJarsCache(val scriptToFile: (SourceCode, ScriptCompilationConfiguration) -> File?) :
|
||||
CompiledJvmScriptsCache {
|
||||
|
||||
override fun get(script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration): CompiledScript<*>? {
|
||||
val file = scriptToFile(script, scriptCompilationConfiguration)
|
||||
+7
-36
@@ -14,26 +14,9 @@ import kotlin.script.experimental.host.ScriptingHostConfiguration
|
||||
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration
|
||||
import kotlin.script.experimental.jvmhost.impl.withDefaults
|
||||
|
||||
interface CompiledJvmScriptsCache {
|
||||
fun get(script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration): CompiledScript<*>?
|
||||
fun store(compiledScript: CompiledScript<*>, script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration)
|
||||
|
||||
object NoCache : CompiledJvmScriptsCache {
|
||||
override fun get(
|
||||
script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||
): CompiledScript<*>? = null
|
||||
|
||||
override fun store(
|
||||
compiledScript: CompiledScript<*>, script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||
) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
open class JvmScriptCompiler(
|
||||
baseHostConfiguration: ScriptingHostConfiguration = defaultJvmScriptingHostConfiguration,
|
||||
compilerProxy: ScriptJvmCompilerProxy? = null,
|
||||
val cache: CompiledJvmScriptsCache = CompiledJvmScriptsCache.NoCache
|
||||
compilerProxy: ScriptJvmCompilerProxy? = null
|
||||
) : ScriptCompiler {
|
||||
|
||||
val hostConfiguration = baseHostConfiguration.withDefaults()
|
||||
@@ -43,24 +26,12 @@ open class JvmScriptCompiler(
|
||||
override suspend operator fun invoke(
|
||||
script: SourceCode,
|
||||
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||
): ResultWithDiagnostics<CompiledScript<*>> {
|
||||
|
||||
// TODO: implement caching deeper in the compilation pipeline - the actual configuration should be calculated first, with dependencies and imported scripts
|
||||
// Note that previous implementation wasn't a solution for that and added problems with cache usage. Now it is consistent although shallow.
|
||||
|
||||
val cached = cache.get(script, scriptCompilationConfiguration)
|
||||
if (cached != null) return cached.asSuccess()
|
||||
|
||||
return scriptCompilationConfiguration.with {
|
||||
hostConfiguration(this@JvmScriptCompiler.hostConfiguration)
|
||||
}.refineBeforeParsing(script).onSuccess { refinedConfiguration ->
|
||||
|
||||
compilerProxy.compile(script, refinedConfiguration).also {
|
||||
if (it is ResultWithDiagnostics.Success) {
|
||||
cache.store(it.value, script, scriptCompilationConfiguration)
|
||||
}
|
||||
): ResultWithDiagnostics<CompiledScript<*>> =
|
||||
compilerProxy.compile(
|
||||
script,
|
||||
scriptCompilationConfiguration.with {
|
||||
hostConfiguration(this@JvmScriptCompiler.hostConfiguration)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user