diff --git a/PersistentMap/BTreeFunctions.cs b/PersistentMap/BTreeFunctions.cs index 4596c0a..ca199fc 100644 --- a/PersistentMap/BTreeFunctions.cs +++ b/PersistentMap/BTreeFunctions.cs @@ -10,12 +10,9 @@ namespace PersistentMap // Public API // --------------------------------------------------------- - /// TryGetValue tries to get the value at mapping key. If it finds the key it sets th - /// out var to value and returns true. public static bool TryGetValue(Node root, K key, TStrategy strategy, out V value) where TStrategy : IKeyStrategy { - // We always get a strategy to avoid branching already here long keyPrefix = strategy.UsesPrefixes ? strategy.GetPrefix(key) : 0; Node current = root; @@ -46,7 +43,6 @@ namespace PersistentMap { root = root.EnsureEditable(owner); - // Todo, this should really be made a tuple return value to not stress the GC var splitResult = InsertRecursive(root, key, value, strategy, owner, out countChanged); if (splitResult != null) @@ -216,7 +212,7 @@ namespace PersistentMap where TStrategy : IKeyStrategy { - if (typeof(K) == typeof(int)) +if (typeof(K) == typeof(int)) { Span keys = node.GetKeys(); ref K firstKeyRef = ref MemoryMarshal.GetReference(keys); @@ -333,11 +329,11 @@ namespace PersistentMap // Insertion Logic // --------------------------------------------------------- - private class SeplitResult + private class SplitResult { public Node NewNode; public K Separator; - public SeplitResult(Node newNode, K separator) + public SplitResult(Node newNode, K separator) { NewNode = newNode; Separator = separator; @@ -371,7 +367,7 @@ namespace PersistentMap leaf.SetCount(count + 1); } - private static (Node, K) SplitLeaf(LeafNode left, int insertIndex, K key, V value, TStrategy strategy, OwnerId owner) + private static SplitResult SplitLeaf(LeafNode left, int insertIndex, K key, V value, TStrategy strategy, OwnerId owner) where TStrategy : IKeyStrategy { var right = new LeafNode(owner, strategy.UsesPrefixes); @@ -395,15 +391,15 @@ namespace PersistentMap right.SetCount(moveCount); if (insertIndex < splitPoint || (splitPoint == 0 && insertIndex == 0)) - { - InsertIntoLeaf(left, insertIndex, key, value, strategy); - } - else - { - InsertIntoLeaf(right, insertIndex - splitPoint, key, value, strategy); - } +{ + InsertIntoLeaf(left, insertIndex, key, value, strategy); +} +else +{ + InsertIntoLeaf(right, insertIndex - splitPoint, key, value, strategy); +} - return (right, right.Keys[0]); + return new SplitResult(right, right.Keys[0]); } private static void InsertIntoInternal(InternalNode node, int index, K separator, Node newChild, TStrategy strategy) @@ -437,7 +433,7 @@ namespace PersistentMap node.SetCount(count + 1); } - private static (Node, K) SplitInternal(InternalNode left, int insertIndex, K separator, Node newChild, TStrategy strategy, OwnerId owner) + private static SplitResult SplitInternal(InternalNode left, int insertIndex, K separator, Node newChild, TStrategy strategy, OwnerId owner) where TStrategy : IKeyStrategy { var right = strategy.UsesPrefixes @@ -477,7 +473,7 @@ namespace PersistentMap InsertIntoInternal(right, insertIndex - (splitPoint + 1), separator, newChild, strategy); } - return (right, upKey); + return new SplitResult(right, upKey); } // --------------------------------------------------------- diff --git a/PersistentMap/KeyStrategies/IntScanner.cs b/PersistentMap/KeyStrategies/IntScanner.cs index 325436d..3c19856 100644 --- a/PersistentMap/KeyStrategies/IntScanner.cs +++ b/PersistentMap/KeyStrategies/IntScanner.cs @@ -44,7 +44,7 @@ public static class IntScanner { var vData = Avx2.LoadVector256(ptr + i); var vResult = Avx2.CompareGreaterThan(vData, vTarget); - + // MoveMask creates a 32-bit integer from the most significant bit of each byte. var mask = (uint)Avx2.MoveMask(vResult.AsByte()); @@ -73,7 +73,7 @@ public static class IntScanner fixed (int* ptr = keys) { var vData = Avx512F.LoadVector512(ptr + i); - + // Vector512 API is used directly here to cleanly get the mask var mask = Vector512.GreaterThanOrEqual(vData, vTarget); @@ -88,7 +88,7 @@ public static class IntScanner return LinearScan(keys.Slice(i), target) + i; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int FindFirstGreater(ReadOnlySpan keys, int target) { if (!Avx2.IsSupported || keys.Length < 8) @@ -107,7 +107,7 @@ public static class IntScanner return i; return keys.Length; } - + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static unsafe int ScanAvx2Greater(ReadOnlySpan keys, int target) { @@ -122,7 +122,7 @@ public static class IntScanner { var vData = Avx2.LoadVector256(ptr + i); var vResult = Avx2.CompareGreaterThan(vData, vTarget); - + var mask = (uint)Avx2.MoveMask(vResult.AsByte()); if (mask != 0) @@ -135,7 +135,7 @@ public static class IntScanner return LinearScanGreater(keys.Slice(i), target) + i; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] +[MethodImpl(MethodImplOptions.AggressiveInlining)] private static unsafe int ScanAvx512Greater(ReadOnlySpan keys, int target) { var vTarget = Vector512.Create(target); @@ -147,7 +147,7 @@ public static class IntScanner fixed (int* ptr = keys) { var vData = Avx512F.LoadVector512(ptr + i); - + // Use GreaterThan instead of GreaterThanOrEqual var mask = Vector512.GreaterThan(vData, vTarget); diff --git a/PersistentMap/Nodes.cs b/PersistentMap/Nodes.cs index 59075c4..1e1d6ba 100644 --- a/PersistentMap/Nodes.cs +++ b/PersistentMap/Nodes.cs @@ -90,10 +90,10 @@ public abstract class Node } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public PrefixInternalNode AsPrefixInternal() - { - return Unsafe.As>(this); - } +public PrefixInternalNode AsPrefixInternal() +{ + return Unsafe.As>(this); +} } public sealed class LeafNode : Node