Yortw / Xid.Net

Ported from https://github.com/rs/xid - A fast, low allocation, zero config, thread-safe 12 byte UUID generator based on the Mongo Object Id algorithm.
https://yortw.github.io/Xid.Net/
MIT License
27 stars 4 forks source link

Many Zeros in the middle of xid running on Macos #1

Closed inebritov closed 3 weeks ago

inebritov commented 3 months ago

I use the Xid.NewXid() generation method and it gives me result with zeros in the middle. I was curious and checked internals of the library, so it seems that function generating for Machine ID (B5-B7) return small number. Also, Process ID (B8-B9) in Docker is less than 255, so I have noticeable amount of zeros in the middle of the Xid:

  Xid.NewXid()
  {cq6jk4jp000000bu30mg}
    _B1: 102
    _B2: 141
    _B3: 58
    _B4: 18
    _B5: 121
    _B6: 0
    _B7: 0
    _B8: 0
    _B9: 1
    _B10: 126
    _B11: 24
    _B12: 45

  {cq6kkkmg000008d6gp90}
    _B1: 102
    _B2: 141
    _B3: 74
    _B4: 82
    _B5: 208
    _B6: 0
    _B7: 0
    _B8: 0
    _B9: 33
    _B10: 166
    _B11: 134
    _B12: 83

On the host machine (.net installed in the system) it looks better, but it still has zero-bytes:

 {cq6md0460004fnfkqhpg}
    _B1: 102
    _B2: 141
    _B3: 102
    _B4: 128
    _B5: 134
    _B6: 0
    _B7: 0
    _B8: 71
    _B9: 221
    _B10: 244
    _B11: 212
    _B12: 115

{cq6md9460004fnfkqhq0}
    _B1: 102
    _B2: 141
    _B3: 102
    _B4: 164
    _B5: 134
    _B6: 0
    _B7: 0
    _B8: 71
    _B9: 221
    _B10: 244
    _B11: 212
    _B12: 117

Environment:

inebritov commented 3 months ago

Reproduced in Docker on Linux

inebritov commented 3 months ago

I’ve checked the code with the debugger, and it looks like the library has a slight difference from the original. Here, in the machine ID generation, we calculate the sum of the hash, but in the original library, the hash bytes are used raw. So I made a pull request to fix that: https://github.com/Yortw/Xid.Net/pull/2

I'm not sure about performance, as the last build shows the Error Message: Test 'Xid_New_Performance' exceeded execution timeout period. I can't figure out what's wrong and I need assistance with that.

Yortw commented 4 weeks ago

I have published a 2.0.1-beta version of the package to nuget, if you'd like to try it and let me know if it works. The tests all pass, benchmarks run and the generated values look good to me.

inebritov commented 3 weeks ago

Yep, tested it, works well:

using XidNet;
Console.WriteLine(Xid.NewXid());
Console.WriteLine(Xid.NewXid());
Console.WriteLine(Xid.NewXid());
Console.WriteLine(Xid.NewXid());

Output:

cs1titrb3p24shn4h8qg
cs1titrb3p24shn4h8r0
cs1titrb3p24shn4h8rg
cs1titrb3p24shn4h8s0
Yortw commented 3 weeks ago

Thanks for the PR and your assistance with testing. New, non-beta, package published now.