-
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.
Merge pull request #1 from InCerryGit/ls_opt
Optimization performance
- Loading branch information
Showing
21 changed files
with
272 additions
and
121 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
8 changes: 5 additions & 3 deletions
8
src/FasterKv.Cache.Core/Abstractions/IFasterKvCacheSerializer.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,8 +1,10 @@ | ||
namespace FasterKv.Cache.Core; | ||
using System.IO; | ||
|
||
namespace FasterKv.Cache.Core; | ||
|
||
public interface IFasterKvCacheSerializer | ||
{ | ||
string Name { get;} | ||
byte[] Serialize<TValue>(TValue data); | ||
TValue? Deserialize<TValue>(byte[] serializerData); | ||
void Serialize<TValue>(Stream stream, TValue data); | ||
TValue? Deserialize<TValue>(byte[] bytes, int length); | ||
} |
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
1 change: 1 addition & 0 deletions
1
src/FasterKv.Cache.Core/Configurations/ServiceCollectionExtensions.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
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
80 changes: 38 additions & 42 deletions
80
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,65 +1,61 @@ | ||
using System; | ||
using System.IO; | ||
using System.Buffers; | ||
using FASTER.core; | ||
using MessagePack; | ||
|
||
namespace FasterKv.Cache.Core.Serializers; | ||
|
||
public class FasterKvSerializer<TValue> : IObjectSerializer<ValueWrapper<TValue>> | ||
internal sealed class FasterKvSerializer<TValue> : BinaryObjectSerializer<ValueWrapper<TValue>> | ||
{ | ||
private Stream? _read; | ||
private Stream? _write; | ||
private readonly IFasterKvCacheSerializer _serializer; | ||
|
||
public FasterKvSerializer(IFasterKvCacheSerializer serializer) | ||
{ | ||
_serializer = serializer.ArgumentNotNull(); | ||
} | ||
|
||
public void BeginSerialize(Stream stream) | ||
public override void Deserialize(out ValueWrapper<TValue> obj) | ||
{ | ||
_write = stream; | ||
} | ||
|
||
public void Serialize(ref ValueWrapper<TValue> obj) | ||
{ | ||
var pack = new DataPack | ||
obj = new ValueWrapper<TValue>(); | ||
var etNullFlag = reader.ReadByte(); | ||
if (etNullFlag == 1) | ||
{ | ||
ExpiryTime = obj.ExpiryTime, | ||
SerializerData = _serializer.Serialize(obj.Data) | ||
}; | ||
MessagePackSerializer.Serialize(_write, pack); | ||
} | ||
obj.ExpiryTime = reader.ReadInt64(); | ||
} | ||
|
||
public void BeginDeserialize(Stream stream) | ||
{ | ||
_read = stream; | ||
var dataLength = reader.ReadInt32(); | ||
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 void Deserialize(out ValueWrapper<TValue> obj) | ||
public override void Serialize(ref ValueWrapper<TValue> obj) | ||
{ | ||
var pack = MessagePackSerializer.Deserialize<DataPack>(_read); | ||
obj = new ValueWrapper<TValue>(_serializer.Deserialize<TValue>(pack.SerializerData), pack.ExpiryTime); | ||
} | ||
if (obj.ExpiryTime is null) | ||
{ | ||
// write Expiry Time is null flag | ||
writer.Write((byte)0); | ||
} | ||
else | ||
{ | ||
writer.Write((byte)1); | ||
writer.Write(obj.ExpiryTime.Value); | ||
} | ||
|
||
public void EndSerialize() | ||
{ | ||
} | ||
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; | ||
|
||
public void EndDeserialize() | ||
{ | ||
} | ||
|
||
[MessagePackObject] | ||
public struct DataPack | ||
{ | ||
/// <summary> | ||
/// Expiry Time | ||
/// </summary> | ||
[Key(0)] | ||
public DateTimeOffset? ExpiryTime { get; set; } | ||
var length = (int)(afterPos - dataPos); | ||
writer.BaseStream.Position = beforePos; | ||
|
||
[Key(1)] | ||
public byte[] SerializerData { get; set; } | ||
writer.Write(length); | ||
writer.BaseStream.Position = afterPos; | ||
} | ||
} |
Oops, something went wrong.