Skip to content

Commit a22f3d3

Browse files
Add suspended cache validation; Add possibility to remove old variables from cache; some improvements
1 parent fe50298 commit a22f3d3

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
@@ -136,7 +139,8 @@ interface ReplForJupyter {
136139

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

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

141145
val homeDir: File?
142146

@@ -209,7 +213,7 @@ class ReplForJupyterImpl(
209213

210214
override val variablesSerializer = VariablesSerializer()
211215

212-
private val librariesScanner = LibrariesScanner(notebook)
216+
val librariesScanner = LibrariesScanner(notebook)
213217
private val resourcesProcessor = LibraryResourcesProcessorImpl()
214218

215219
override var outputConfig
@@ -455,8 +459,10 @@ class ReplForJupyterImpl(
455459
// printUsagesInfo(jupyterId, cellVariables[jupyterId - 1])
456460
val serializedData = variablesSerializer.serializeVariables(jupyterId - 1, notebook.variablesState, notebook.unchangedVariables())
457461

462+
GlobalScope.launch(Dispatchers.Default) {
463+
variablesSerializer.tryValidateCache(jupyterId - 1, notebook.cellVariables)
464+
}
458465

459-
val variablesStateUpdate = notebook.variablesState.mapValues { "" }
460466
EvalResultEx(
461467
result.result.value,
462468
rendered,
@@ -559,8 +565,9 @@ class ReplForJupyterImpl(
559565
doWithLock(SerializationArgs(descriptorsState, cellId = cellId, callback = callback), serializationQueue, SerializationReply(cellId, descriptorsState), ::doSerializeVariables)
560566
}
561567

562-
override suspend fun serializeVariables(topLevelVarName: String, descriptorsState: Map<String, SerializedVariablesState>, callback: (SerializationReply) -> Unit) {
563-
doWithLock(SerializationArgs(descriptorsState, topLevelVarName = topLevelVarName, callback = callback), serializationQueue, SerializationReply(), ::doSerializeVariables)
568+
override suspend fun serializeVariables(topLevelVarName: String, descriptorsState: Map<String, SerializedVariablesState>, pathToDescriptor: List<String>,
569+
callback: (SerializationReply) -> Unit) {
570+
doWithLock(SerializationArgs(descriptorsState, topLevelVarName = topLevelVarName, callback = callback, pathToDescriptor = pathToDescriptor), serializationQueue, SerializationReply(), ::doSerializeVariables)
564571
}
565572

566573
private fun doSerializeVariables(args: SerializationArgs): SerializationReply {
@@ -571,7 +578,7 @@ class ReplForJupyterImpl(
571578
finalAns
572579
}
573580
args.descriptorsState.forEach { (name, state) ->
574-
resultMap[name] = variablesSerializer.doIncrementalSerialization(cellId - 1, name, state)
581+
resultMap[name] = variablesSerializer.doIncrementalSerialization(cellId - 1, name, state, args.pathToDescriptor)
575582
}
576583
log.debug("Serialization cellID: $cellId")
577584
log.debug("Serialization answer: ${resultMap.entries.first().value.fieldDescriptor}")
@@ -615,6 +622,7 @@ class ReplForJupyterImpl(
615622
val descriptorsState: Map<String, SerializedVariablesState>,
616623
var cellId: Int = -1,
617624
val topLevelVarName: String = "",
625+
val pathToDescriptor: List<String> = emptyList(),
618626
override val callback: (SerializationReply) -> Unit
619627
) : LockQueueArgs<SerializationReply>
620628

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)