easyice commented on PR #15045:
URL: https://github.com/apache/lucene/pull/15045#issuecomment-3220493289

   It s a nice idea!  although it requires allocating an 
`FixedBitSet(bits.length())`, it is still much faster than checking bits one by 
one.
   
   Here are some JMH numbers:
   
   ```
   Benchmark                                     (density)  (size)   Mode  Cnt  
 Score   Error   Units
   FixedBitSetBenchmark.countWithCardinality           0.5    1024  thrpt    5  
53.244 ± 3.754  ops/us
   FixedBitSetBenchmark.countWithFallbackGet           0.5    1024  thrpt    5  
 1.912 ± 0.090  ops/us
   FixedBitSetBenchmark.countWithFixedBitSetGet        0.5    1024  thrpt    5  
 1.758 ± 0.047  ops/us
   ```
   
   `countWithCardinality` this approach, use `applyMask` and `cardinality`
   `countWithFallbackGet`  uses a subclass of `Bits` to mock a `Bits` object 
that is not an instance of `FixedBitSet`
   `countWithFixedBitSetGet` use `FixedBitSet#get`
   
   
   <details>
   <summary >Code</summary>
   
   ```java
   
   @BenchmarkMode(Mode.Throughput)
   @OutputTimeUnit(TimeUnit.MICROSECONDS)
   @State(Scope.Benchmark)
   @Warmup(iterations = 3, time = 3)
   @Measurement(iterations = 5, time = 5)
   @Fork(1)
   public class FixedBitSetBenchmark {
   
     @Param({"1024"})
     private int size;
   
     @Param({"0.5"})
     private float density; // the percentage of 1 in the bitset
   
     private Bits bits; // FixedBitSet#asReadOnlyBits 
   
     Bits fallbackBits; // will not use FixedBitSet
   
     @Setup(Level.Trial)
     public void setup() {
       FixedBitSet bitSet = new FixedBitSet(size);
       int numSet = (int) (size * density);
   
       if (numSet == size) {
         bitSet.set(0, size);
       } else if (numSet > 0) {
         Random random = new Random(0);
         for (int i = 0; i < numSet; i++) {
           bitSet.set(random.nextInt(size - 1));
         }
       }
       bits = bitSet.asReadOnlyBits();
   
       fallbackBits =
           new Bits() {
             @Override
             public boolean get(int index) {
               return index % 2 == 0;
             }
   
             @Override
             public int length() {
               return size;
             }
           };
     }
   
     @Benchmark
     public void countWithCardinality(Blackhole bh) {
       int count = 0;
       FixedBitSet bitSet = new FixedBitSet(size);
       bitSet.set(0, size);
   
       bits.applyMask(bitSet, 0);
       count = bitSet.cardinality();
       bh.consume(count);
     }
   
     @Benchmark
     public void countWithFixedBitSetGet(Blackhole bh) {
       int count = 0;
       for (int i = 0; i < bits.length(); i++) {
         if (bits.get(i)) {
           count++;
         }
       }
       bh.consume(count);
     }
   
     @Benchmark
     public void countWithFallbackGet(Blackhole bh) {
       int count = 0;
       for (int i = 0; i < fallbackBits.length(); i++) {
         if (fallbackBits.get(i)) {
           count++;
         }
       }
       bh.consume(count);
     }
   }
   
   ```
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to