Skip to content

Commit

Permalink
mandatory header, bit more efficient encoders
Browse files Browse the repository at this point in the history
  • Loading branch information
pshirshov committed Dec 18, 2024
1 parent 5d68598 commit f11ac81
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
23 changes: 10 additions & 13 deletions src/main/resources/baboon-runtime/cs/BaboonRuntimeShared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ public interface IBaboonCodecData {
public class BaboonCodecContext {
public BaboonCodecContext(bool useIndexes)
{
UseIndexes = useIndexes;
UseIndices = useIndexes;
}

public Boolean UseIndexes { get; }
public Boolean UseIndices { get; }

public static BaboonCodecContext Indexed { get; } = new BaboonCodecContext(true);
public static BaboonCodecContext Compact { get; } = new BaboonCodecContext(false);
Expand Down Expand Up @@ -152,10 +152,12 @@ public interface IBaboonBinCodecIndexed

List<BaboonIndexEntry> ReadIndex(BaboonCodecContext ctx, BinaryReader wire)
{
var header = wire.ReadByte();
var isIndexed = (header & 0b0000001) != 0;
var result = new List<BaboonIndexEntry>();
uint prevoffset = 0;
uint prevlen = 0;
if (ctx.UseIndexes)
if (isIndexed)
{
var left = IndexElementsCount(ctx);
while (left > 0)
Expand All @@ -177,21 +179,16 @@ List<BaboonIndexEntry> ReadIndex(BaboonCodecContext ctx, BinaryReader wire)

public interface IBaboonBinCodec<T> : IBaboonStreamCodec<T, BinaryWriter, BinaryReader>
{
void EncodeMessage(BaboonCodecContext ctx, BinaryWriter writer, T instance)
void EncodeIndexed(BinaryWriter writer, T instance)
{
var header = 0b0000000;
if (ctx.UseIndexes)
{
header |= 0b0000001;
}
BaboonCodecContext ctx = BaboonCodecContext.Indexed;
Encode(ctx, writer, instance);
}

T DecodeMessage(BinaryReader wire)
void EncodeCompact(BinaryWriter writer, T instance)
{
var header = wire.ReadByte();
var ctx = new BaboonCodecContext((header & 0b0000001) != 0);
return Decode(ctx, wire);
BaboonCodecContext ctx = BaboonCodecContext.Compact;
Encode(ctx, writer, instance);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ object CSCodecTestsTranslator {
| $codecName.Instance.Encode(context, binaryWriter, $fieldName);
| }
| writeMemoryStream.Flush();
| var $serialized = writeMemoryStream.GetBuffer();
| var $serialized = writeMemoryStream.ToArray();
| var $readStream = new MemoryStream($serialized);
| var $binaryReader = new BinaryReader($readStream);
| var $decoded = $codecName.Instance.Decode(context, $binaryReader);
Expand All @@ -256,7 +256,7 @@ object CSCodecTestsTranslator {
| }
| writeMemoryStream.Flush();
|
| var $serialized = writeMemoryStream.GetBuffer();
| var $serialized = writeMemoryStream.ToArray();
| var readMemoryStream = new MemoryStream($serialized);
| var binaryReader = new BinaryReader(readMemoryStream);
| var $decoded = $codecName.Instance.Decode(context, binaryReader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,18 +295,26 @@ class CSUEBACodecGenerator(trans: CSTypeTranslator,
val fields = fieldsOf(domain, d, evo)

val fenc =
q"""if (ctx.UseIndexes)
q"""byte header = 0b0000000;
|
|if (ctx.UseIndices)
|{
| header |= 0b0000001;
| writer.Write(header);
| using ($memoryStream writeMemoryStream = new $memoryStream())
| {
| using ($binaryWriter fakeWriter = new $binaryWriter(writeMemoryStream))
| {
| ${fields.map(_._3).join("\n").shift(12).trim}
| }
| writeMemoryStream.Flush();
| writer.Write(writeMemoryStream.ToArray());
| }
|} else {
| writer.Write(header);
| ${fields.map(_._1).join(";\n").shift(4).trim};
|}
|
|${fields.map(_._1).join(";\n")};""".stripMargin
|""".stripMargin

val fdec =
dtoDec(name, fields.map { case (a, b, _) => (a, b) })
Expand Down Expand Up @@ -350,7 +358,7 @@ class CSUEBACodecGenerator(trans: CSTypeTranslator,
fields: List[(TextTree[CSValue], TextTree[CSValue])]) = {
q"""var index = ((IBaboonBinCodecIndexed)this).ReadIndex(ctx, wire);
|
|if (ctx.UseIndexes)
|if (ctx.UseIndices)
|{
| ${debug}.Assert(index.Count == IndexElementsCount(ctx));
|}
Expand All @@ -372,22 +380,19 @@ class CSUEBACodecGenerator(trans: CSTypeTranslator,

val w = if (isVarlen) {
q"""{
| // ${f.toString}
| var before = (uint)fakeWriter.BaseStream.Position;
| writer.Write(before);
| ${fakeEnc.shift(4).trim};
| var after = (uint)fakeWriter.BaseStream.Position;
| writer.Write(after - before);
| //Console.WriteLine($$"${d.id.toString} {before} {after} {after - before}");
| fakeWriter.Flush();
|}""".stripMargin
} else {
q"""$fakeEnc;
|fakeWriter.Flush();""".stripMargin
q"""$fakeEnc;""".stripMargin
}
val vle = q"""// ${f.toString}
|$w""".stripMargin

(enc, dec, vle)
(enc, dec, w)
}
}

Expand Down

0 comments on commit f11ac81

Please sign in to comment.