@@ -15,39 +15,16 @@ typealias FieldDescriptor = Map<String, SerializedVariablesState?>
1515typealias MutableFieldDescriptor = MutableMap <String , SerializedVariablesState ?>
1616typealias PropertiesData = Collection <KProperty1 <out Any , * >>
1717
18- data class ProcessedSerializedVarsState (
18+ class ProcessedSerializedVarsState (
1919 val serializedVariablesState : SerializedVariablesState ,
2020 val propertiesData : PropertiesData ? ,
2121 val jvmOnlyFields : Array <Field >? = null
22- ) {
23- // autogenerated
24- override fun equals (other : Any? ): Boolean {
25- if (this == = other) return true
26- if (javaClass != other?.javaClass) return false
27-
28- other as ProcessedSerializedVarsState
29-
30- if (serializedVariablesState != other.serializedVariablesState) return false
31- if (propertiesData != other.propertiesData) return false
32- if (jvmOnlyFields != null ) {
33- if (other.jvmOnlyFields == null ) return false
34- if (! jvmOnlyFields.contentEquals(other.jvmOnlyFields)) return false
35- } else if (other.jvmOnlyFields != null ) return false
36-
37- return true
38- }
39-
40- override fun hashCode (): Int {
41- var result = serializedVariablesState.hashCode()
42- result = 31 * result + (propertiesData?.hashCode() ? : 0 )
43- result = 31 * result + (jvmOnlyFields?.contentHashCode() ? : 0 )
44- return result
45- }
46- }
22+ )
4723
4824data class ProcessedDescriptorsState (
49- // perhaps, better tp make SerializedVariablesState -> PropertiesData?
50- val processedSerializedVarsState : MutableMap <SerializedVariablesState , PropertiesData ?> = mutableMapOf(),
25+ val processedSerializedVarsToKProperties : MutableMap <SerializedVariablesState , PropertiesData ?> = mutableMapOf(),
26+ // do we need this? Probably, not
27+ // val processedSerializedVarsToJvmFields: MutableMap<SerializedVariablesState, Array<Field>?> = mutableMapOf(),
5128 val instancesPerState : MutableMap <SerializedVariablesState , Any ?> = mutableMapOf()
5229)
5330
@@ -68,7 +45,11 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
6845 */
6946 private val computedDescriptorsPerCell: MutableMap <Int , ProcessedDescriptorsState > = mutableMapOf ()
7047
48+ private val isSerializationActive: Boolean = System .getProperty(serializationEnvProperty)?.toBooleanStrictOrNull() ? : true
49+
7150 fun serializeVariables (cellId : Int , variablesState : Map <String , VariableState >): Map <String , SerializedVariablesState > {
51+ if (! isSerializationActive) return emptyMap()
52+
7253 if (seenObjectsPerCell.containsKey(cellId)) {
7354 seenObjectsPerCell[cellId]!! .clear()
7455 }
@@ -80,6 +61,8 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
8061 }
8162
8263 fun doIncrementalSerialization (cellId : Int , propertyName : String , serializedVariablesState : SerializedVariablesState ): SerializedVariablesState {
64+ if (! isSerializationActive) return serializedVariablesState
65+
8366 val cellDescriptors = computedDescriptorsPerCell[cellId] ? : return serializedVariablesState
8467 return updateVariableState(cellId, propertyName, cellDescriptors, serializedVariablesState)
8568 }
@@ -94,9 +77,8 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
9477 evaluatedDescriptorsState : ProcessedDescriptorsState ,
9578 serializedVariablesState : SerializedVariablesState
9679 ): SerializedVariablesState {
97- // TODO: consider anonymous class as well and fix bug with state
9880 val value = evaluatedDescriptorsState.instancesPerState[serializedVariablesState]
99- val propertiesData = evaluatedDescriptorsState.processedSerializedVarsState [serializedVariablesState]
81+ val propertiesData = evaluatedDescriptorsState.processedSerializedVarsToKProperties [serializedVariablesState]
10082 if (propertiesData == null && value != null && (value::class .java.isArray || value::class .java.isMemberClass)) {
10183 return serializeVariableState(cellId, propertyName, propertiesData, value, false )
10284 }
@@ -108,21 +90,25 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
10890 }
10991
11092 fun serializeVariableState (cellId : Int , name : String? , variableState : VariableState ? , isOverride : Boolean = true): SerializedVariablesState {
111- if (variableState == null || name == null ) return SerializedVariablesState ()
93+ if (! isSerializationActive || variableState == null || name == null ) return SerializedVariablesState ()
11294 return serializeVariableState(cellId, name, variableState.property, variableState.value, isOverride)
11395 }
11496
115- fun serializeVariableState (cellId : Int , name : String , property : Field ? , value : Any? , isOverride : Boolean = true): SerializedVariablesState {
97+ private fun serializeVariableState (cellId : Int , name : String , property : Field ? , value : Any? , isOverride : Boolean = true): SerializedVariablesState {
11698 val processedData = createSerializeVariableState(name, getSimpleTypeNameFrom(property, value), value)
11799 return doActualSerialization(cellId, processedData, value, isOverride)
118100 }
119101
120- fun serializeVariableState (cellId : Int , name : String , property : KProperty <* >, value : Any? , isOverride : Boolean = true): SerializedVariablesState {
102+ private fun serializeVariableState (cellId : Int , name : String , property : KProperty <* >, value : Any? , isOverride : Boolean = true): SerializedVariablesState {
121103 val processedData = createSerializeVariableState(name, getSimpleTypeNameFrom(property, value), value)
122104 return doActualSerialization(cellId, processedData, value, isOverride)
123105 }
124106
125107 private fun doActualSerialization (cellId : Int , processedData : ProcessedSerializedVarsState , value : Any? , isOverride : Boolean = true): SerializedVariablesState {
108+ fun isCanBeComputed (fieldDescriptors : MutableMap <String , SerializedVariablesState ?>): Boolean {
109+ return (fieldDescriptors.isEmpty() || (fieldDescriptors.isNotEmpty() && fieldDescriptors.entries.first().value?.fieldDescriptor!! .isEmpty()))
110+ }
111+
126112 val serializedVersion = processedData.serializedVariablesState
127113
128114 seenObjectsPerCell.putIfAbsent(cellId, mutableMapOf ())
@@ -131,13 +117,27 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
131117 computedDescriptorsPerCell[cellId] = ProcessedDescriptorsState ()
132118 }
133119 val currentCellDescriptors = computedDescriptorsPerCell[cellId]
134- currentCellDescriptors!! .processedSerializedVarsState[serializedVersion] = processedData.propertiesData
120+ currentCellDescriptors!! .processedSerializedVarsToKProperties[serializedVersion] = processedData.propertiesData
121+ // currentCellDescriptors.processedSerializedVarsToJvmFields[serializedVersion] = processedData.jvmOnlyFields
135122
136123 if (value != null ) {
137- seenObjectsPerCell[cellId]!! [ value] = serializedVersion
124+ seenObjectsPerCell[cellId]!! .putIfAbsent( value, serializedVersion)
138125 }
139126 if (serializedVersion.isContainer) {
140- iterateThroughContainerMembers(cellId, value, serializedVersion.fieldDescriptor, currentCellDescriptors.processedSerializedVarsState[serializedVersion])
127+ // check for seen
128+ if (seenObjectsPerCell[cellId]!! .containsKey(value)) {
129+ val previouslySerializedState = seenObjectsPerCell[cellId]!! [value] ? : return processedData.serializedVariablesState
130+ serializedVersion.fieldDescriptor + = previouslySerializedState.fieldDescriptor
131+ if (isCanBeComputed(serializedVersion.fieldDescriptor)) {
132+ iterateThroughContainerMembers(cellId, value, serializedVersion.fieldDescriptor, currentCellDescriptors.processedSerializedVarsToKProperties[serializedVersion])
133+ }
134+ } else {
135+ // add jvm descriptors
136+ processedData.jvmOnlyFields?.forEach {
137+ serializedVersion.fieldDescriptor[it.name] = serializeVariableState(cellId, it.name, it, value)
138+ }
139+ iterateThroughContainerMembers(cellId, value, serializedVersion.fieldDescriptor, currentCellDescriptors.processedSerializedVarsToKProperties[serializedVersion])
140+ }
141141 }
142142 return processedData.serializedVariablesState
143143 }
@@ -150,6 +150,7 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
150150 seenObjectsPerCell.putIfAbsent(cellId, mutableMapOf ())
151151 val seenObjectsPerCell = seenObjectsPerCell[cellId]
152152 val currentCellDescriptors = computedDescriptorsPerCell[cellId]!!
153+ // ok, it's a copy on the left for some reason
153154 val instancesPerState = currentCellDescriptors.instancesPerState
154155
155156 for (it in properties) {
@@ -181,6 +182,26 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
181182 }
182183
183184 val isArrayType = checkCreateForPossibleArray(callInstance, descriptor, serializedIteration)
185+ computedDescriptorsPerCell[cellId]!! .instancesPerState + = instancesPerState
186+
187+ // check for seen
188+ // for now it's O(c*n)
189+ if (serializedIteration.isEmpty()) {
190+ val processedVars = computedDescriptorsPerCell[cellId]!! .processedSerializedVarsToKProperties
191+ descriptor.forEach { (_, state) ->
192+ if (processedVars.containsKey(state)) {
193+ processedVars.entries.firstOrNull {
194+ val itValue = it.key
195+ if (itValue.value == state?.value && itValue.type == state?.value) {
196+ state?.fieldDescriptor?.put(itValue.type, itValue)
197+ true
198+ } else {
199+ false
200+ }
201+ }
202+ }
203+ }
204+ }
184205
185206 serializedIteration.forEach {
186207 val serializedVariablesState = it.value.serializedVariablesState
@@ -227,8 +248,12 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
227248 // update state with JVMFields
228249 it.value.jvmOnlyFields?.forEach { field ->
229250 serializedVariablesState.fieldDescriptor[field.name] = serializeVariableState(cellId, field.name, field, neededCallInstance)
230- instancesPerState[serializedVariablesState] = neededCallInstance
251+ val properInstance = serializedVariablesState.fieldDescriptor[field.name]
252+ instancesPerState[properInstance!! ] = neededCallInstance
253+ seenObjectsPerCell?.set(neededCallInstance!! , serializedVariablesState)
231254 }
255+ computedDescriptorsPerCell[cellId]!! .instancesPerState + = instancesPerState
256+ // computedDescriptorsPerCell[cellId]!!.processedSerializedVarsToJvmFields[serializedVariablesState] = it.value.jvmOnlyFields
232257 iterateThroughContainerMembers(
233258 cellId,
234259 neededCallInstance,
@@ -286,7 +311,7 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
286311 simpleTypeName.toString()
287312 }
288313
289- val serializedVariablesState = SerializedVariablesState (name, type, getProperString(value), isContainer)
314+ val serializedVariablesState = SerializedVariablesState (type, getProperString(value), isContainer)
290315
291316 return ProcessedSerializedVarsState (serializedVariablesState, membersProperties, jvmFields)
292317 }
@@ -324,6 +349,10 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
324349 false
325350 }
326351 }
352+
353+ companion object {
354+ const val serializationEnvProperty = " jupyter.serialization.enabled"
355+ }
327356}
328357
329358fun getProperString (value : Any? ): String {
0 commit comments