Skip to content

Commit

Permalink
Additional 10% perf on btree inserts
Browse files Browse the repository at this point in the history
  • Loading branch information
dasatomic committed Dec 7, 2021
1 parent 10745c2 commit eac10e2
Show file tree
Hide file tree
Showing 43 changed files with 350 additions and 37 deletions.
10 changes: 5 additions & 5 deletions DataStructures/BTreeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public async Task Add(RowHolder item, ITransaction tran)
{
this.rowUniqueCheckPageLevel(currPage, itemToInsert, tran);

int pos = currPage.InsertOrdered(itemToInsert, tran, this.btreeColumnTypes, this.indexComparer);
int pos = currPage.InsertOrdered(itemToInsert, tran, this.btreeColumnTypes, this.indexPosition);
Debug.Assert(pos >= 0);

if (debugPrintPage != null)
Expand All @@ -209,12 +209,12 @@ public async Task Add(RowHolder item, ITransaction tran)
if (compareResult < 0)
{
// left.
int pos = currPage.InsertOrdered(itemToInsert, tran, this.btreeColumnTypes, this.indexComparer);
int pos = currPage.InsertOrdered(itemToInsert, tran, this.btreeColumnTypes, this.indexPosition);
}
else
{
// right.
int pos = newPageForSplit.InsertOrdered(itemToInsert, tran, this.btreeColumnTypes, this.indexComparer);
int pos = newPageForSplit.InsertOrdered(itemToInsert, tran, this.btreeColumnTypes, this.indexPosition);
}

insertFinished = true;
Expand Down Expand Up @@ -421,7 +421,7 @@ private async Task<RowHolder> SplitBtreePage(MixedPage currPage, MixedPage newPa

if (prevPage != null)
{
int pos = prevPage.InsertOrdered(rowHolderForSplit, tran, this.btreeColumnTypes, this.indexComparer);
int pos = prevPage.InsertOrdered(rowHolderForSplit, tran, this.btreeColumnTypes, this.indexPosition);

// We know that there will be enough space since we proactivly clean parent nodes.
Debug.Assert(pos >= 0);
Expand All @@ -435,7 +435,7 @@ private async Task<RowHolder> SplitBtreePage(MixedPage currPage, MixedPage newPa
MixedPage newRoot = await pageAllocator.AllocateMixedPage(this.btreeColumnTypes, PageManagerConstants.NullPageId, PageManagerConstants.NullPageId, tran);
this.SetLeaf(newRoot, false);
using Releaser newRootLock = await tran.AcquireLock(newPageForSplit.PageId(), LockManager.LockTypeEnum.Exclusive).ConfigureAwait(false);
int pos = newRoot.InsertOrdered(rowHolderForSplit, tran, this.btreeColumnTypes, this.indexComparer);
int pos = newRoot.InsertOrdered(rowHolderForSplit, tran, this.btreeColumnTypes, this.indexPosition);
Debug.Assert(pos == 0);

newRoot.SetPrevPageId(currPage.PageId());
Expand Down
2 changes: 1 addition & 1 deletion PageManager/PageTypes/IPageSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void ResetDirty()

public abstract void Update(ST item, ushort position, ITransaction transaction);
public abstract int Insert(ST item, ITransaction transaction);
public abstract int InsertOrdered(ST item, ITransaction transaction, ColumnInfo[] columnTypes, Func<RowHolder, RowHolder, int> comparer);
public abstract int InsertOrdered(ST item, ITransaction transaction, ColumnInfo[] columnTypes, int comparisonField);
public abstract void At(ushort position, ITransaction tran, ref ST item);
public abstract void SplitPage(MixedPage newPage, ref RowHolder splitValue, int elemNumForSplit, ITransaction transaction);

Expand Down
4 changes: 2 additions & 2 deletions PageManager/PageTypes/MixedPageType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ public override int Insert(RowHolder item, ITransaction transaction)
}
}

public override int InsertOrdered(RowHolder item, ITransaction transaction, ColumnInfo[] columnTypes, Func<RowHolder, RowHolder, int> comparer)
public override int InsertOrdered(RowHolder item, ITransaction transaction, ColumnInfo[] columnTypes, int comparisonField)
{
transaction.VerifyLock(this.pageId, LockManager.LockTypeEnum.Exclusive);

lock (this.lockObject)
{
int position = this.items.InsertRowOrdered(item, columnTypes, comparer);
int position = this.items.InsertRowOrdered(item, columnTypes, comparisonField);

if (position == -1)
{
Expand Down
2 changes: 1 addition & 1 deletion PageManager/PageTypes/StringOnlyPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public override void At(ushort position, ITransaction tran, ref char[] item)
throw new NotImplementedException();
}

public override int InsertOrdered(char[] item, ITransaction transaction, ColumnInfo[] columnTypes, Func<RowHolder, RowHolder, int> comparer)
public override int InsertOrdered(char[] item, ITransaction transaction, ColumnInfo[] columnTypes, int comparisonField)
{
throw new NotImplementedException();
}
Expand Down
47 changes: 42 additions & 5 deletions PageManager/RowsetHolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public unsafe struct RowsetHolder

private ushort rowCount;

private static readonly CompareWithRowHolderCreator compareWithRowHolderCreator = new CompareWithRowHolderCreator();

public RowsetHolder(ColumnInfo[] columnTypes, Memory<byte> storage, bool init)
{
System.Diagnostics.Debug.Assert(BitConverter.IsLittleEndian, "Rowset holder fixed assumes that we are running on little endian");
Expand Down Expand Up @@ -106,6 +108,13 @@ public void UpdateRowCount()
}
}

public int CompareFieldWithRowHolder(int row, int col, RowHolder rh, ColumnInfo ci)
{
return ColumnTypeHandlerRouter<Func<int, int, RowHolder, ColumnInfo, RowsetHolder, int>>.Route(
compareWithRowHolderCreator,
ci.ColumnType)(row, col, rh, ci, this);
}

public void GetRow(int row, ref RowHolder rowHolder)
{
System.Diagnostics.Debug.Assert(IsPresent(row));
Expand Down Expand Up @@ -143,9 +152,10 @@ public int InsertRow(RowHolder rowHolder)
}
}

public int InsertRowOrdered(RowHolder rowHolderToInsert, ColumnInfo[] columnTypes, Func<RowHolder, RowHolder, int> comparer)
public int InsertRowOrdered(RowHolder rowHolderToInsert, ColumnInfo[] columnTypes, int comparisonField)
{
// find the first element that is bigger than one to insert.
// No need to keep free items. Try to compact every time.
// TODO: this can be logn.
int positionToInsert = -1;
bool insertAtEnd = false;
Expand All @@ -154,11 +164,8 @@ public int InsertRowOrdered(RowHolder rowHolderToInsert, ColumnInfo[] columnType
{
if (BitArray.IsSet(i, this.storage.Span))
{
RowHolder rowHolder = new RowHolder(columnTypes);
GetRow(i, ref rowHolder);
if (comparer(rowHolderToInsert, rowHolder) != 1)
if (this.CompareFieldWithRowHolder(i, comparisonField, rowHolderToInsert, columnTypes[comparisonField]) != -1)
{
// I am bigger than you, I should be at your place.
positionToInsert = i;
break;
}
Expand Down Expand Up @@ -445,4 +452,34 @@ private int GetTuplePosition(int row, int col)
return tuplePosition + offsetInTouple;
}
}

public class CompareWithRowHolderCreator : ColumnTypeHandlerBasicSingle<Func<int, int, RowHolder, ColumnInfo, RowsetHolder, int>>
{
public Func<int, int, RowHolder, ColumnInfo, RowsetHolder, int> HandleDouble()
{
return (row, col, rh, ci, rs) =>
{
double fieldRowsetHolder = rs.GetRowGeneric<double>(row, col);
double fieldFromRowHolder = rh.GetField<double>(col);
return fieldRowsetHolder.CompareTo(fieldFromRowHolder);
};
}

public Func<int, int, RowHolder, ColumnInfo, RowsetHolder, int> HandleInt()
{
return (row, col, rh, ci, rs) =>
{
int fieldRowsetHolder = rs.GetRowGeneric<int>(row, col);
int fieldFromRowHolder = rh.GetField<int>(col);
return fieldRowsetHolder.CompareTo(fieldFromRowHolder);
};
}

public Func<int, int, RowHolder, ColumnInfo, RowsetHolder, int> HandleString()
{
throw new NotImplementedException();
}
}
}
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit eac10e2

Please sign in to comment.