MutBits class as a mutable Bits? #14
scott-griffiths
started this conversation in
Ideas
Replies: 1 comment
-
After some more thought and experimentation:
This means that the methods in the list above would move out of
Things would be chainable, allowing code like
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Motivation
I was hoping to not have a mutable bits at all (or perhaps only much later) as there are numerous advantages to the immutable class. However, after having done some speed tests I think it's going to be needed sooner.
The task I've profiled is the classic prime sieve for the first 100 million integers. There is example code for this on bitarray's site, so that's a good baseline. I can also compare to bitstring:
This is clearly problematic!
I then compared it to a naive pure Rust implementation (using the bitvec crate) both with an immutable bitvec and one that gets mutated:
The immutable Rust code just has an extra clone() in the loop, but it's taking 95% of the time. This does suggest that using a mutable bits in bitformat could be 30x faster for this task, and quite possibly faster than other optimised libraries.
Things to think about:
Name:
I'm quite tempted to call it
MutBits
, which may be a bit too Rust-like, but the Pythonic name would be BitArray and I feel that name has been overloaded too much elsewhere already.MutBits
also has a name that strongly suggests its difference toBits
, whereasBitArray
isn't as clear.Methods:
There are already a bunch of mutating methods on
Bits
, except of course the don't actually mutate it but just return a new instance instead. I don't want to remove these methods, so I think they just get reimplemented withMutBits
. One question is whetherMutBits
should returnNone
orself
- the first makes it clear that it's mutating, but the second means thatBits
code can be trivially changed toMutBits
. There are probably lots of subtle and not so subtle bugs that could be introduced by mimicing theBits
API here so it might be best to returnNone
at least in the first iteration?we'd need:
byte_swap
insert
invert
overwrite
replace
reverse
rol
ror
set
BitsProxy
class wouldn't be needed at all as it could be replaced byMutBits
.Reader
class use theMutBits
and add some extra methods?Beta Was this translation helpful? Give feedback.
All reactions