Skip to content

Commit 3e4137f

Browse files
Add suspended cache validation; Add possibility to remove old variables from cache; some improvements
1 parent 3a4f45f commit 3e4137f

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
@@ -578,7 +578,8 @@ class ListErrorsReply(
578578
class SerializationRequest(
579579
val cellId: Int,
580580
val descriptorsState: Map<String, SerializedVariablesState>,
581-
val topLevelDescriptorName: String = ""
581+
val topLevelDescriptorName: String = "",
582+
val pathToDescriptor: List<String> = emptyList()
582583
) : MessageContent()
583584

584585
@Serializable

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

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

316316
val messageContent = getVariablesDescriptorsFromJson(data)
317317
GlobalScope.launch(Dispatchers.Default) {
318-
repl.serializeVariables(messageContent.topLevelDescriptorName, messageContent.descriptorsState) { result ->
318+
repl.serializeVariables(
319+
messageContent.topLevelDescriptorName,
320+
messageContent.descriptorsState,
321+
messageContent.pathToDescriptor
322+
) { result ->
319323
sendWrapped(msg, makeReplyMessage(msg, MessageType.COMM_OPEN, content = result))
320324
}
321325
}
@@ -337,7 +341,7 @@ fun JupyterConnection.Socket.shellMessagesHandler(msg: Message, repl: ReplForJup
337341
is SerializationRequest -> {
338342
GlobalScope.launch(Dispatchers.Default) {
339343
if (content.topLevelDescriptorName.isNotEmpty()) {
340-
repl.serializeVariables(content.topLevelDescriptorName, content.descriptorsState) { result ->
344+
repl.serializeVariables(content.topLevelDescriptorName, content.descriptorsState, content.pathToDescriptor) { result ->
341345
sendWrapped(msg, makeReplyMessage(msg, MessageType.SERIALIZATION_REPLY, content = result))
342346
}
343347
} 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
@@ -122,7 +125,8 @@ interface ReplForJupyter {
122125

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

125-
suspend fun serializeVariables(topLevelVarName: String, descriptorsState: Map<String, SerializedVariablesState>, callback: (SerializationReply) -> Unit)
128+
suspend fun serializeVariables(topLevelVarName: String, descriptorsState: Map<String, SerializedVariablesState>, pathToDescriptor: List<String> = emptyList(),
129+
callback: (SerializationReply) -> Unit)
126130

127131
val homeDir: File?
128132

@@ -193,7 +197,7 @@ class ReplForJupyterImpl(
193197

194198
override val variablesSerializer = VariablesSerializer()
195199

196-
private val librariesScanner = LibrariesScanner(notebook)
200+
val librariesScanner = LibrariesScanner(notebook)
197201
private val resourcesProcessor = LibraryResourcesProcessorImpl()
198202

199203
override var outputConfig
@@ -428,8 +432,10 @@ class ReplForJupyterImpl(
428432
// printUsagesInfo(jupyterId, cellVariables[jupyterId - 1])
429433
val serializedData = variablesSerializer.serializeVariables(jupyterId - 1, notebook.variablesState, notebook.unchangedVariables())
430434

435+
GlobalScope.launch(Dispatchers.Default) {
436+
variablesSerializer.tryValidateCache(jupyterId - 1, notebook.cellVariables)
437+
}
431438

432-
val variablesStateUpdate = notebook.variablesState.mapValues { "" }
433439
EvalResultEx(
434440
result.result.value,
435441
rendered,
@@ -532,8 +538,9 @@ class ReplForJupyterImpl(
532538
doWithLock(SerializationArgs(descriptorsState, cellId = cellId, callback = callback), serializationQueue, SerializationReply(cellId, descriptorsState), ::doSerializeVariables)
533539
}
534540

535-
override suspend fun serializeVariables(topLevelVarName: String, descriptorsState: Map<String, SerializedVariablesState>, callback: (SerializationReply) -> Unit) {
536-
doWithLock(SerializationArgs(descriptorsState, topLevelVarName = topLevelVarName, callback = callback), serializationQueue, SerializationReply(), ::doSerializeVariables)
541+
override suspend fun serializeVariables(topLevelVarName: String, descriptorsState: Map<String, SerializedVariablesState>, pathToDescriptor: List<String>,
542+
callback: (SerializationReply) -> Unit) {
543+
doWithLock(SerializationArgs(descriptorsState, topLevelVarName = topLevelVarName, callback = callback, pathToDescriptor = pathToDescriptor), serializationQueue, SerializationReply(), ::doSerializeVariables)
537544
}
538545

539546
private fun doSerializeVariables(args: SerializationArgs): SerializationReply {
@@ -544,7 +551,7 @@ class ReplForJupyterImpl(
544551
finalAns
545552
}
546553
args.descriptorsState.forEach { (name, state) ->
547-
resultMap[name] = variablesSerializer.doIncrementalSerialization(cellId - 1, name, state)
554+
resultMap[name] = variablesSerializer.doIncrementalSerialization(cellId - 1, name, state, args.pathToDescriptor)
548555
}
549556
log.debug("Serialization cellID: $cellId")
550557
log.debug("Serialization answer: ${resultMap.entries.first().value.fieldDescriptor}")
@@ -588,6 +595,7 @@ class ReplForJupyterImpl(
588595
val descriptorsState: Map<String, SerializedVariablesState>,
589596
var cellId: Int = -1,
590597
val topLevelVarName: String = "",
598+
val pathToDescriptor: List<String> = emptyList(),
591599
override val callback: (SerializationReply) -> Unit
592600
) : LockQueueArgs<SerializationReply>
593601

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)