Skip to content

Commit

Permalink
StorageItem: optimize binary representation (#2379)
Browse files Browse the repository at this point in the history
* StorageItem: optimize binary representation

After #2351 StorageItem is just a thin wrapper for some byte array in its
essence so we can Use the same trick as was used for storage keys in #1566 and
avoid additional wrapping/unwrapping with VarBytes encoding.

* StorageItem: optimize out reader.BaseStream.Position

It's always 0.
  • Loading branch information
roman-khimov authored Mar 8, 2021
1 parent e6dadb2 commit 26c3adf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/neo/SmartContract/StorageItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public StorageItem Clone()

public void Deserialize(BinaryReader reader)
{
Value = reader.ReadVarBytes();
Value = reader.ReadBytes((int)(reader.BaseStream.Length));
}

public void FromReplica(StorageItem replica)
Expand Down Expand Up @@ -95,7 +95,7 @@ public void FromReplica(StorageItem replica)

public void Serialize(BinaryWriter writer)
{
writer.WriteVarBytes(Value);
writer.Write(Value);
}

public void Set(BigInteger integer)
Expand Down
4 changes: 2 additions & 2 deletions tests/neo.UnitTests/Ledger/UT_StorageItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void Clone()
[TestMethod]
public void Deserialize()
{
byte[] data = new byte[] { 10, 66, 32, 32, 32, 32, 32, 32, 32, 32, 32, 0 };
byte[] data = new byte[] { 66, 32, 32, 32, 32, 32, 32, 32, 32, 32 };
int index = 0;
using (MemoryStream ms = new MemoryStream(data, index, data.Length - index, false))
{
Expand Down Expand Up @@ -96,7 +96,7 @@ public void Serialize()
}
}

byte[] requiredData = new byte[] { 10, 66, 32, 32, 32, 32, 32, 32, 32, 32, 32 };
byte[] requiredData = new byte[] { 66, 32, 32, 32, 32, 32, 32, 32, 32, 32 };

data.Length.Should().Be(requiredData.Length);
for (int i = 0; i < requiredData.Length; i++)
Expand Down

0 comments on commit 26c3adf

Please sign in to comment.