roaring-rs icon indicating copy to clipboard operation
roaring-rs copied to clipboard

Constant time len

Open saik0 opened this issue 3 years ago • 9 comments

Fixes #45 Closes #105

saik0 avatar Feb 07 '22 08:02 saik0

I'm not sure what the right approach to testing this is.

saik0 avatar Feb 07 '22 15:02 saik0

I'm not sure what the right approach to testing this is.

Do you mean benchmarking the constant length or checking that select which uses len() is valid? You already added tests for the select method so... Nothing more to do.

However, checking that the length is valid is maybe a little bit complex, we could maybe add an iter().count() == .len() after all of our proptests or something?

Kerollmops avatar Feb 07 '22 15:02 Kerollmops

However, checking that the length is valid is maybe a little bit complex

This is what I meant. I'm trying to think of a clean way to debug_assert that each time the len changes we compare it against the computed len

saik0 avatar Feb 07 '22 16:02 saik0

This is what I meant. I'm trying to think of a clean way to debug_assert that each time the len changes we compare it against the computed len

Wouldn't it be ok to directly do debug_assert_eq!(self.iter().count(), self.len())? As the macro expansion is done at compile-time, therefore the call to iter/count is only done for debug builds.

I just hope that we haven't implemented the count method on our iterator to only call self.len() 😂

Kerollmops avatar Feb 07 '22 16:02 Kerollmops

I just checked in the playground and the macro expansion indeed works like that: it didn't panic when compiling in release.

Kerollmops avatar Feb 07 '22 16:02 Kerollmops

I just hope that we haven't implemented the count method on our iterator to only call self.len()

Of course it does. Why should it recompute the cached len?

Edit: It's an exact size iterator whose size hint is initialized by RoaringBitmap::len I believe ExactSizeIterator overrides count to return the exact size

saik0 avatar Feb 07 '22 16:02 saik0

Of course it does. Why should it recompute the cached len?

Ok so we should trick by doing a fold instead.

Kerollmops avatar Feb 07 '22 16:02 Kerollmops

Of course it does. Why should it recompute the cached len?

Ok so we should trick by doing a fold instead.

We'd probably have to wrap it in a black_box. LLVM is very clever.

saik0 avatar Feb 07 '22 16:02 saik0

Thinking we should assert invariants #197 before we merge this to be sure we didn't introduce a regression

saik0 avatar Feb 10 '22 23:02 saik0