-
Notifications
You must be signed in to change notification settings - Fork 20
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
Base64 import/export of a bitSet ? #22
Comments
In practice...
is an array of 32-bit integers. So it is a simple matter of converting arrays of 32-bit integers to and from base64. It ought to be fairly easy to do. I have never written a base64 codec in JavaScript but it is not very hard to do. It may not be possible to use an existing solution, but coding it up is probably a good week-end project. Pull request invited. |
Thanks for fast answer, and your pedagogy. I thought that native function like atob() and btoa() should be faster than vanilla code, but according to this benchmark (that I have not reproduced), it may be faster to write our own functions. This is better for understanding, and also, control on some parameters (little or big endian ; safe chars in URL +/ -> -_). This time will be useful to ensure encoding/decoding server side (for this project it is PHP). Trying to write my own solution from scratch, I was mature enough to understand that people has a more tested code to acclimate. It is 4:12 in the morning here (after some beers tonight), I’m going to bed happy to have found a working solution, but give me some sleep and tests before submit you a pull request. |
I’m implementing a BitSet for server side (in PHP), to work with the base64 build with this javascript client, and I have questions and comments about your js implementation.
|
Because you want it to be backed by an array. If you want it to be backed by an object, you can just do
You may need |
I was using (index&63), do you recommend modulo ?
Sorry to have not search enough before asking the question, you have already elaborate on this issue #8. I’m concerned because of PHP “arrays”, sparse but memory expensive. My goal is not to be the best implementation in the world, but if possible, not the worst. A recommended package in PHP world is backed on an array of “bits”, yes, a package to have $bits[1] = true. I got a working prototype backed on a PHP array of int. For better memory control, it is possible in PHP to use strings, they are mutable. I’m afraid of the cost of copy and casting, benchmark will decide. |
If the index is non-negative, then the result should be the same. It is possible that |
Some stats from PHP.
It is difficult to have precise memory sizes in this language, the params are set here to verify expectations about sizes. Native boolean random array is faster, it appears with a size similar as native int array, but it could be very heavy if it is full. String is lighter and compete not so bad with arrays, but cast and copy of chars to int have a cost. Native int pseudo array in php maybe lighter than continuous if data are sparse.
PHP has no typed arrays with fixed size, the best way for performances seems to pay for a bigger server. |
Real life usage. Communicate a set of ints to a server. The new short url The previous ones (could be much more longer when only one item is excluded from a set). Humans (or humanists, maybe not so common humans) can work with a corpus of 1000-2000 texts, and build personal sets among those items for a search. Usually, such apps do not allow persistent and communicable URLs, a set is available only for a session. This feature allow to communicate such partitions in 2000/6~336 chars, a descent URL, below the 2000 chars limit. After experiment, it is not a good idea to pack base64 converters in FastBitSet (even if I done it in my code), because it is of a larger interest, especially to replace btoa(), deprecated in Node.js. What could be nice for FastBitSet is import from, or export to: bytes ; because string of bytes is the common i/o for Base64 encoders. I’m not confident enough to propose a pull request, but each methods could be a oneliner: // this.length = «the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus one.» (@see ava.util.BitSet)
// internally, native js array is copied as a typed array, then, the Int32 are read as bytes
const bytes = new Uint8Array(new Int32Array(this.words).buffer, 0, (this.length + 7) >> 3));
this.words = Array.from(new Int32Array(bytes.buffer)); Real life example where a form allow to build a Bitset, send to a server as a Base64 string. |
Have you thought to implement an import/export of a BitSet as a base64 encoding string, so you can send or receive BitSets from server ? If you think it could be useful feature for your project, any suggestion for the implementation is welcome, I need this kind of thing for a project. I need around 200 sequential bits, so it makes a quite short simple string.
The text was updated successfully, but these errors were encountered: