GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Better use of bit manipulation instructions for bitmap iterations by richardstartin · Pull Request #227 · RoaringBitmap/RoaringBitmap

Conversation

@richardstartin

@richardstartin richardstartin commented Mar 9, 2018

Copy link
Copy Markdown
Member

I aim to speed container iteration up. Before doing that I did some instruction profiling, which revealed some suboptimal instructions were being JIT compiled for bitmap iteration:

long t = bitset & -bitset;
array[pos++] = (short) (base + Long.bitCount(t - 1));
bitset ^= t;
  0.89%    0x0000000004b27296: blsi    rbx,r10           
  0.93%    0x0000000004b2729b: mov     rdi,rbx
  2.22%    0x0000000004b2729e: xor     rdi,r10           
  1.40%    0x0000000004b272a1: mov     qword ptr [r11+10h],rdi  
  2.22%    0x0000000004b272a5: mov     ecx,r8d
  0.57%    0x0000000004b272a8: shl     ecx,6h
  2.06%    0x0000000004b272ab: dec     rbx
  1.24%    0x0000000004b272ae: popcnt  rbx,rbx
  2.25%    0x0000000004b272b3: add     ecx,ebx           

Iterating over a bitmap of various densities looks like this:

Benchmark (density) Mode Cnt Score Error Units
BitmapIteratorBenchmark.forwards 0.1 avgt 5 17.645 0.505 us/op
BitmapIteratorBenchmark.forwards 0.2 avgt 5 41.879 0.907 us/op
BitmapIteratorBenchmark.forwards 0.3 avgt 5 59.874 0.457 us/op
BitmapIteratorBenchmark.forwards 0.4 avgt 5 78.776 1.826 us/op
BitmapIteratorBenchmark.forwards 0.5 avgt 5 89.05 0.775 us/op

This is very hotspot/Intel specific but C2 emits better code on Intel ("fusing" blsi and xor into blsr) with the pattern below, tzcnt is a drop in for popcnt and doesn't require a subtraction.

array[pos++] = (short) (base + numberOfTrailingZeros(bitset));
bitset &= (bitset - 1);
  0.77%    0x0000000005605904: mov     edi,r10d
  1.60%    0x0000000005605907: shl     edi,6h
  3.19%    0x000000000560590a: tzcnt   r8,r11
  1.49%    0x000000000560590f: add     edi,r8d        
  1.41%    0x0000000005605912: blsr    r11,r11           

This performs better:

Benchmark (density) Mode Cnt Score Error Units
BitmapIteratorBenchmark.forwards 0.1 avgt 5 17.051 0.412 us/op
BitmapIteratorBenchmark.forwards 0.2 avgt 5 36.951 0.456 us/op
BitmapIteratorBenchmark.forwards 0.3 avgt 5 52.025 0.924 us/op
BitmapIteratorBenchmark.forwards 0.4 avgt 5 67.616 0.974 us/op
BitmapIteratorBenchmark.forwards 0.5 avgt 5 73.792 0.781 us/op

Is this micro-optimisation? Yes. But if you have 5 dense bitmaps it would cost 100us. This only takes effect in code compiled by C2 and I don't know what the tradeoff is on different microarchitectures.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@coveralls

coveralls commented Mar 9, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.03%) to 91.058% when pulling b57a0e4 on richardstartin:use-blsr into 7d8b720 on RoaringBitmap:master.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@richardstartin

richardstartin commented Mar 9, 2018

Copy link
Copy Markdown
Member Author

I don't really get the coverage metrics reduction - this replaces three lines with two in several places.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lemire

lemire commented Mar 9, 2018

Copy link
Copy Markdown
Member

Happy to merge this, but before we do... Are you interested in porting over the same changes to the buffer package?

https://github.com/RoaringBitmap/RoaringBitmap/blob/master/roaringbitmap/src/main/java/org/roaringbitmap/buffer/MappeableBitmapContainer.java#L472

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@richardstartin

richardstartin commented Mar 9, 2018

Copy link
Copy Markdown
Member Author

Yes I will port them.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@richardstartin

richardstartin commented Mar 10, 2018

Copy link
Copy Markdown
Member Author

There's not much justification for the changes in the buffer package. There may be a small benefit, and there is no cost, but I would be happy to revert the latest commit. My prejudice is that the inherent inefficiencies of ByteBuffers dominate, but don't have time to investigate further.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@blacelle

blacelle commented Mar 10, 2018

Copy link
Copy Markdown
Member

About coverage reduction, I guess you lowered the number of lines in a code very well covered. The weight of not-covered lines is then greater, hence decreasing the coverage ratio.

richardstartin reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@richardstartin
use tzcnt, blsr instead of blsi, popcnt, xor
b57a0e4
@richardstartin

richardstartin commented Mar 10, 2018

Copy link
Copy Markdown
Member Author

@BlasD have a gold star.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lemire

lemire commented Mar 10, 2018

Copy link
Copy Markdown
Member

Minute coverage differences (-0.03%) are obviously not significant.

Merging.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lemire
lemire merged commit 33200d5 into RoaringBitmap:master Mar 10, 2018
@richardstartin
richardstartin deleted the use-blsr branch April 15, 2018 08:43
Smallhi pushed a commit to Smallhi/RoaringBitmap that referenced this pull request Jun 14, 2021
@richardstartin @lemire
use tzcnt, blsr instead of blsi, popcnt, xor (RoaringBitmap#227)
e64e5eb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@richardstartin @coveralls @lemire @blacelle
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.