Skip to content

Commit 68f82c8

Browse files
Add suspended cache validation; Add possibility to remove old variables from cache; some improvements
1 parent a59da9a commit 68f82c8

File tree

8 files changed

+361
-64
lines changed

8 files changed

+361
-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
@@ -316,7 +316,11 @@ fun JupyterConnection.Socket.shellMessagesHandler(msg: Message, repl: ReplForJup
316316

317317
val messageContent = getVariablesDescriptorsFromJson(data)
318318
GlobalScope.launch(Dispatchers.Default) {
319-
repl.serializeVariables(messageContent.topLevelDescriptorName, messageContent.descriptorsState) { result ->
319+
repl.serializeVariables(
320+
messageContent.topLevelDescriptorName,
321+
messageContent.descriptorsState,
322+
messageContent.pathToDescriptor
323+
) { result ->
320324
sendWrapped(msg, makeReplyMessage(msg, MessageType.COMM_OPEN, content = result))
321325
}
322326
}
@@ -338,7 +342,7 @@ fun JupyterConnection.Socket.shellMessagesHandler(msg: Message, repl: ReplForJup
338342
is SerializationRequest -> {
339343
GlobalScope.launch(Dispatchers.Default) {
340344
if (content.topLevelDescriptorName.isNotEmpty()) {
341-
repl.serializeVariables(content.topLevelDescriptorName, content.descriptorsState) { result ->
345+
repl.serializeVariables(content.topLevelDescriptorName, content.descriptorsState, content.pathToDescriptor) { result ->
342346
sendWrapped(msg, makeReplyMessage(msg, MessageType.SERIALIZATION_REPLY, content = result))
343347
}
344348
} 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
@@ -131,7 +134,8 @@ interface ReplForJupyter {
131134

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

134-
suspend fun serializeVariables(topLevelVarName: String, descriptorsState: Map<String, SerializedVariablesState>, callback: (SerializationReply) -> Unit)
137+
suspend fun serializeVariables(topLevelVarName: String, descriptorsState: Map<String, SerializedVariablesState>, pathToDescriptor: List<String> = emptyList(),
138+
callback: (SerializationReply) -> Unit)
135139

136140
val homeDir: File?
137141

@@ -202,7 +206,7 @@ class ReplForJupyterImpl(
202206

203207
override val variablesSerializer = VariablesSerializer()
204208

205-
private val librariesScanner = LibrariesScanner(notebook)
209+
val librariesScanner = LibrariesScanner(notebook)
206210
private val resourcesProcessor = LibraryResourcesProcessorImpl()
207211

208212
override var outputConfig
@@ -439,8 +443,10 @@ class ReplForJupyterImpl(
439443
// printUsagesInfo(jupyterId, cellVariables[jupyterId - 1])
440444
val serializedData = variablesSerializer.serializeVariables(jupyterId - 1, notebook.variablesState, notebook.unchangedVariables())
441445

446+
GlobalScope.launch(Dispatchers.Default) {
447+
variablesSerializer.tryValidateCache(jupyterId - 1, notebook.cellVariables)
448+
}
442449

443-
val variablesStateUpdate = notebook.variablesState.mapValues { "" }
444450
EvalResultEx(
445451
result.result.value,
446452
rendered,
@@ -543,8 +549,9 @@ class ReplForJupyterImpl(
543549
doWithLock(SerializationArgs(descriptorsState, cellId = cellId, callback = callback), serializationQueue, SerializationReply(cellId, descriptorsState), ::doSerializeVariables)
544550
}
545551

546-
override suspend fun serializeVariables(topLevelVarName: String, descriptorsState: Map<String, SerializedVariablesState>, callback: (SerializationReply) -> Unit) {
547-
doWithLock(SerializationArgs(descriptorsState, topLevelVarName = topLevelVarName, callback = callback), serializationQueue, SerializationReply(), ::doSerializeVariables)
552+
override suspend fun serializeVariables(topLevelVarName: String, descriptorsState: Map<String, SerializedVariablesState>, pathToDescriptor: List<String>,
553+
callback: (SerializationReply) -> Unit) {
554+
doWithLock(SerializationArgs(descriptorsState, topLevelVarName = topLevelVarName, callback = callback, pathToDescriptor = pathToDescriptor), serializationQueue, SerializationReply(), ::doSerializeVariables)
548555
}
549556

550557
private fun doSerializeVariables(args: SerializationArgs): SerializationReply {
@@ -555,7 +562,7 @@ class ReplForJupyterImpl(
555562
finalAns
556563
}
557564
args.descriptorsState.forEach { (name, state) ->
558-
resultMap[name] = variablesSerializer.doIncrementalSerialization(cellId - 1, name, state)
565+
resultMap[name] = variablesSerializer.doIncrementalSerialization(cellId - 1, name, state, args.pathToDescriptor)
559566
}
560567
log.debug("Serialization cellID: $cellId")
561568
log.debug("Serialization answer: ${resultMap.entries.first().value.fieldDescriptor}")
@@ -599,6 +606,7 @@ class ReplForJupyterImpl(
599606
val descriptorsState: Map<String, SerializedVariablesState>,
600607
var cellId: Int = -1,
601608
val topLevelVarName: String = "",
609+
val pathToDescriptor: List<String> = emptyList(),
602610
override val callback: (SerializationReply) -> Unit
603611
) : LockQueueArgs<SerializationReply>
604612

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ internal class InternalEvaluatorImpl(
151151

152152
private fun updateVariablesState(cellId: Int) {
153153
variablesWatcher.removeOldUsages(cellId)
154-
155154
variablesHolder.forEach {
156155
val state = it.value as VariableStateImpl
157156

@@ -171,11 +170,23 @@ internal class InternalEvaluatorImpl(
171170
it.name
172171
}.toHashSet()
173172
val ans = mutableMapOf<String, VariableStateImpl>()
173+
// maybe remove known declarations
174+
val addedDeclarations = mutableSetOf<String>()
175+
174176
fields.forEach { property ->
175177
if (!memberKPropertiesNames.contains(property.name)) return@forEach
176178

177179
val state = VariableStateImpl(property, cellClassInstance)
178180
variablesWatcher.addDeclaration(cellId, property.name)
181+
addedDeclarations.add(property.name)
182+
183+
// try check values
184+
if (variablesHolder.containsKey(property.name)) {
185+
val seenState = variablesHolder[property.name]
186+
if (seenState?.value?.equals(state.value) == true) {
187+
addedDeclarations.remove(property.name)
188+
}
189+
}
179190

180191
// it was val, now it's var
181192
if (isValField(property)) {
@@ -187,6 +198,9 @@ internal class InternalEvaluatorImpl(
187198

188199
ans[property.name] = state
189200
}
201+
// remove old
202+
variablesWatcher.removeOldDeclarations(cellId, addedDeclarations)
203+
190204
return ans
191205
}
192206

@@ -197,7 +211,7 @@ internal class InternalEvaluatorImpl(
197211
private fun updateDataAfterExecution(lastExecutionCellId: Int, resultValue: ResultValue) {
198212
variablesWatcher.ensureStorageCreation(lastExecutionCellId)
199213
variablesHolder += getVisibleVariables(resultValue, lastExecutionCellId)
200-
214+
// remove unreached variables
201215
updateVariablesState(lastExecutionCellId)
202216
}
203217
}

0 commit comments

Comments
 (0)