Rewriting this some recommendations
wade-welles opened this issue · comments
You can replace your misnamed RandomInt32 (which should be RandomUint32 because it returns a uint32) with a single return rand.Uint32()
Most of the bitwise mess that is intelligible to non-C developers, and often makes them cry when they look at it with binary.(Big|Little)Endian.PutUint(16|32)
I also found a way to shrink the timestamp data to 2 bytes while keeping the most important aspects of it.
If there is any interest I can make pull requests with some of these changes to make it more readable to most Go programmers and make it easier to modify and update because right now I imagine most developers who don't know bitwise operataions very well look at this codebase and cry and its not making it more efficient.
There is tons of efficiency issues in the code, it uses way way more memory than it needs to.
Also unless you are getting incredible speed increase from your custom implementation of base32 encoding. Almost half of your code-base could be replaced with
encoder := base32.NewEncoding("0123456789abcdefghijklmnopqrstuv").WithPadding(base32.NoPadding)
base32Id := encoder.EncodeToString(id)
fmt.Println("custom encoder:", base32Id)
This is not just smaller, its easier to understand, and therefore easier for other developers to interact with. Personally I don't have a problem reading the codebase but I know from experience that many developers would have trouble, and some may cry.
You can see these and other refactoring I did (to the point of being a different library since I merged in bsonid too and added several more features including checksums) @ github.com/multiverse-os/muid
If you are interested in refactoring this library let me know I would be interested in contributing.
Ok to rename the misnamed randInt. The rest is about avoiding allocations and bound checks. If you think you can make the code simpler while not affecting the performance, go ahead.