Skip to content

RoamingBitmap to Uncompressed BitSet/byte[] #556

Closed

Description

@jsorel

Hello,

In a format call 'GRIB' there is bitmap section, so far i used a classic java Bitset but on some files it takes a lot of memory.
So I'm trying to use RoamingBitmap instead, but at writing I didn't find any way to get a compact byte[] as in Bitset.toArray.

Maybe I missing it, something like :

BitSet bs = BitSetUtil.toBiset(RoamingBitmap)

Or maybe there is somekind of serializer ? How do you suggest it could be done in a efficient way (looping on all int to rebuild the byte[] would obviously be inefficient) ?

Thanks

Activity

  1. larsk-db commented on Mar 29, 2022

    @larsk-db
    Contributor

    You are looking for bitmap.serialize(buffer) I believe? Something like:

    val buffer = ByteBuffer.allocate(bitmap.serializedSizeInBytes)
    bitmap.serialize(buffer)
    buffer.toArray
  2. jsorel commented on Mar 29, 2022

    @jsorel
    Author

    The doc is not very clear :

    Description copied from interface: [ImmutableBitmapDataProvider](https://github.com/RoaringBitmap/RoaringBitmap/issues/ImmutableBitmapDataProvider.html#serialize(java.nio.ByteBuffer))
    Serialize this bitmap to a ByteBuffer. This is the preferred method to serialize to a byte array (byte[]) or to a String (via Base64.getEncoder().encodeToString).. Irrespective of the endianess of the provided buffer, data is written using LITTlE_ENDIAN **as per the RoaringBitmap specification**. The current bitmap is not modified.
    

    this looks like it generates a custom serialization byte array.

    Or do you confirm it will produce the same output as BitSet.toByteArray() ?

  3. jsorel commented on Mar 29, 2022

    @jsorel
    Author

    Just made a test :

            RoaringBitmap bitset = new RoaringBitmap();
            bitset.add(1000);
    
            final ByteBuffer buffer = ByteBuffer.allocate(bitset.serializedSizeInBytes());
            bitset.serialize(buffer);
            final byte[] bits = buffer.array();
            System.out.println(Arrays.toString(bits));
    

    output is :

    [58, 48, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, -24, 3]
    

    Which is not what I expected, a single bit set at offset 1000.
    Any other solution ?

  4. larsk-db commented on Mar 29, 2022

    @larsk-db
    Contributor

    Ah, my bad. I thought you just wanted a serialised version you can store in a GRIB file.

    I don't believe RoaringBitmap has an API to convert into a bitset representation, beyond just iterating through all the values, adding them into a bitset and calling toByteArray() on that.

  5. jsorel commented on Mar 29, 2022

    @jsorel
    Author

    That's what I have for now, but it does look really inefficient.

            final BitSet bitset = new BitSet(numBits);
            this.bitset.forEach((IntConsumer) bitset::set);  
            final byte[] bits = bitset.toByteArray();
    
  6. larsk-db commented on Mar 29, 2022

    @larsk-db
    Contributor

    It certainly is.

    You could add a new method for this in a PR, that at least for containers that are already bitset encoded gives you better performance, but it'd still have to essentially go through this step (or something similar) for array and RLE containers.

    And of course you'd immediately lose your size reduction benefits from using RoaringBitmap in the first place again. So I think the real question is, if this conversion can't somehow be avoided on your side? If not, using RoaringBitmap may not make a lot of sense in your use case.

  7. jsorel commented on Mar 29, 2022

    @jsorel
    Author

    I have no choice, the GRIB Format is well defined, it's a standard
    https://www.nco.ncep.noaa.gov/pmb/docs/grib2/grib2_doc/

    We loose in memory, but it's only when writing it back in GRIB, when file is loaded it uses far less then a BitSet, so it's worth it.

    After a benchmark, we drop from 3.83Mo to 194Ko

    With BitSet :
    grib_bitset

    With RoaringBitmap :
    grib_roaringbitmap

  8. larsk-db commented on Mar 29, 2022

    @larsk-db
    Contributor

    Ok, that makes sense.
    Would it be possible, instead of materialising the entire byte[] from the RoaringBitmap at once, to perhaps materialise container by container and immediately write that out into the GRIB file? That would avoid the sudden blow-up, allow you to reuse the allocated byte[] (since all containers are the same size), and there should already be functions in RoaringBitmap to turn any container into a bitmap container, so I think it'd be fairly easy to implement in a PR.

  9. jsorel commented on Mar 29, 2022

    @jsorel
    Author

    You are likely right, but I don't have the time, I'm new with RoaringBitmap. I'm a user here with a few allocated hours to fix an out of memory exception in a project. But I do work/contribute on other Open-Source projects (Apache-SIS,GeoAPI,GeotoolKit,Examind,Unlicense-lib...).

    But now that RoaringBitMap has set a foot in Examind and that we use a lot of bitmap (because we(Geomatys) make cartography tools) It's likely in the futur we will submit more issues and/or PR.

    Thanks for the help.

  10. richardstartin commented on Mar 29, 2022

    @richardstartin
    Member

    Let's leave this open since it's generally useful, and it could probably be implemented in less than an hour for someone familiar with the code.

  11. changed the title [-]RoamingBitmap to BitSet/byte[][/-] [+]RoamingBitmap to Uncompressed BitSet/byte[][/+] on Mar 29, 2022
  12. lemire commented on Mar 29, 2022

    @lemire
    Member

    You'd probably want to go in both directions (serialize to uncompressed bitset and back).

  13. sakinapelli commented on Mar 30, 2023

    @sakinapelli

    patch.diff.zip

    Please look at the patch that supports the conversion to bitset which makes it easy to create an uncompressed version.
    @RickMoynihan @lemire please take a look. Appreciate your time.

  14. added 5 commits that reference this issue on Mar 22, 2024
    6c77633
    568aea7
    b532659
    d49e7e2
    fbce977
  15. added a commit that references this issue on Apr 2, 2024
    01fad43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions