Skip to content

Commit 18e844c

Browse files
Add suspended cache validation; Add possibility to remove old variables from cache; some improvements
1 parent dc305f7 commit 18e844c

File tree

8 files changed

+348
-64
lines changed

8 files changed

+348
-64
lines changed

src/main/kotlin/org/jetbrains/kotlinx/jupyter/message_types.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,8 @@ class ListErrorsReply(
560560
class SerializationRequest(
561561
val cellId: Int,
562562
val descriptorsState: Map<String, SerializedVariablesState>,
563-
val topLevelDescriptorName: String = ""
563+
val topLevelDescriptorName: String = "",
564+
val pathToDescriptor: List<String> = emptyList()
564565
) : MessageContent()
565566

566567
@Serializable

src/main/kotlin/org/jetbrains/kotlinx/jupyter/protocol.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,11 @@ fun JupyterConnection.Socket.shellMessagesHandler(msg: Message, repl: ReplForJup
317317

318318
val messageContent = getVariablesDescriptorsFromJson(data)
319319
GlobalScope.launch(Dispatchers.Default) {
320-
repl.serializeVariables(messageContent.topLevelDescriptorName, messageContent.descriptorsState) { result ->
320+
repl.serializeVariables(
321+
messageContent.topLevelDescriptorName,
322+
messageContent.descriptorsState,
323+
messageContent.pathToDescriptor
324+
) { result ->
321325
sendWrapped(msg, makeReplyMessage(msg, MessageType.COMM_OPEN, content = result))
322326
}
323327
}
@@ -339,7 +343,7 @@ fun JupyterConnection.Socket.shellMessagesHandler(msg: Message, repl: ReplForJup
339343
is SerializationRequest -> {
340344
GlobalScope.launch(Dispatchers.Default) {
341345
if (content.topLevelDescriptorName.isNotEmpty()) {
342-
repl.serializeVariables(content.topLevelDescriptorName, content.descriptorsState) { result ->
346+
repl.serializeVariables(content.topLevelDescriptorName, content.descriptorsState, content.pathToDescriptor) { result ->
343347
sendWrapped(msg, makeReplyMessage(msg, MessageType.SERIALIZATION_REPLY, content = result))
344348
}
345349
} else {

src/main/kotlin/org/jetbrains/kotlinx/jupyter/repl.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import jupyter.kotlin.DependsOn
55
import jupyter.kotlin.KotlinContext
66
import jupyter.kotlin.KotlinKernelHostProvider
77
import jupyter.kotlin.Repository
8+
import kotlinx.coroutines.Dispatchers
9+
import kotlinx.coroutines.GlobalScope
10+
import kotlinx.coroutines.launch
811
import org.jetbrains.annotations.TestOnly
912
import org.jetbrains.kotlin.config.KotlinCompilerVersion
1013
import org.jetbrains.kotlinx.jupyter.api.Code
@@ -139,7 +142,8 @@ interface ReplForJupyter {
139142

140143
suspend fun serializeVariables(cellId: Int, descriptorsState: Map<String, SerializedVariablesState>, callback: (SerializationReply) -> Unit)
141144

142-
suspend fun serializeVariables(topLevelVarName: String, descriptorsState: Map<String, SerializedVariablesState>, callback: (SerializationReply) -> Unit)
145+
suspend fun serializeVariables(topLevelVarName: String, descriptorsState: Map<String, SerializedVariablesState>, pathToDescriptor: List<String> = emptyList(),
146+
callback: (SerializationReply) -> Unit)
143147

144148
val homeDir: File?
145149

@@ -212,7 +216,7 @@ class ReplForJupyterImpl(
212216

213217
override val variablesSerializer = VariablesSerializer()
214218

215-
private val librariesScanner = LibrariesScanner(notebook)
219+
val librariesScanner = LibrariesScanner(notebook)
216220
private val resourcesProcessor = LibraryResourcesProcessorImpl()
217221

218222
override var outputConfig
@@ -459,8 +463,10 @@ class ReplForJupyterImpl(
459463
// printUsagesInfo(jupyterId, cellVariables[jupyterId - 1])
460464
val serializedData = variablesSerializer.serializeVariables(jupyterId - 1, notebook.variablesState, notebook.unchangedVariables())
461465

466+
GlobalScope.launch(Dispatchers.Default) {
467+
variablesSerializer.tryValidateCache(jupyterId - 1, notebook.cellVariables)
468+
}
462469

463-
val variablesStateUpdate = notebook.variablesState.mapValues { "" }
464470
EvalResultEx(
465471
result.result.value,
466472
rendered,
@@ -567,8 +573,9 @@ class ReplForJupyterImpl(
567573
doWithLock(SerializationArgs(descriptorsState, cellId = cellId, callback = callback), serializationQueue, SerializationReply(cellId, descriptorsState), ::doSerializeVariables)
568574
}
569575

570-
override suspend fun serializeVariables(topLevelVarName: String, descriptorsState: Map<String, SerializedVariablesState>, callback: (SerializationReply) -> Unit) {
571-
doWithLock(SerializationArgs(descriptorsState, topLevelVarName = topLevelVarName, callback = callback), serializationQueue, SerializationReply(), ::doSerializeVariables)
576+
override suspend fun serializeVariables(topLevelVarName: String, descriptorsState: Map<String, SerializedVariablesState>, pathToDescriptor: List<String>,
577+
callback: (SerializationReply) -> Unit) {
578+
doWithLock(SerializationArgs(descriptorsState, topLevelVarName = topLevelVarName, callback = callback, pathToDescriptor = pathToDescriptor), serializationQueue, SerializationReply(), ::doSerializeVariables)
572579
}
573580

574581
private fun doSerializeVariables(args: SerializationArgs): SerializationReply {
@@ -579,7 +586,7 @@ class ReplForJupyterImpl(
579586
finalAns
580587
}
581588
args.descriptorsState.forEach { (name, state) ->
582-
resultMap[name] = variablesSerializer.doIncrementalSerialization(cellId - 1, name, state)
589+
resultMap[name] = variablesSerializer.doIncrementalSerialization(cellId - 1, name, state, args.pathToDescriptor)
583590
}
584591
log.debug("Serialization cellID: $cellId")
585592
log.debug("Serialization answer: ${resultMap.entries.first().value.fieldDescriptor}")
@@ -623,6 +630,7 @@ class ReplForJupyterImpl(
623630
val descriptorsState: Map<String, SerializedVariablesState>,
624631
var cellId: Int = -1,
625632
val topLevelVarName: String = "",
633+
val pathToDescriptor: List<String> = emptyList(),
626634
override val callback: (SerializationReply) -> Unit
627635
) : LockQueueArgs<SerializationReply>
628636

src/main/kotlin/org/jetbrains/kotlinx/jupyter/repl/impl/InternalEvaluatorImpl.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ internal class InternalEvaluatorImpl(
155155

156156
private fun updateVariablesState(cellId: Int) {
157157
variablesWatcher.removeOldUsages(cellId)
158-
159158
variablesHolder.forEach {
160159
val state = it.value as VariableStateImpl
161160

@@ -189,6 +188,8 @@ internal class InternalEvaluatorImpl(
189188

190189
put(property.name, state)
191190
}
191+
// remove old
192+
variablesWatcher.removeOldDeclarations(cellId, addedDeclarations)
192193
}
193194
}
194195

@@ -199,7 +200,7 @@ internal class InternalEvaluatorImpl(
199200
private fun updateDataAfterExecution(lastExecutionCellId: Int, resultValue: ResultValue) {
200201
variablesWatcher.ensureStorageCreation(lastExecutionCellId)
201202
variablesHolder += getVisibleVariables(resultValue, lastExecutionCellId)
202-
203+
// remove unreached variables
203204
updateVariablesState(lastExecutionCellId)
204205
}
205206
}

0 commit comments

Comments
 (0)