Skip to content

Pre-hash rows and add SmallHashSets #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion examples~/regression-tests/client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ void ValidateBTreeIndexes(IRemoteDbContext conn)
Log.Debug("Checking indexes...");
foreach (var data in conn.Db.ExampleData.Iter())
{
Debug.Assert(conn.Db.ExampleData.Indexed.Filter(data.Id).Contains(data));
Log.Debug($"{data}: [{string.Join(", ", conn.Db.ExampleData.Indexed.Filter(data.Indexed))}]");
Debug.Assert(conn.Db.ExampleData.Indexed.Filter(data.Indexed).Contains(data));
}
var outOfIndex = conn.Db.ExampleData.Iter().ToHashSet();

Expand All @@ -107,10 +108,38 @@ void OnSubscriptionApplied(SubscriptionEventContext context)
waiting++;
context.Reducers.Add(1, 1);

Log.Debug("Calling Add");
waiting++;
context.Reducers.Add(2, 1);

Log.Debug("Calling Add");
waiting++;
context.Reducers.Add(3, 1);

Log.Debug("Calling Add");
waiting++;
context.Reducers.Add(4, 2);

Log.Debug("Calling Add");
waiting++;
context.Reducers.Add(6, 2);

Log.Debug("Calling Add");
waiting++;
context.Reducers.Add(5, 2);

Log.Debug("Calling Delete");
waiting++;
context.Reducers.Delete(1);

Log.Debug("Calling Delete");
waiting++;
context.Reducers.Delete(2);

Log.Debug("Calling Delete");
waiting++;
context.Reducers.Delete(3);

Log.Debug("Calling Add");
waiting++;
context.Reducers.Add(1, 1);
Expand Down
192 changes: 192 additions & 0 deletions src/SmallHashSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;

#nullable enable

/// <summary>
/// A hashset optimized to store small numbers of values of type T.
/// Used because many of the hash sets in our BTreeIndexes store only one value.
/// </summary>
/// <typeparam name="T"></typeparam>
internal struct SmallHashSet<T, EQ> : IEnumerable<T>
where T : struct
where EQ : IEqualityComparer<T>, new()
{
static EQ DefaultEqualityComparer = new();

// Invariant: zero or one of the following is not null.
T? Value;
HashSet<T>? Values;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(T newValue)
{
if (Values == null)
{
if (Value == null)
{
Value = newValue;
}
else
{
Values = new(2, DefaultEqualityComparer)
{
newValue,
Value.Value
};
Value = null;
}
}
else
{
Values.Add(newValue);
}
}

public void Remove(T remValue)
{
if (Value != null && DefaultEqualityComparer.Equals(Value.Value, remValue))
{
Value = null;
}
if (Values != null && Values.Contains(remValue))
{
Values.Remove(remValue);
// Do not try to go back to single-row state.
// We might as well keep the allocation around if this set has needed to store multiple values before.
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(T value)
{
if (Values != null)
{
return Values.Contains(value);
}
if (Value != null)
{
return DefaultEqualityComparer.Equals(Value.Value, value);
}
return false;
}

public int Count
{
get
{
if (Value != null)
{
return 1;
}
else if (Values != null)
{
return Values.Count;
}
return 0;
}

}

public IEnumerator<T> GetEnumerator()
{
if (Value != null)
{
return new SingleElementEnumerator<T>(Value.Value);
}
else if (Values != null)
{
return Values.GetEnumerator();
}
else
{
return new NoElementEnumerator<T>();
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

/// <summary>
/// This is a silly object.
/// </summary>
/// <typeparam name="T"></typeparam>
internal struct SingleElementEnumerator<T> : IEnumerator<T>
where T : struct
{
T value;
enum State
{
Unstarted,
Started,
finished
}

State state;

public SingleElementEnumerator(T value)
{
this.value = value;
state = State.Unstarted;
}

public T Current => value;

object IEnumerator.Current => Current;

public void Dispose()
{
}

public bool MoveNext()
{
if (state == State.Unstarted)
{
state = State.Started;
return true;
}
else if (state == State.Started)
{
state = State.finished;
return false;
}
return false;
}

public void Reset()
{
state = State.Started;
}
}

/// <summary>
/// This is a very silly object.
/// </summary>
/// <typeparam name="T"></typeparam>
internal struct NoElementEnumerator<T> : IEnumerator<T>
where T : struct
{
public T Current => new();

object IEnumerator.Current => Current;

public void Dispose()
{
}

public bool MoveNext()
{
return false;
}

public void Reset()
{
}
}

#nullable disable
7 changes: 7 additions & 0 deletions src/SmallHashSet.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/SpacetimeDBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ struct ProcessedDatabaseUpdate
// Map: table handles -> (primary key -> IStructuralReadWrite).
// If a particular table has no primary key, the "primary key" is just the row itself.
// This is valid because any [SpacetimeDB.Type] automatically has a correct Equals and HashSet implementation.
public Dictionary<IRemoteTableHandle, MultiDictionaryDelta<object, IStructuralReadWrite>> Updates;
public Dictionary<IRemoteTableHandle, MultiDictionaryDelta<object, PreHashedRow>> Updates;

// Can't override the default constructor. Make sure you use this one!
public static ProcessedDatabaseUpdate New()
Expand All @@ -225,11 +225,11 @@ public static ProcessedDatabaseUpdate New()
return result;
}

public MultiDictionaryDelta<object, IStructuralReadWrite> DeltaForTable(IRemoteTableHandle table)
public MultiDictionaryDelta<object, PreHashedRow> DeltaForTable(IRemoteTableHandle table)
{
if (!Updates.TryGetValue(table, out var delta))
{
delta = new MultiDictionaryDelta<object, IStructuralReadWrite>(EqualityComparer<object>.Default, EqualityComparer<object>.Default);
delta = new MultiDictionaryDelta<object, PreHashedRow>(EqualityComparer<object>.Default, PreHashedRowComparer.Default);
Updates[table] = delta;
}

Expand Down Expand Up @@ -266,13 +266,13 @@ struct ProcessedMessage
/// <param name="reader"></param>
/// <param name="primaryKey"></param>
/// <returns></returns>
static IStructuralReadWrite Decode(IRemoteTableHandle table, BinaryReader reader, out object primaryKey)
static PreHashedRow Decode(IRemoteTableHandle table, BinaryReader reader, out object primaryKey)
{
var obj = table.DecodeValue(reader);

// TODO(1.1): we should exhaustively check that GenericEqualityComparer works
// for all types that are allowed to be primary keys.
var primaryKey_ = table.GetPrimaryKey(obj);
var primaryKey_ = table.GetPrimaryKey(obj.Row);
primaryKey_ ??= obj;
primaryKey = primaryKey_;

Expand Down
Loading