Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move instance field to static #714

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

DanielThomas
Copy link
Contributor

SUMMARY

This adds an additional unnecessary reference to every BitmapContainer.

Automated Checks

  • [ x ] I have run ./gradlew test and made sure that my PR does not break any unit test.
  • [ x ] I have run ./gradlew checkstyleMain or the equivalent and corrected the formatting warnings reported.

@lemire
Copy link
Member

lemire commented Mar 22, 2024

Looks like a good catch.

@lemire
Copy link
Member

lemire commented Mar 22, 2024

@DanielThomas Could you also do:

private final int MAXRUNS = (getArraySizeInBytes() - 2) / 4;

@DanielThomas
Copy link
Contributor Author

@lemire I removed some duplication and switched in some size constants. Do you mind the cross-cutting package imports?

@@ -2234,7 +2236,7 @@ private void smartAppendExclusive(char[] vl, char start, char length) {
// convert to bitmap *if needed* (useful if you know it can't be an array)
private MappeableContainer toBitmapIfNeeded() {
int sizeAsRunContainer = MappeableRunContainer.serializedSizeInBytes(this.nbrruns);
int sizeAsBitmapContainer = MappeableBitmapContainer.serializedSizeInBytes(0);
int sizeAsBitmapContainer = serializedSizeInBytes(0);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, let me fix this.

Copy link
Member

@richardstartin richardstartin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess nobody ever spotted this because bitmap containers contain an 8KiB array, so if you have enough of them for this slip up to show up noticeably, you would be distracted by hundreds of MiBs or even GiBs of long[] in the heap dump, but this is a great catch.

@DanielThomas
Copy link
Contributor Author

YourKit's duplicate object inspection :)

@richardstartin
Copy link
Member

richardstartin commented Mar 22, 2024

Interestingly, this won't always amount to a footprint optimisation, because the stray field will usually fit into alignment shadow anyway (1.0.5)

System.err.println(ClassLayout.parseClass(BitmapContainer.class).toPrintable());
org.roaringbitmap.BitmapContainer object internals:
OFF  SZ     TYPE DESCRIPTION                   VALUE
  0   8          (object header: mark)         N/A
  8   4          (object header: class)        N/A
 12   4      int BitmapContainer.cardinality   N/A
 16   4      int BitmapContainer.MAXRUNS       N/A
 20   4   long[] BitmapContainer.bitmap        N/A
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

but will save 8 bytes per BitmapContainer on large heaps or with ZGC, whenever compressed oops doesn't apply:

org.roaringbitmap.BitmapContainer object internals:
OFF  SZ     TYPE DESCRIPTION                   VALUE
  0   8          (object header: mark)         N/A
  8   4          (object header: class)        N/A
 12   4      int BitmapContainer.cardinality   N/A
 16   4      int BitmapContainer.MAXRUNS       N/A
 20   4          (alignment/padding gap)       
 24   8   long[] BitmapContainer.bitmap        N/A
Instance size: 32 bytes
Space losses: 4 bytes internal + 0 bytes external = 4 bytes total

@lemire
Copy link
Member

lemire commented Mar 22, 2024

@DanielThomas Can you check the CI logs? I see...


  symbol:   variable MAX_CAPACITY
  location: class MappeableBitmapContainer
/home/runner/work/RoaringBitmap/RoaringBitmap/RoaringBitmap/src/main/java/org/roaringbitmap/buffer/BufferBitSetUtil.java:24: error: cannot find symbol
  static final private int BLOCK_LENGTH = MappeableBitmapContainer.MAX_CAPACITY / Long.SIZE; //
                                                                  ^

@lemire
Copy link
Member

lemire commented Apr 18, 2024

@DanielThomas This PR still does not build...

/home/runner/work/RoaringBitmap/RoaringBitmap/RoaringBitmap/src/main/java/org/roaringbitmap/longlong/Roaring64NavigableMap.java:1165: warning: [deprecation] MutableRoaringBitmapPrivate in org.roaringbitmap.buffer has been deprecated
        MutableRoaringBitmapPrivate.repairAfterLazy((MutableRoaringBitmap) lowBitmap);
        ^
/home/runner/work/RoaringBitmap/RoaringBitmap/RoaringBitmap/src/main/java/org/roaringbitmap/buffer/ImmutableRoaringArray.java:185: error: cannot find symbol
      bitmapArray.limit(MappeableBitmapContainer.MAX_CAPACITY / 64);
                                                ^
  symbol:   variable MAX_CAPACITY
  location: class MappeableBitmapContainer
/home/runner/work/RoaringBitmap/RoaringBitmap/RoaringBitmap/src/main/java/org/roaringbitmap/buffer/BufferBitSetUtil.java:24: error: cannot find symbol
  static final private int BLOCK_LENGTH = MappeableBitmapContainer.MAX_CAPACITY / Long.SIZE;

@DanielThomas
Copy link
Contributor Author

Sorry @lemire, let me get that fixed.

@DanielThomas
Copy link
Contributor Author

DanielThomas commented Apr 22, 2024

Based on your comments on the other PR about code duplication being a thing for the project, I've rolled back any other changes and restricted this to making the field in BitmapContainer static.

@lemire
Copy link
Member

lemire commented Apr 22, 2024

Based on your comments on the other PR about code duplication being a thing for the project, I've rolled back any other changes and restricted this to making the field in BitmapContainer static.

Note that it is not that we encourage code duplication; it is merely that, historically, we have accepted it in this library.

I do not discourage you from removing unnecessary code duplication. However, I prefer to be prudent with code refactoring that merely reduces code duplication because we can introduce new bugs by mistake.

Copy link
Member

@lemire lemire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to keep in sync BitmapContainer.java and MappeableBitmapContainer.java. Could you apply your changes to MappeableBitmapContainer.java.

See...

private final int MAXRUNS = (getArraySizeInBytes() - 2) / 4;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants