-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimization Serialize Performance When Key Expired
- Loading branch information
1 parent
aa5ca5c
commit 635d92d
Showing
14 changed files
with
740 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 41 additions & 28 deletions
69
src/FasterKv.Cache.Core/Serializers/FasterKvSerializer.TValue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,74 @@ | ||
using System.Buffers; | ||
using FASTER.core; | ||
using FasterKv.Cache.Core.Abstractions; | ||
|
||
namespace FasterKv.Cache.Core.Serializers; | ||
|
||
internal sealed class FasterKvSerializer<TValue> : BinaryObjectSerializer<ValueWrapper<TValue>> | ||
{ | ||
private readonly ISystemClock _systemClock; | ||
private readonly IFasterKvCacheSerializer _serializer; | ||
|
||
public FasterKvSerializer(IFasterKvCacheSerializer serializer) | ||
public FasterKvSerializer(IFasterKvCacheSerializer serializer, ISystemClock systemClock) | ||
{ | ||
_serializer = serializer.ArgumentNotNull(); | ||
_systemClock = systemClock.ArgumentNotNull(); | ||
} | ||
|
||
public override void Deserialize(out ValueWrapper<TValue> obj) | ||
{ | ||
obj = new ValueWrapper<TValue>(); | ||
var etNullFlag = reader.ReadByte(); | ||
if (etNullFlag == 1) | ||
var flags = (FasterKvSerializerFlags)reader.ReadByte(); | ||
if ((flags & FasterKvSerializerFlags.HasExpiryTime) == FasterKvSerializerFlags.HasExpiryTime) | ||
{ | ||
obj.ExpiryTime = reader.ReadInt64(); | ||
} | ||
|
||
var dataLength = reader.ReadInt32(); | ||
var buffer = ArrayPool<byte>.Shared.Rent(dataLength); | ||
try | ||
if ((flags & FasterKvSerializerFlags.HasBody) == FasterKvSerializerFlags.HasBody) | ||
{ | ||
_ = reader.Read(buffer, 0, dataLength); | ||
obj.Data = _serializer.Deserialize<TValue>(buffer, dataLength); | ||
} | ||
finally | ||
{ | ||
ArrayPool<byte>.Shared.Return(buffer); | ||
var dataLength = reader.ReadInt32(); | ||
if (obj.HasExpired(_systemClock.NowUnixTimestamp())) | ||
{ | ||
reader.BaseStream.Position += dataLength; | ||
} | ||
else | ||
{ | ||
var buffer = ArrayPool<byte>.Shared.Rent(dataLength); | ||
try | ||
{ | ||
_ = reader.Read(buffer, 0, dataLength); | ||
obj.Data = _serializer.Deserialize<TValue>(buffer, dataLength); | ||
} | ||
finally | ||
{ | ||
ArrayPool<byte>.Shared.Return(buffer); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
public override void Serialize(ref ValueWrapper<TValue> obj) | ||
{ | ||
if (obj.ExpiryTime is null) | ||
{ | ||
// write Expiry Time is null flag | ||
writer.Write((byte)0); | ||
} | ||
else | ||
var flags = obj.GetFlags(_systemClock.NowUnixTimestamp()); | ||
writer.Write((byte)flags); | ||
if ((flags & FasterKvSerializerFlags.HasExpiryTime) == FasterKvSerializerFlags.HasExpiryTime) | ||
{ | ||
writer.Write((byte)1); | ||
writer.Write(obj.ExpiryTime.Value); | ||
writer.Write(obj.ExpiryTime!.Value); | ||
} | ||
|
||
var beforePos = writer.BaseStream.Position; | ||
var dataPos = writer.BaseStream.Position = writer.BaseStream.Position += sizeof(int); | ||
_serializer.Serialize(writer.BaseStream, obj.Data); | ||
var afterPos = writer.BaseStream.Position; | ||
if ((flags & FasterKvSerializerFlags.HasBody) == FasterKvSerializerFlags.HasBody) | ||
{ | ||
var beforePos = writer.BaseStream.Position; | ||
var dataPos = writer.BaseStream.Position = writer.BaseStream.Position += sizeof(int); | ||
_serializer.Serialize(writer.BaseStream, obj.Data); | ||
var afterPos = writer.BaseStream.Position; | ||
|
||
var length = (int)(afterPos - dataPos); | ||
writer.BaseStream.Position = beforePos; | ||
var length = (int)(afterPos - dataPos); | ||
writer.BaseStream.Position = beforePos; | ||
|
||
writer.Write(length); | ||
writer.BaseStream.Position = afterPos; | ||
writer.Write(length); | ||
writer.BaseStream.Position = afterPos; | ||
} | ||
} | ||
} |
68 changes: 45 additions & 23 deletions
68
src/FasterKv.Cache.Core/Serializers/FasterKvSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,75 @@ | ||
using System.Buffers; | ||
using System; | ||
using System.Buffers; | ||
using FASTER.core; | ||
using FasterKv.Cache.Core.Abstractions; | ||
|
||
namespace FasterKv.Cache.Core.Serializers; | ||
|
||
internal sealed class FasterKvSerializer : BinaryObjectSerializer<ValueWrapper> | ||
{ | ||
private readonly ISystemClock _systemClock; | ||
private readonly IFasterKvCacheSerializer _serializer; | ||
|
||
public FasterKvSerializer(IFasterKvCacheSerializer serializer) | ||
public FasterKvSerializer(IFasterKvCacheSerializer serializer, ISystemClock systemClock) | ||
{ | ||
_systemClock = systemClock.ArgumentNotNull(); | ||
_serializer = serializer.ArgumentNotNull(); | ||
} | ||
|
||
public override void Deserialize(out ValueWrapper obj) | ||
{ | ||
obj = new ValueWrapper(); | ||
var etNullFlag = reader.ReadByte(); | ||
if (etNullFlag == 1) | ||
var flags = (FasterKvSerializerFlags)reader.ReadByte(); | ||
if ((flags & FasterKvSerializerFlags.HasExpiryTime) == FasterKvSerializerFlags.HasExpiryTime) | ||
{ | ||
obj.ExpiryTime = reader.ReadInt64(); | ||
} | ||
|
||
obj.DataByteLength = reader.ReadInt32(); | ||
obj.DataBytes = ArrayPool<byte>.Shared.Rent(obj.DataByteLength); | ||
_ = reader.Read(obj.DataBytes, 0, obj.DataByteLength); | ||
if ((flags & FasterKvSerializerFlags.HasBody) == FasterKvSerializerFlags.HasBody) | ||
{ | ||
obj.DataByteLength = reader.ReadInt32(); | ||
if (obj.HasExpired(_systemClock.NowUnixTimestamp())) | ||
{ | ||
reader.BaseStream.Position += obj.DataByteLength; | ||
obj.DataByteLength = 0; | ||
} | ||
else | ||
{ | ||
obj.DataBytes = ArrayPool<byte>.Shared.Rent(obj.DataByteLength); | ||
_ = reader.Read(obj.DataBytes, 0, obj.DataByteLength); | ||
} | ||
} | ||
} | ||
|
||
public override void Serialize(ref ValueWrapper obj) | ||
{ | ||
if (obj.ExpiryTime is null) | ||
{ | ||
// write Expiry Time is null flag | ||
writer.Write((byte)0); | ||
} | ||
else | ||
var flags = obj.GetFlags(_systemClock.NowUnixTimestamp()); | ||
writer.Write((byte)flags); | ||
if ((flags & FasterKvSerializerFlags.HasExpiryTime) == FasterKvSerializerFlags.HasExpiryTime) | ||
{ | ||
writer.Write((byte)1); | ||
writer.Write(obj.ExpiryTime.Value); | ||
writer.Write(obj.ExpiryTime!.Value); | ||
} | ||
|
||
var beforePos = writer.BaseStream.Position; | ||
var dataPos = writer.BaseStream.Position = writer.BaseStream.Position += sizeof(int); | ||
_serializer.Serialize(writer.BaseStream, obj.Data); | ||
var afterPos = writer.BaseStream.Position; | ||
if ((flags & FasterKvSerializerFlags.HasBody) == FasterKvSerializerFlags.HasBody) | ||
{ | ||
var beforePos = writer.BaseStream.Position; | ||
var dataPos = writer.BaseStream.Position = writer.BaseStream.Position += sizeof(int); | ||
_serializer.Serialize(writer.BaseStream, obj.Data); | ||
var afterPos = writer.BaseStream.Position; | ||
|
||
var length = (int)(afterPos - dataPos); | ||
writer.BaseStream.Position = beforePos; | ||
var length = (int)(afterPos - dataPos); | ||
writer.BaseStream.Position = beforePos; | ||
|
||
writer.Write(length); | ||
writer.BaseStream.Position = afterPos; | ||
writer.Write(length); | ||
writer.BaseStream.Position = afterPos; | ||
} | ||
} | ||
} | ||
|
||
[Flags] | ||
internal enum FasterKvSerializerFlags : byte | ||
{ | ||
None = 0, | ||
HasExpiryTime = 1 << 0, | ||
HasBody = 1 << 1 | ||
} |
6 changes: 4 additions & 2 deletions
6
tests/FasterKv.Cache.Core.Tests/FasterKv.Cache.Core.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.