@@ -15,39 +15,16 @@ typealias FieldDescriptor = Map<String, SerializedVariablesState?>
15
15
typealias MutableFieldDescriptor = MutableMap <String , SerializedVariablesState ?>
16
16
typealias PropertiesData = Collection <KProperty1 <out Any , * >>
17
17
18
- data class ProcessedSerializedVarsState (
18
+ class ProcessedSerializedVarsState (
19
19
val serializedVariablesState : SerializedVariablesState ,
20
20
val propertiesData : PropertiesData ? ,
21
21
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
+ )
47
23
48
24
data 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(),
51
28
val instancesPerState : MutableMap <SerializedVariablesState , Any ?> = mutableMapOf()
52
29
)
53
30
@@ -68,7 +45,11 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
68
45
*/
69
46
private val computedDescriptorsPerCell: MutableMap <Int , ProcessedDescriptorsState > = mutableMapOf ()
70
47
48
+ private val isSerializationActive: Boolean = System .getProperty(serializationEnvProperty)?.toBooleanStrictOrNull() ? : true
49
+
71
50
fun serializeVariables (cellId : Int , variablesState : Map <String , VariableState >): Map <String , SerializedVariablesState > {
51
+ if (! isSerializationActive) return emptyMap()
52
+
72
53
if (seenObjectsPerCell.containsKey(cellId)) {
73
54
seenObjectsPerCell[cellId]!! .clear()
74
55
}
@@ -80,6 +61,8 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
80
61
}
81
62
82
63
fun doIncrementalSerialization (cellId : Int , propertyName : String , serializedVariablesState : SerializedVariablesState ): SerializedVariablesState {
64
+ if (! isSerializationActive) return serializedVariablesState
65
+
83
66
val cellDescriptors = computedDescriptorsPerCell[cellId] ? : return serializedVariablesState
84
67
return updateVariableState(cellId, propertyName, cellDescriptors, serializedVariablesState)
85
68
}
@@ -94,9 +77,8 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
94
77
evaluatedDescriptorsState : ProcessedDescriptorsState ,
95
78
serializedVariablesState : SerializedVariablesState
96
79
): SerializedVariablesState {
97
- // TODO: consider anonymous class as well and fix bug with state
98
80
val value = evaluatedDescriptorsState.instancesPerState[serializedVariablesState]
99
- val propertiesData = evaluatedDescriptorsState.processedSerializedVarsState [serializedVariablesState]
81
+ val propertiesData = evaluatedDescriptorsState.processedSerializedVarsToKProperties [serializedVariablesState]
100
82
if (propertiesData == null && value != null && (value::class .java.isArray || value::class .java.isMemberClass)) {
101
83
return serializeVariableState(cellId, propertyName, propertiesData, value, false )
102
84
}
@@ -108,21 +90,25 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
108
90
}
109
91
110
92
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 ()
112
94
return serializeVariableState(cellId, name, variableState.property, variableState.value, isOverride)
113
95
}
114
96
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 {
116
98
val processedData = createSerializeVariableState(name, getSimpleTypeNameFrom(property, value), value)
117
99
return doActualSerialization(cellId, processedData, value, isOverride)
118
100
}
119
101
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 {
121
103
val processedData = createSerializeVariableState(name, getSimpleTypeNameFrom(property, value), value)
122
104
return doActualSerialization(cellId, processedData, value, isOverride)
123
105
}
124
106
125
107
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
+
126
112
val serializedVersion = processedData.serializedVariablesState
127
113
128
114
seenObjectsPerCell.putIfAbsent(cellId, mutableMapOf ())
@@ -131,13 +117,27 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
131
117
computedDescriptorsPerCell[cellId] = ProcessedDescriptorsState ()
132
118
}
133
119
val currentCellDescriptors = computedDescriptorsPerCell[cellId]
134
- currentCellDescriptors!! .processedSerializedVarsState[serializedVersion] = processedData.propertiesData
120
+ currentCellDescriptors!! .processedSerializedVarsToKProperties[serializedVersion] = processedData.propertiesData
121
+ // currentCellDescriptors.processedSerializedVarsToJvmFields[serializedVersion] = processedData.jvmOnlyFields
135
122
136
123
if (value != null ) {
137
- seenObjectsPerCell[cellId]!! [ value] = serializedVersion
124
+ seenObjectsPerCell[cellId]!! .putIfAbsent( value, serializedVersion)
138
125
}
139
126
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
+ }
141
141
}
142
142
return processedData.serializedVariablesState
143
143
}
@@ -150,6 +150,7 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
150
150
seenObjectsPerCell.putIfAbsent(cellId, mutableMapOf ())
151
151
val seenObjectsPerCell = seenObjectsPerCell[cellId]
152
152
val currentCellDescriptors = computedDescriptorsPerCell[cellId]!!
153
+ // ok, it's a copy on the left for some reason
153
154
val instancesPerState = currentCellDescriptors.instancesPerState
154
155
155
156
for (it in properties) {
@@ -181,6 +182,26 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
181
182
}
182
183
183
184
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
+ }
184
205
185
206
serializedIteration.forEach {
186
207
val serializedVariablesState = it.value.serializedVariablesState
@@ -227,8 +248,12 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
227
248
// update state with JVMFields
228
249
it.value.jvmOnlyFields?.forEach { field ->
229
250
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)
231
254
}
255
+ computedDescriptorsPerCell[cellId]!! .instancesPerState + = instancesPerState
256
+ // computedDescriptorsPerCell[cellId]!!.processedSerializedVarsToJvmFields[serializedVariablesState] = it.value.jvmOnlyFields
232
257
iterateThroughContainerMembers(
233
258
cellId,
234
259
neededCallInstance,
@@ -286,7 +311,7 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
286
311
simpleTypeName.toString()
287
312
}
288
313
289
- val serializedVariablesState = SerializedVariablesState (name, type, getProperString(value), isContainer)
314
+ val serializedVariablesState = SerializedVariablesState (type, getProperString(value), isContainer)
290
315
291
316
return ProcessedSerializedVarsState (serializedVariablesState, membersProperties, jvmFields)
292
317
}
@@ -324,6 +349,10 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
324
349
false
325
350
}
326
351
}
352
+
353
+ companion object {
354
+ const val serializationEnvProperty = " jupyter.serialization.enabled"
355
+ }
327
356
}
328
357
329
358
fun getProperString (value : Any? ): String {
0 commit comments