Skip to content

Commit

Permalink
Improved Storage in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Sep 1, 2024
1 parent ad39801 commit 8b1595f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
6 changes: 4 additions & 2 deletions YantraJS.Core/Core/Storage/SAUint32Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enum NodeState : byte

static Node Empty = new Node();

[DebuggerDisplay("{Key}={Value}")]
struct Node
{
public bool HasValue
Expand Down Expand Up @@ -210,7 +211,7 @@ private ref Node GetNode(uint originalKey, bool create = false)

if (this.roots.IsEmpty) {
if (!create) {
return ref node;
return ref Empty;
}
// extend...
this.roots = this.nodes.Allocate(4);
Expand All @@ -228,7 +229,7 @@ private ref Node GetNode(uint originalKey, bool create = false)
for (long key = originalKey; key > 0; key >>= 2)
{
var index = (int)(key & 0x3);
node = ref this.nodes[leaves, (int)index];
node = ref this.nodes[leaves, index];
if (node.Key == originalKey) {
if (create)
{
Expand Down Expand Up @@ -259,6 +260,7 @@ private ref Node GetNode(uint originalKey, bool create = false)
ref var newChild = ref GetNode(oldKey, true);
newChild.Key = oldKey;
newChild.Value = oldValue;
newChild.Children = oldChild;
newChild.State |= NodeState.HasValue;
// this is case when array is resized
// and we still might have reference to old node
Expand Down
11 changes: 3 additions & 8 deletions YantraJS.Core/Core/Storage/VirtualMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public VirtualMemory()
public VirtualArray Allocate(int length)
{
var max = this.last + length;
if (this.nodes == null || this.nodes.Length <= length)
if (this.nodes == null || this.nodes.Length <= max)
{
// we need to resize...
var capacity = this.last * 2;
if (capacity <= length)
if (capacity <= max)
{
capacity = ((length / 16)+ 1) * 16;
capacity = ((max / 16)+ 1) * 16;
}
this.SetCapacity(capacity);
}
Expand Down Expand Up @@ -68,11 +68,6 @@ public readonly struct VirtualArray
public readonly int Offset;
public readonly int Length;

public VirtualArray()
{
this.Offset = -1;
}

public VirtualArray(int offset, int length)
{
this.Offset = offset;
Expand Down

0 comments on commit 8b1595f

Please sign in to comment.