From fb9e6147f78b3b5ccb76439e5828f09186255772 Mon Sep 17 00:00:00 2001 From: konard Date: Sun, 14 Sep 2025 07:24:16 +0300 Subject: [PATCH 1/3] Initial commit with task details for issue #57 Adding CLAUDE.md with task information for AI processing. This file will be removed when the task is complete. Issue: https://github.com/linksplatform/Collections/issues/57 --- CLAUDE.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 CLAUDE.md diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 00000000..b057deb1 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,5 @@ +Issue to solve: https://github.com/linksplatform/Collections/issues/57 +Your prepared branch: issue-57-1c43f17d +Your prepared working directory: /tmp/gh-issue-solver-1757823843798 + +Proceed. \ No newline at end of file From 02f4aecdca22c4d03d86a2cc839b8495230692ad Mon Sep 17 00:00:00 2001 From: konard Date: Sun, 14 Sep 2025 07:33:37 +0300 Subject: [PATCH 2/3] Implement BitString chaining mechanism for optimized multi-operation performance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This implementation addresses issue #57 by providing a chaining mechanism that allows multiple BitString operations to be applied in a single memory pass, reducing memory bandwidth usage and improving performance. **Key Features:** - `BitStringChain` class that accumulates multiple operations - Single-pass execution through memory for all queued operations - Support for AND, OR, XOR, and NOT operations in any combination - Fluent API for easy operation chaining - Performance-optimized implementation using internal array access **Performance Benefits:** - Reduces memory bandwidth usage by processing all operations in one pass - Eliminates redundant memory traversals when applying multiple operations - Maintains cache locality for better performance on large BitStrings **Usage Examples:** ```csharp // Traditional approach (multiple passes) bitString.Not().And(other1).Or(other2).Xor(other3); // Optimized approach (single pass) bitString.Chain().Not().And(other1).Or(other2).Xor(other3).Execute(); ``` **Testing:** - Comprehensive unit tests for all operation combinations - Benchmarks comparing traditional vs chained approaches - Validation that chained operations produce identical results 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../BitStringBenchmarks.cs | 50 +++++ .../BitStringTests.cs | 121 ++++++++++ csharp/Platform.Collections/BitString.cs | 33 ++- csharp/Platform.Collections/BitStringChain.cs | 211 ++++++++++++++++++ examples/BitStringChainExample.cs | 78 +++++++ 5 files changed, 491 insertions(+), 2 deletions(-) create mode 100644 csharp/Platform.Collections/BitStringChain.cs create mode 100644 examples/BitStringChainExample.cs diff --git a/csharp/Platform.Collections.Benchmarks/BitStringBenchmarks.cs b/csharp/Platform.Collections.Benchmarks/BitStringBenchmarks.cs index 67649f9b..d4641439 100644 --- a/csharp/Platform.Collections.Benchmarks/BitStringBenchmarks.cs +++ b/csharp/Platform.Collections.Benchmarks/BitStringBenchmarks.cs @@ -70,5 +70,55 @@ public void Setup() [Benchmark] public BitString ParallelVectorXor() => new BitString(_left).ParallelVectorXor(_right); + + [Benchmark] + public BitString ChainedOperationsSeparate() + { + // Traditional approach: multiple passes through memory + var result = new BitString(_left); + result.Not(); + result.And(_right); + result.Xor(_left); + return result; + } + + [Benchmark] + public BitString ChainedOperationsOptimized() + { + // Optimized approach: single pass through memory using chaining + return new BitString(_left) + .Chain() + .Not() + .And(_right) + .Xor(_left) + .Execute(); + } + + [Benchmark] + public BitString ComplexChainedOperationsSeparate() + { + // More complex operations with traditional approach + var result = new BitString(_left); + result.Not(); + result.And(_right); + result.Not(); + result.Or(_left); + result.Xor(_right); + return result; + } + + [Benchmark] + public BitString ComplexChainedOperationsOptimized() + { + // More complex operations with chaining + return new BitString(_left) + .Chain() + .Not() + .And(_right) + .Not() + .Or(_left) + .Xor(_right) + .Execute(); + } } } diff --git a/csharp/Platform.Collections.Tests/BitStringTests.cs b/csharp/Platform.Collections.Tests/BitStringTests.cs index 988deca8..d0ad3a03 100644 --- a/csharp/Platform.Collections.Tests/BitStringTests.cs +++ b/csharp/Platform.Collections.Tests/BitStringTests.cs @@ -161,5 +161,126 @@ private static void TestToOperationsWithSameMeaning(Action SetBorders(0, _array.LongLength - 1); [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void GetBorders(out long from, out long to) + internal void GetBorders(out long from, out long to) { from = _minPositiveWord; to = _maxPositiveWord; @@ -1859,5 +1859,34 @@ public static void GetCommonBorders(BitString left, BitString right, out ulong f /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public override string ToString() => base.ToString(); + + /// + /// + /// Creates a new BitStringChain for chaining multiple operations efficiently. + /// This allows multiple operations to be applied in a single memory pass, + /// reducing memory bandwidth usage and improving performance. + /// + /// + /// + /// + /// A new BitStringChain instance for method chaining. + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public BitStringChain Chain() => new BitStringChain(this); + + /// + /// + /// Gets the internal array for internal operations. This method is used by BitStringChain. + /// + /// + /// + /// + /// The internal long array. + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal long[] GetInternalArray() => _array; + } } \ No newline at end of file diff --git a/csharp/Platform.Collections/BitStringChain.cs b/csharp/Platform.Collections/BitStringChain.cs new file mode 100644 index 00000000..115d018e --- /dev/null +++ b/csharp/Platform.Collections/BitStringChain.cs @@ -0,0 +1,211 @@ +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; + +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + +namespace Platform.Collections +{ + /// + /// + /// Represents a chaining mechanism for BitString operations that applies multiple operations in a single memory pass. + /// This provides better performance when applying multiple operations sequentially by reducing memory bandwidth usage. + /// + /// + /// + public class BitStringChain + { + private readonly BitString _target; + private readonly List _operations; + + /// + /// + /// Initializes a new instance. + /// + /// + /// + /// + /// The target BitString to apply operations to. + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public BitStringChain(BitString target) + { + _target = target ?? throw new ArgumentNullException(nameof(target)); + _operations = new List(); + } + + /// + /// + /// Chains a NOT operation. + /// + /// + /// + /// + /// The current BitStringChain instance for method chaining. + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public BitStringChain Not() + { + _operations.Add(new NotOperation()); + return this; + } + + /// + /// + /// Chains an AND operation with another BitString. + /// + /// + /// + /// + /// The other BitString to AND with. + /// + /// + /// + /// The current BitStringChain instance for method chaining. + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public BitStringChain And(BitString other) + { + _operations.Add(new AndOperation(other)); + return this; + } + + /// + /// + /// Chains an OR operation with another BitString. + /// + /// + /// + /// + /// The other BitString to OR with. + /// + /// + /// + /// The current BitStringChain instance for method chaining. + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public BitStringChain Or(BitString other) + { + _operations.Add(new OrOperation(other)); + return this; + } + + /// + /// + /// Chains an XOR operation with another BitString. + /// + /// + /// + /// + /// The other BitString to XOR with. + /// + /// + /// + /// The current BitStringChain instance for method chaining. + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public BitStringChain Xor(BitString other) + { + _operations.Add(new XorOperation(other)); + return this; + } + + /// + /// + /// Executes all chained operations in a single pass over the memory. + /// This provides optimal memory bandwidth usage by applying all operations + /// to each memory location before moving to the next. + /// + /// + /// + /// + /// The target BitString with all operations applied. + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public BitString Execute() + { + if (_operations.Count == 0) + { + return _target; + } + + // Get the working range for optimization + _target.GetBorders(out long from, out long to); + + // Apply all operations in a single pass through memory + var targetArray = _target.GetInternalArray(); + for (var i = from; i <= to; i++) + { + var currentValue = targetArray[i]; + + // Apply each operation in sequence to the current word + foreach (var operation in _operations) + { + currentValue = operation.Apply(currentValue, i); + } + + targetArray[i] = currentValue; + _target.RefreshBordersByWord(i); + } + + _operations.Clear(); + return _target; + } + + private interface IChainedOperation + { + long Apply(long value, long wordIndex); + } + + private class NotOperation : IChainedOperation + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public long Apply(long value, long wordIndex) => ~value; + } + + private class AndOperation : IChainedOperation + { + private readonly long[] _otherArray; + + public AndOperation(BitString other) + { + _otherArray = other.GetInternalArray(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public long Apply(long value, long wordIndex) => value & _otherArray[wordIndex]; + } + + private class OrOperation : IChainedOperation + { + private readonly long[] _otherArray; + + public OrOperation(BitString other) + { + _otherArray = other.GetInternalArray(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public long Apply(long value, long wordIndex) => value | _otherArray[wordIndex]; + } + + private class XorOperation : IChainedOperation + { + private readonly long[] _otherArray; + + public XorOperation(BitString other) + { + _otherArray = other.GetInternalArray(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public long Apply(long value, long wordIndex) => value ^ _otherArray[wordIndex]; + } + } +} \ No newline at end of file diff --git a/examples/BitStringChainExample.cs b/examples/BitStringChainExample.cs new file mode 100644 index 00000000..0588c1cd --- /dev/null +++ b/examples/BitStringChainExample.cs @@ -0,0 +1,78 @@ +using System; +using Platform.Collections; + +namespace Examples +{ + /// + /// This example demonstrates the BitString chaining mechanism that allows multiple operations + /// to be applied in a single memory pass, improving performance by reducing memory bandwidth usage. + /// + public class BitStringChainExample + { + public static void RunExample() + { + Console.WriteLine("BitString Chaining Example"); + Console.WriteLine("=========================="); + + // Create three BitStrings for demonstration + var bitString1 = new BitString(1000); + var bitString2 = new BitString(1000); + var bitString3 = new BitString(1000); + + // Fill them with random data + bitString1.SetRandomBits(); + bitString2.SetRandomBits(); + bitString3.SetRandomBits(); + + Console.WriteLine($"BitString1 set bits: {bitString1.CountSetBits()}"); + Console.WriteLine($"BitString2 set bits: {bitString2.CountSetBits()}"); + Console.WriteLine($"BitString3 set bits: {bitString3.CountSetBits()}"); + + // Traditional approach: Multiple memory passes + Console.WriteLine("\nTraditional approach (multiple memory passes):"); + var traditionalResult = new BitString(bitString1); + var start = DateTime.UtcNow; + + traditionalResult.Not(); // Pass 1: Invert all bits + traditionalResult.And(bitString2); // Pass 2: AND with bitString2 + traditionalResult.Or(bitString3); // Pass 3: OR with bitString3 + + var traditionalTime = DateTime.UtcNow - start; + Console.WriteLine($"Result set bits: {traditionalResult.CountSetBits()}"); + Console.WriteLine($"Time taken: {traditionalTime.TotalMicroseconds:F2} μs"); + + // Optimized approach: Single memory pass using chaining + Console.WriteLine("\nOptimized approach (single memory pass with chaining):"); + var optimizedResult = new BitString(bitString1); + start = DateTime.UtcNow; + + optimizedResult.Chain() + .Not() // Queue: Invert all bits + .And(bitString2) // Queue: AND with bitString2 + .Or(bitString3) // Queue: OR with bitString3 + .Execute(); // Execute all operations in single pass + + var optimizedTime = DateTime.UtcNow - start; + Console.WriteLine($"Result set bits: {optimizedResult.CountSetBits()}"); + Console.WriteLine($"Time taken: {optimizedTime.TotalMicroseconds:F2} μs"); + + // Verify both approaches produce identical results + Console.WriteLine($"\nResults are identical: {traditionalResult.Equals(optimizedResult)}"); + + // Complex chaining example + Console.WriteLine("\nComplex chaining example:"); + var complexResult = new BitString(bitString1); + + complexResult.Chain() + .Not() // Invert all bits + .And(bitString2) // AND with bitString2 + .Not() // Invert again + .Or(bitString3) // OR with bitString3 + .Xor(bitString1) // XOR with original bitString1 + .Execute(); // Execute all in single pass + + Console.WriteLine($"Complex result set bits: {complexResult.CountSetBits()}"); + Console.WriteLine("\nChaining allows complex operations to be efficiently executed in a single memory traversal!"); + } + } +} \ No newline at end of file From c4d55e3c85b8b4a350444b8e4d3de0ee4014d75d Mon Sep 17 00:00:00 2001 From: konard Date: Sun, 14 Sep 2025 07:34:35 +0300 Subject: [PATCH 3/3] Remove CLAUDE.md - Claude command completed --- CLAUDE.md | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 CLAUDE.md diff --git a/CLAUDE.md b/CLAUDE.md deleted file mode 100644 index b057deb1..00000000 --- a/CLAUDE.md +++ /dev/null @@ -1,5 +0,0 @@ -Issue to solve: https://github.com/linksplatform/Collections/issues/57 -Your prepared branch: issue-57-1c43f17d -Your prepared working directory: /tmp/gh-issue-solver-1757823843798 - -Proceed. \ No newline at end of file