Skip to content

Commit

Permalink
Merge pull request #93 from yantrajs/feature/sa-trie
Browse files Browse the repository at this point in the history
Performance improved by removal of nested arrays
  • Loading branch information
ackava committed Oct 29, 2023
2 parents ede4eff + b5ef385 commit 67999ee
Show file tree
Hide file tree
Showing 16 changed files with 958 additions and 684 deletions.
16 changes: 7 additions & 9 deletions YantraJS.Core.Tests/Maps/BinaryMapEnumerateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,12 @@ public void BigMap()
[TestMethod]
public void IntMap()
{
var a = new UInt32Map<int>();
var a = new SAUint32Map<int>();

#pragma warning disable CS0618 // Type or member is obsolete
a[1] = 1;
a[3] = 2;
a[2] = 3;
a[4] = 4;
#pragma warning restore CS0618 // Type or member is obsolete
a.Put(1) = 1;
a.Put(3) = 2;
a.Put(2) = 3;
a.Put(4) = 4;

var all = a.AllValues().ToList();
Assert.AreEqual(4, all.Count);
Expand Down Expand Up @@ -114,7 +112,7 @@ public void IntMap()
[TestMethod]
public void UIntMapTest()
{
var map = new UInt32Map<uint>();
var map = new SAUint32Map<uint>();

int max = 100;
for (int i = max; i >= 0; i--)
Expand All @@ -130,7 +128,7 @@ public void UIntMapTest()
}


map = new UInt32Map<uint>();
map = new SAUint32Map<uint>();

for (int i = 0; i < max; i++)
{
Expand Down
8 changes: 3 additions & 5 deletions YantraJS.Core.Tests/Maps/BinaryMapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@ public void CharTest()
[TestMethod]
public void IntTest()
{
var im = new UInt32Map<string>();
var im = new SAUint32Map<string>();

var i1 = (uint)4;
var i2 = (uint)687;

#pragma warning disable CS0618 // Type or member is obsolete
im[i1] = "a";
im[i2] = "b";
#pragma warning restore CS0618 // Type or member is obsolete
im.Put(i1) = "a";
im.Put(i2) = "b";

Assert.AreEqual("a", im[i1]);
Assert.AreEqual("b", im[i2]);
Expand Down
94 changes: 94 additions & 0 deletions YantraJS.Core.Tests/Maps/SATrieMapTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using YantraJS.Core;
using YantraJS.Core.Core.Storage;

namespace YantraJS.Tests
{
[TestClass]
public class SATrieMapTests
{
[TestMethod]
public void CharTest()
{
var tm = new StringMap<string>();

var i1 = "k1";
var i2 = "k2";

#pragma warning disable CS0618 // Type or member is obsolete
tm[i1] = "a";
tm[i2] = "b";
#pragma warning restore CS0618 // Type or member is obsolete

Assert.AreEqual("a", tm[i1]);
Assert.AreEqual("b", tm[i2]);
Assert.IsNull(tm["k3"]);
Assert.IsNull(tm["k"]);



tm.RemoveAt(i2);

Assert.IsNull(tm["k2"]);
}

[TestMethod]
public void IntTest()
{
var im = new SAUint32Map<string>();

var i1 = (uint)4;
var i2 = (uint)687;

im.Put(i1) = "a";
im.Put(i2) = "b";

Assert.AreEqual("a", im[i1]);
Assert.AreEqual("b", im[i2]);
Assert.IsNull(im[680]);
Assert.IsNull(im[3]);



im.RemoveAt(i2);

Assert.IsNull(im[i2]);
}

[TestMethod]
public void UIntMapTest()
{
var map = new SAUint32Map<uint>();

int max = 100;
for (int i = max; i >= 0; i--)
{
map.Save((uint)i, (uint)i);
}

for (uint i = 0; i < max; i++)
{
if (!map.TryGetValue(i, out var value))
Assert.Fail($"Could not get value for {i}");
Assert.AreEqual(i, value);
}


map = new SAUint32Map<uint>();

for (int i = 0; i < max; i++)
{
map.Save((uint)i, (uint)i);
}

for (uint i = 0; i < max; i++)
{
if (!map.TryGetValue(i, out var value))
Assert.Fail();
Assert.AreEqual(i, value);
}


}
}
}
4 changes: 2 additions & 2 deletions YantraJS.Core/Core/Events/EventTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public EventTarget(in Arguments a): this(a.NewPrototype)
private static ConcurrentNameMap eventNames = new ConcurrentNameMap();


private UInt32Map<List<DomEventHandler>> captureHandlers;
private UInt32Map<List<DomEventHandler>> handlers;
private SAUint32Map<List<DomEventHandler>> captureHandlers;
private SAUint32Map<List<DomEventHandler>> handlers;

[JSExport]
public virtual JSValue DispatchEvent(Event e) {
Expand Down
2 changes: 1 addition & 1 deletion YantraJS.Core/Core/JSContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public void Dispose()
// }
//}

UInt32Map<JSVariable> globalVars = new UInt32Map<JSVariable>();
SAUint32Map<JSVariable> globalVars = new SAUint32Map<JSVariable>();

internal JSValue Register(JSVariable variable)
{
Expand Down
12 changes: 6 additions & 6 deletions YantraJS.Core/Core/JSPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class JSPrototype

internal class JSPropertySet
{
internal UInt32Map<(JSProperty property, JSPrototype owner)> properties;
internal UInt32Map<(JSProperty property, JSPrototype owner)> elements;
internal UInt32Map<(JSProperty property, JSPrototype owner)> symbols;
internal SAUint32Map<(JSProperty property, JSPrototype owner)> properties;
internal SAUint32Map<(JSProperty property, JSPrototype owner)> elements;
internal SAUint32Map<(JSProperty property, JSPrototype owner)> symbols;

internal Sequence<KeyString> stringKeys = new Sequence<KeyString>();
internal Sequence<uint> uintKeys = new Sequence<uint>();
Expand All @@ -42,9 +42,9 @@ private void Build()
{
if (!this.dirty)
return;
ps.properties = new UInt32Map<(JSProperty, JSPrototype)>();
ps.elements = new UInt32Map<(JSProperty, JSPrototype)>();
ps.symbols = new UInt32Map<(JSProperty, JSPrototype)>();
ps.properties = new SAUint32Map<(JSProperty, JSPrototype)>();
ps.elements = new SAUint32Map<(JSProperty, JSPrototype)>();
ps.symbols = new SAUint32Map<(JSProperty, JSPrototype)>();

Build(ps, this);
dirty = false;
Expand Down
4 changes: 2 additions & 2 deletions YantraJS.Core/Core/Object/JSObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal void Dirty()
internal event PropertyChangedEventHandler PropertyChanged;
private ElementArray elements;
private PropertySequence ownProperties;
private UInt32Map<JSProperty> symbols;
private SAUint32Map<JSProperty> symbols;
private long? uid;

private static long NextID = 0;
Expand Down Expand Up @@ -138,7 +138,7 @@ public override JSValue GetOwnProperty(uint name)
return ref elements;
}

public ref UInt32Map<JSProperty> GetSymbols()
public ref SAUint32Map<JSProperty> GetSymbols()
{
return ref symbols;
}
Expand Down
2 changes: 1 addition & 1 deletion YantraJS.Core/Core/Storage/ConcurrentStringMap`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public T GetOrCreate(in StringSpan key, Func<StringSpan, T> value)
public class ConcurrentUInt32Map<T>
{

private UInt32Map<T> Map;
private SAUint32Map<T> Map;

private ReaderWriterLockSlim lockSlim;

Expand Down
34 changes: 0 additions & 34 deletions YantraJS.Core/Core/Storage/IBitTrie.cs

This file was deleted.

6 changes: 3 additions & 3 deletions YantraJS.Core/Core/Storage/PropertySequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public PropertyEnumerator GetEnumerator(bool showEnumerableOnly = true)

public struct PropertyEnumerator
{
private UInt32Map<JSObjectProperty> map;
private SAUint32Map<JSObjectProperty> map;
private readonly uint tail;
private readonly bool showEnumerableOnly;
private uint start;
Expand Down Expand Up @@ -132,7 +132,7 @@ public bool MoveNextKey(out KeyString key)
public struct ValueEnumerator
{
public JSObject target;
private UInt32Map<JSObjectProperty> map;
private SAUint32Map<JSObjectProperty> map;
private uint start;
readonly bool showEnumerableOnly;
public ValueEnumerator(JSObject target, bool showEnumerableOnly)
Expand Down Expand Up @@ -227,7 +227,7 @@ public bool MoveNextProperty(out JSProperty value, out KeyString key)
#endregion


private UInt32Map<JSObjectProperty> map;
private SAUint32Map<JSObjectProperty> map;
private uint head;
private uint tail;

Expand Down
Loading

0 comments on commit 67999ee

Please sign in to comment.