Joel VanderWerf said:
It would be nice for ruby to have some bit operation primitives like
extract(start_bit, n_bits) and insert and so on. Then bit stream ops
could be built op from these. The former would make a nice C ext...
I did some rudimentary work (enough for my purposes) here:
http://www.bruschkewitz.de/job/ruby/CBitStream.rb
There is no optimisation at all, just made it work.
I would prefer not to use a C-Extension unless it would be part of Ruby
standard library.
IMHO, there are enough possibilities to optimize in Ruby.
For example, it is not necessary to analyze the values bit by bit.
Instead, if the current buffer byte contains already 1 bit from former
output operations, remaining 7 bits, these 7 bits could be copied at once.
It breaks down to shift the value to be written by some bits (if even
necessary), "or"-ing current buffer byte and assinging rest of output bytes
to buffer.
I did similar work some years before in Modula.
Depending on output buffer representation (byte, word, dword, qword, Array,
String, Bignum), this could be very effective even in Ruby.
So, fitting into the general performance landscape, an implementation done
in Ruby would be enough, because the values to be written will - most
probably - be prepared in Ruby too.
Regards,
Michael B.