refs #35 hack to avoid cache clush for encodeAny and encode, double caching same object conflict. to refactor!
This commit is contained in:
		
							parent
							
								
									a9f65bdbe3
								
							
						
					
					
						commit
						405ff2ec2b
					
				@ -81,17 +81,24 @@ data class ObjString(val value: String) : Obj() {
 | 
			
		||||
        return value == other.value
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override suspend fun lynonType(): LynonType = LynonType.String
 | 
			
		||||
 | 
			
		||||
    override suspend fun serialize(scope: Scope, encoder: LynonEncoder, lynonType: LynonType?) {
 | 
			
		||||
        if( lynonType == null )
 | 
			
		||||
        encoder.encodeCached(this) { encoder.encodeBinaryData(value.encodeToByteArray()) }
 | 
			
		||||
        else
 | 
			
		||||
            encoder.encodeBinaryData(value.encodeToByteArray())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    companion object {
 | 
			
		||||
        val type = object : ObjClass("String") {
 | 
			
		||||
            override suspend fun deserialize(scope: Scope, decoder: LynonDecoder, lynonType: LynonType?): Obj =
 | 
			
		||||
                ObjString(
 | 
			
		||||
                    decoder.unpackBinaryData().decodeToString()
 | 
			
		||||
//                        ?: scope.raiseError("unexpected end of data")
 | 
			
		||||
                )
 | 
			
		||||
                if( lynonType == null )
 | 
			
		||||
                    decoder.decodeCached {
 | 
			
		||||
                        ObjString(decoder.unpackBinaryData().decodeToString())
 | 
			
		||||
                    }
 | 
			
		||||
                else ObjString(decoder.unpackBinaryData().decodeToString())
 | 
			
		||||
        }.apply {
 | 
			
		||||
            addFn("toInt") {
 | 
			
		||||
                ObjInt(thisAs<ObjString>().value.toLong())
 | 
			
		||||
 | 
			
		||||
@ -41,7 +41,7 @@ open class LynonDecoder(val bin: BitInput, val settings: LynonSettings = LynonSe
 | 
			
		||||
 | 
			
		||||
    // todo: rewrite/remove?
 | 
			
		||||
    suspend fun unpackObject(scope: Scope, type: ObjClass): Obj {
 | 
			
		||||
        return decodeCached { type.deserialize(scope, this, null) }
 | 
			
		||||
        return type.deserialize(scope, this, null)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun unpackBinaryData(): ByteArray = bin.decompress()
 | 
			
		||||
 | 
			
		||||
@ -28,21 +28,22 @@ open class LynonEncoder(val bout: BitOutput,val settings: LynonSettings = LynonS
 | 
			
		||||
    suspend fun encodeCached(item: Any, packer: suspend LynonEncoder.() -> Unit) {
 | 
			
		||||
 | 
			
		||||
        suspend fun serializeAndCache(key: Any = item) {
 | 
			
		||||
            cache[key]?.let { cacheId ->
 | 
			
		||||
                val size = sizeInBits(cache.size)
 | 
			
		||||
                bout.putBit(1)
 | 
			
		||||
                bout.putBits(cacheId.toULong(), size)
 | 
			
		||||
            } ?: run {
 | 
			
		||||
                bout.putBit(0)
 | 
			
		||||
                if (settings.shouldCache(item))
 | 
			
		||||
                    cache[key] = cache.size
 | 
			
		||||
                packer()
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        when (item) {
 | 
			
		||||
            is Obj -> cache[item]?.let { cacheId ->
 | 
			
		||||
                val size = sizeInBits(cache.size)
 | 
			
		||||
                bout.putBit(1)
 | 
			
		||||
                bout.putBits(cacheId.toULong(), size)
 | 
			
		||||
            } ?: serializeAndCache()
 | 
			
		||||
 | 
			
		||||
            is ByteArray -> serializeAndCache(ByteChunk(item.asUByteArray()))
 | 
			
		||||
            is UByteArray -> serializeAndCache(ByteChunk(item))
 | 
			
		||||
            else -> serializeAndCache(item)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -65,10 +66,8 @@ open class LynonEncoder(val bout: BitOutput,val settings: LynonSettings = LynonS
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    suspend fun encodeObj(scope: Scope, obj: Obj) {
 | 
			
		||||
        encodeCached(obj) {
 | 
			
		||||
            obj.serialize(scope, this, null)
 | 
			
		||||
    }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun encodeBinaryData(data: ByteArray) {
 | 
			
		||||
        bout.compress(data)
 | 
			
		||||
 | 
			
		||||
@ -328,6 +328,7 @@ class LynonTests {
 | 
			
		||||
            testEncode(Instant.now().truncateToMicrosecond())
 | 
			
		||||
 | 
			
		||||
            testEncode("Hello, world".encodeUtf8())
 | 
			
		||||
            testEncode("Hello, world")
 | 
			
		||||
            
 | 
			
		||||
        """.trimIndent())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user