hyperuuid 0.2.1-x86_64-linux → 0.4.0-x86_64-linux
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +331 -88
- data/lib/hyperuuid/3.4/hyperuuid_native.so +0 -0
- data/lib/hyperuuid/4.0/hyperuuid_native.so +0 -0
- data/lib/hyperuuid/errors.rb +12 -0
- data/lib/hyperuuid/native/linux-arm64/libhyperuuid.so +0 -0
- data/lib/hyperuuid/native/linux-musl-arm64/libhyperuuid.so +0 -0
- data/lib/hyperuuid/native/linux-musl-x64/libhyperuuid.so +0 -0
- data/lib/hyperuuid/native/linux-x64/libhyperuuid.so +0 -0
- data/lib/hyperuuid/native/osx-arm64/libhyperuuid.dylib +0 -0
- data/lib/hyperuuid/native/osx-x64/libhyperuuid.dylib +0 -0
- data/lib/hyperuuid/native/wasm32-wasip1/hyperuuid.wasm +0 -0
- data/lib/hyperuuid/native/win-arm64/hyperuuid.dll +0 -0
- data/lib/hyperuuid/native/win-x64/hyperuuid.dll +0 -0
- data/lib/hyperuuid/native_platform.rb +13 -7
- data/lib/hyperuuid/runtime.rb +70 -20
- data/lib/hyperuuid/uuid.rb +32 -13
- data/lib/hyperuuid/wasm_runtime.rb +259 -0
- data/lib/hyperuuid.rb +166 -35
- metadata +18 -22
- data/lib/hyperuuid/native/README.md +0 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e7bb71114e32b62e18d3acea28221fbd111f2aa6d7925cb106d3b32f852bd364
|
|
4
|
+
data.tar.gz: cae2dd2712f450af1e28e1f378c5c54117e2b48ed63cdea7cdb267944cbdd9af
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0bb0977d503d56bc8ce2a994213565f1f178e4779fe2d77023d377b1320b7b50b66de809ae318202102a54a1bc7bfdd804c0084188029f961f6e1a0b60d11489
|
|
7
|
+
data.tar.gz: 418665e67759e03373d856321872def9e5edf3d5479a04c8febc0410159a1787749cc8c753576cfe0bb5116d1e3da9f2352e53e7ea4eceb03a94f893a68ad5b9
|
data/README.md
CHANGED
|
@@ -2,20 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://github.com/SkunkWerkx/HyperUuid/actions/workflows/ci.yml)
|
|
4
4
|
[](https://rubygems.org/gems/hyperuuid)
|
|
5
|
+
[](https://github.com/SkunkWerkx/HyperUuid/blob/master/LICENSE)
|
|
5
6
|
|
|
6
7
|
**Ruby's own stdlib stops at `SecureRandom.uuid` — random v4, full stop. No v5, no v6, no v7. This gem is the whole RFC, with zero gem dependency beyond `Fiddle` (which ships with every Ruby install) — and it's faster than `SecureRandom.uuid` too.**
|
|
7
8
|
|
|
8
9
|
RFC 9562 UUID v4 (random), v5 (deterministic), v6 and v7 (time-sortable) generation, with
|
|
9
|
-
|
|
10
|
-
[Magnus](https://github.com/matsadler/magnus) — the Rust core linked
|
|
11
|
-
VM, auto-selected when loadable — which redefines the low-level
|
|
12
|
-
on require; everything above them (`Uuid`, the module doors
|
|
13
|
-
byte-for-byte between backends. The
|
|
14
|
-
|
|
15
|
-
dlopen/dlsym plus a raw C-ABI
|
|
16
|
-
|
|
17
|
-
`
|
|
18
|
-
|
|
10
|
+
three backends sharing one public surface. Ruby 3.3 is the floor. The fast path is a native
|
|
11
|
+
extension built with [Magnus](https://github.com/matsadler/magnus) — the Rust core linked
|
|
12
|
+
directly into the Ruby VM, auto-selected when loadable — which redefines the low-level
|
|
13
|
+
`Runtime` methods in place on require; everything above them (`Uuid`, the module doors and
|
|
14
|
+
their argument checks, batch slicing) is shared byte-for-byte between backends. The
|
|
15
|
+
universal fallback calls the native `libhyperuuid` shared library via
|
|
16
|
+
[`Fiddle`](https://docs.ruby-lang.org/en/master/Fiddle.html) — dlopen/dlsym plus a raw C-ABI
|
|
17
|
+
call, no runtime bridge. A third backend runs the same core as a WebAssembly module inside
|
|
18
|
+
the [`wasmtime`](https://rubygems.org/gems/wasmtime) gem, for any platform with no native
|
|
19
|
+
build at all. `HyperUuid::BACKEND` reports which one is live; `HYPERUUID_PURE=1` forces
|
|
20
|
+
Fiddle, `HYPERUUID_WASM=1` the wasm module — see [Backends](#backends).
|
|
19
21
|
|
|
20
22
|
```ruby
|
|
21
23
|
require "hyperuuid"
|
|
@@ -33,12 +35,31 @@ id4.to_sql_order # byte order SQL Server's uniqueidentifier needs to sort by cre
|
|
|
33
35
|
batch = HyperUuid.new_v7_batch(1000)
|
|
34
36
|
```
|
|
35
37
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
## The doors
|
|
39
|
+
|
|
40
|
+
| Door | Returns | Takes |
|
|
41
|
+
|---|---|---|
|
|
42
|
+
| `new_v4` | `Uuid` | — |
|
|
43
|
+
| `new_v5(namespace, name)` | `Uuid`, the same one for the same pair | a `Uuid` namespace (`Namespaces::DNS`/`URL`/`OID`/`X500`, or your own) and a `String` name — text is hashed as UTF-8, a binary String as its bytes, and it may be empty |
|
|
44
|
+
| `new_v6(time = nil)` `new_v7(time = nil)` | `Uuid` | nothing (now), a `Time`, or an Integer of Unix-epoch milliseconds |
|
|
45
|
+
| `new_v6_batch(count, time = nil)` `new_v7_batch(count, time = nil)` | `Array` of `Uuid` | a count, and the same time forms |
|
|
46
|
+
| `new_v6_batch_bytes(count, time = nil)` `new_v7_batch_bytes(count, time = nil)` | one binary `String`, 16 bytes per UUID | as the batch doors — see [Bulk generation into bytes](#bulk-generation-into-bytes) |
|
|
47
|
+
| `native_version` | the loaded core's `"major.minor.patch"` | — |
|
|
48
|
+
| `available?` | `true`/`false`, never raising | — |
|
|
49
|
+
|
|
50
|
+
Every generating door returns `HyperUuid::Uuid`, a minimal value object — this gem has no
|
|
51
|
+
runtime dependency on the `uuid` gem:
|
|
52
|
+
|
|
53
|
+
| On `Uuid` | |
|
|
54
|
+
|---|---|
|
|
55
|
+
| `Uuid.parse(text)` | the 8-4-4-4-12 hyphenated form `#to_s` produces, either case, and nothing else — an `ArgumentError` otherwise |
|
|
56
|
+
| `Uuid.new(bytes)` | wraps 16 raw RFC 9562-ordered bytes |
|
|
57
|
+
| `Uuid::NIL` `Uuid::MAX` | the RFC 9562 §5.9/§5.10 special values |
|
|
58
|
+
| `#bytes` `#to_s` `#version` `#variant` | the frozen binary String, the hyphenated text, the version nibble, the variant bits |
|
|
59
|
+
| `#timestamp(raise_on_mismatch: true)` | the UTC `Time` embedded in a version 6 or 7 UUID; `nil` instead of an `ArgumentError` for any other version when passed `false` |
|
|
60
|
+
| `#to_sql_order` `#from_sql_order` | to and from the byte order SQL Server's `uniqueidentifier` sorts by |
|
|
61
|
+
| `==` `eql?` `hash` `<=>` | value equality and byte-order comparison (`Comparable`) |
|
|
62
|
+
|
|
42
63
|
`#to_sql_order`/`#from_sql_order` convert a version 6 or 7 UUID to and from the byte order SQL
|
|
43
64
|
Server's `uniqueidentifier` needs on the wire to sort by creation order (`#to_sql_order`
|
|
44
65
|
dispatches on the UUID's own version, matching `#timestamp`'s convention) — computed once in
|
|
@@ -46,18 +67,50 @@ the native Rust core rather than reimplemented in Ruby, and verified there (and
|
|
|
46
67
|
against the real `System.Data.SqlTypes.SqlGuid` comparator in the C# binding's test suite).
|
|
47
68
|
Same-millisecond v6 UUIDs aren't guaranteed to sort correctly afterward — v6 has no counter,
|
|
48
69
|
so `clock_seq`/`node` (not the timestamp) decide ties, the same pre-existing RFC 9562 v6
|
|
49
|
-
limitation plain order already has. `#from_sql_order` figures out which version to invert by
|
|
50
|
-
provably collision-free between the two (see the method's own
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
70
|
+
limitation plain order already has. `#from_sql_order` figures out which version to invert by
|
|
71
|
+
checking a byte position that's provably collision-free between the two (see the method's own
|
|
72
|
+
doc comment).
|
|
73
|
+
|
|
74
|
+
### Errors
|
|
75
|
+
|
|
76
|
+
Two exceptions are this gem's own, and both carry the same message on every backend:
|
|
77
|
+
|
|
78
|
+
- `HyperUuid::TimestampOutOfRangeError` — the time cannot be embedded: past version 7's
|
|
79
|
+
48-bit millisecond field, past version 6's 60-bit one, or negative (a `Time` before the Unix
|
|
80
|
+
epoch included).
|
|
81
|
+
- `HyperUuid::RandomSourceError` — the operating system's random source failed.
|
|
82
|
+
|
|
83
|
+
Through 0.3.0 these were reachable only as `HyperUuid::Runtime::TimestampOutOfRangeError` and
|
|
84
|
+
`HyperUuid::Runtime::RandomSourceError`; those names remain, as aliases of the same classes.
|
|
85
|
+
|
|
86
|
+
Everything else is a caller bug and raises Ruby's own error for it, checked once in the shared
|
|
87
|
+
doors rather than left to whichever backend is live: a `TypeError` for a time that is not a
|
|
88
|
+
`Time`, an Integer or `nil`, for a count that is not an Integer, for a namespace that is not a
|
|
89
|
+
`Uuid` or a name that is not a `String`; an `ArgumentError` for a count outside
|
|
90
|
+
0..2³² − 1.
|
|
91
|
+
|
|
92
|
+
### Is the native core there?
|
|
93
|
+
|
|
94
|
+
`HyperUuid.available?` answers whether a backend actually loaded — probed once, cached, never
|
|
95
|
+
raising — for a consumer with a fallback of its own:
|
|
96
|
+
|
|
97
|
+
```ruby
|
|
98
|
+
id = HyperUuid.available? ? HyperUuid.new_v7.to_s : SecureRandom.uuid
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
`HyperUuid.native_version` reads the version out of the loaded core itself (its
|
|
102
|
+
`hyperuuid_version` export), so a mismatch against `HyperUuid::VERSION` can be named before
|
|
103
|
+
the first UUID is minted. `HyperUuid::BACKEND` says which backend was *selected*, which is not
|
|
104
|
+
the same question: it reads `:fiddle` on a platform with no library at all, because that is
|
|
105
|
+
the backend whose first call explains what is missing — a `LoadError` naming the path it
|
|
106
|
+
looked for, or `HyperUuid::NativePlatform::UnsupportedPlatformError` naming the platform.
|
|
54
107
|
|
|
55
108
|
## Why not `SecureRandom.uuid`?
|
|
56
109
|
|
|
57
110
|
`SecureRandom.uuid` only ever gives you a random v4 UUID — Ruby's stdlib has no built-in v5, v6, or v7 at all. If you need more than that, the choice is really "which gem":
|
|
58
111
|
|
|
59
112
|
1. **Full RFC 9562 coverage, one gem, zero extra dependency.** v4/v5/v6/v7 plus batch generation plus `Nil`/`Max`, and the only thing this gem adds to your `Gemfile.lock` beyond `Fiddle` — which is Ruby's own bundled FFI layer, not a third-party C extension to compile.
|
|
60
|
-
2. **No native-extension compile step.** Third-party UUID gems that go beyond v4 are typically pure Ruby or wrap a C extension compiled at install time; this gem ships its fast path as a prebuilt platform-gem extension and its fallback as a `dlopen`ed prebuilt library — either way, nothing
|
|
113
|
+
2. **No native-extension compile step.** Third-party UUID gems that go beyond v4 are typically pure Ruby or wrap a C extension compiled at install time; this gem ships its fast path as a prebuilt platform-gem extension and its fallback as a `dlopen`ed prebuilt library — either way, the gem itself compiles nothing at install time ([Install](#install) has the one caveat, which is Fiddle's own).
|
|
61
114
|
3. **Batch generation.** `new_v7_batch(1000)` shares one timestamp capture, one random-bytes fetch, and one counter reservation across the whole batch instead of paying per-item overhead a thousand times over.
|
|
62
115
|
4. **Cross-language consistency.** The same Rust core mints v5 namespace UUIDs for Python, Go, C#, and every other binding in this repo — verified in CI to match Python's own `uuid.uuid5` byte-for-byte. If your system isn't Ruby-only, no Ruby-only gem can offer that.
|
|
63
116
|
|
|
@@ -72,7 +125,7 @@ bytes = HyperUuid.new_v7_batch_bytes(1000)
|
|
|
72
125
|
first = bytes[0, 16] # ready for a BINARY(16) bind parameter
|
|
73
126
|
```
|
|
74
127
|
|
|
75
|
-
**About
|
|
128
|
+
**About 20x faster than `new_v7_batch`** for a 1000-UUID batch (10.4 µs versus 210 µs). The native call is identical — the difference is that `new_v7_batch` then allocates a `Uuid` object and its byte Strings for every item on top of it. This hands back the bytes the native core already produced, untouched.
|
|
76
129
|
|
|
77
130
|
The catch, and it inverts the advice: **if you need `Uuid` objects, keep using `new_v7_batch`.** Slicing these bytes into objects yourself just relocates the identical allocations into your own code, and measures no better — sometimes worse. Reach for the byte form only when bytes are the destination: a bind parameter, a wire format, a bulk load.
|
|
78
131
|
|
|
@@ -80,34 +133,144 @@ Slice it with `bytes[i * 16, 16]` — which is exactly what `new_v7_batch` does
|
|
|
80
133
|
|
|
81
134
|
## Benchmarks
|
|
82
135
|
|
|
83
|
-
|
|
136
|
+
`ruby benchmark/uuid_benchmark.rb` reproduces every table here and the bytes-versus-objects
|
|
137
|
+
comparison above. Its first line names the backend, core version and Ruby it measured, so
|
|
138
|
+
run it again under `HYPERUUID_PURE=1` or `HYPERUUID_WASM=1` for the other two backends.
|
|
139
|
+
|
|
140
|
+
Real numbers, `benchmark-ips` on Ruby 4.0.7, linux-x64 on an Intel Core i9-11900H (`ruby benchmark/uuid_benchmark.rb`) — not claimed, measured. With the Magnus backend (the default wherever the extension loads):
|
|
84
141
|
|
|
85
142
|
| Call | i/s | vs `SecureRandom.uuid` |
|
|
86
143
|
|---|---:|---:|
|
|
87
|
-
| `SecureRandom.uuid` |
|
|
88
|
-
| `HyperUuid.
|
|
89
|
-
| `HyperUuid.
|
|
90
|
-
| `HyperUuid.
|
|
91
|
-
| `HyperUuid.
|
|
92
|
-
| `HyperUuid.new_v7` (current time) |
|
|
93
|
-
| `HyperUuid.
|
|
144
|
+
| `SecureRandom.uuid` | 872,365 | baseline |
|
|
145
|
+
| `HyperUuid.new_v4` | 4,278,000 | **4.9x faster** |
|
|
146
|
+
| `HyperUuid.new_v7` (explicit ms) | 3,517,000 | **4.0x faster** |
|
|
147
|
+
| `HyperUuid.new_v6` (explicit ms) | 3,487,000 | **4.0x faster** |
|
|
148
|
+
| `HyperUuid.new_v6` (current time) | 3,324,000 | **3.8x faster** |
|
|
149
|
+
| `HyperUuid.new_v7` (current time) | 3,320,000 | **3.8x faster** |
|
|
150
|
+
| `HyperUuid.new_v5` | 1,838,000 | 2.1x faster |
|
|
94
151
|
|
|
95
|
-
|
|
152
|
+
A `HyperUuid.new_v4` — real entropy, correct version/variant bits, minted by the shared Rust core — costs a fifth of what `SecureRandom.uuid` does, because the Magnus extension is an ordinary native method call with nothing marshalled around it.
|
|
96
153
|
|
|
97
|
-
The
|
|
154
|
+
The "current time" rows deserve a footnote, because they will not look like this everywhere. Here they land beside the explicit-ms rows: the only difference between the two is one `Process.clock_gettime(CLOCK_REALTIME)` wall-clock read, and on this machine that read is too cheap to see. On a machine with a slow clock it is the whole story — where a virtualized clock defeats the vDSO fast path the same read costs ~1µs, which puts both current-time rows at parity with `SecureRandom.uuid` while the explicit-ms rows stay well ahead of it. `SecureRandom.uuid` never reads a clock — random v4 is the only thing it does. If your clock is slow and you are minting many, read it once and pass the timestamp, or use the batch doors.
|
|
98
155
|
|
|
99
|
-
The Fiddle fallback (`HYPERUUID_PURE=1`, and any platform without a prebuilt extension) keeps its own diet — a reused thread-local scratch buffer instead of two GC-finalizer-registering mallocs per call, zero-copy `String` passes for read-only inputs, an unsynchronized fast path past the load mutex — landing at 1.
|
|
156
|
+
The Fiddle fallback (`HYPERUUID_PURE=1`, and any platform without a prebuilt extension) keeps its own diet — a reused thread-local scratch buffer instead of two GC-finalizer-registering mallocs per call, zero-copy `String` passes for read-only inputs, an unsynchronized fast path past the load mutex — landing at 1.25x slower than `SecureRandom.uuid` for v4 (1.26 µs against 1.01 µs in its own run) and 1.45x slower for v6/v7, with the same structural story as before: `Fiddle`'s interpreted marshalling is the floor, and the batch doors are how you amortize it.
|
|
100
157
|
|
|
101
158
|
Batch generation still amortizes per-call cost on both backends — one native call for the whole batch:
|
|
102
159
|
|
|
103
160
|
| Call | i/s (Magnus backend) |
|
|
104
161
|
|---|---:|
|
|
105
|
-
| `new_v6` × 1000 (individual) |
|
|
106
|
-
| `new_v6_batch(1000)` |
|
|
107
|
-
| `new_v7` × 1000 (individual) |
|
|
108
|
-
| `new_v7_batch(1000)` |
|
|
162
|
+
| `new_v6` × 1000 (individual) | 3,498 |
|
|
163
|
+
| `new_v6_batch(1000)` | 4,633 (**1.3x**) |
|
|
164
|
+
| `new_v7` × 1000 (individual) | 3,539 |
|
|
165
|
+
| `new_v7_batch(1000)` | 4,677 (**1.3x**) |
|
|
166
|
+
|
|
167
|
+
The multiplier is small on this backend for the best reason available: the individual calls are cheap, so there is little waste left to amortize, and what `new_v7_batch` spends its 214 µs on is building a thousand `Uuid` objects — the byte form above does the same native work in 10 µs. On the Fiddle backend, where each call costs 1.4 µs, the same batch is 6.7x the loop. If you need v5/v6/v7, need many at once, or need this Ruby service's IDs to agree byte-for-byte with a Go or Python service's, that's what this gem is for — and now it's the fast option too, not just the capable one.
|
|
168
|
+
|
|
169
|
+
## Backends
|
|
170
|
+
|
|
171
|
+
| `HyperUuid::BACKEND` | What runs | Chosen when |
|
|
172
|
+
|---|---|---|
|
|
173
|
+
| `:native` | the core linked into a Magnus extension | a precompiled platform gem carries an extension for this Ruby's ABI — see [Install](#install) |
|
|
174
|
+
| `:fiddle` | `libhyperuuid` for this platform, `dlopen`ed through Fiddle | no extension loads; also the answer when nothing loads at all |
|
|
175
|
+
| `:wasm` | the core as a `wasm32-wasip1` module inside the `wasmtime` gem | no extension and no library for this platform, and `wasmtime` is installed — see [WebAssembly (wasmtime)](#webassembly-wasmtime) |
|
|
176
|
+
|
|
177
|
+
Selection happens once, at `require`, in that order. Two environment variables override it:
|
|
178
|
+
|
|
179
|
+
| Variable | Effect |
|
|
180
|
+
|---|---|
|
|
181
|
+
| `HYPERUUID_WASM` | forces `:wasm`; `require` raises a `LoadError` naming the gem if `wasmtime` is missing |
|
|
182
|
+
| `HYPERUUID_PURE` | forces `:fiddle`, even where an extension would load |
|
|
183
|
+
|
|
184
|
+
Both are read for presence, not value — set to anything at all, `0` and the empty string
|
|
185
|
+
included, they force their backend — and `HYPERUUID_WASM` wins when both are set. A forced
|
|
186
|
+
backend that turns out to have nothing to load does not fall through to another one: the
|
|
187
|
+
first call raises, and `HyperUuid.available?` answers `false`.
|
|
188
|
+
|
|
189
|
+
**Threads.** Every backend is safe to call from any number of threads. The extension runs
|
|
190
|
+
under the GVL. Fiddle releases the GVL for the duration of each call, so that is the one
|
|
191
|
+
backend where Ruby threads run the core truly in parallel — each through its own scratch
|
|
192
|
+
buffer, with the core's v7 counter shared and atomic. The wasm backend serializes every call
|
|
193
|
+
on one Mutex around one instance. `spec/hyperuuid_spec.rb`'s "concurrent callers" examples
|
|
194
|
+
run under all three.
|
|
195
|
+
|
|
196
|
+
**Ractors.** Main Ractor only, on every backend: called from another Ractor the doors raise
|
|
197
|
+
`Ractor::UnsafeError` (`Ractor::IsolationError` under wasm).
|
|
198
|
+
|
|
199
|
+
## WebAssembly (wasmtime)
|
|
200
|
+
|
|
201
|
+
The Rust core also ships inside this gem as a `wasm32-wasip1` module
|
|
202
|
+
(`lib/hyperuuid/native/wasm32-wasip1/hyperuuid.wasm`), and the
|
|
203
|
+
[`wasmtime`](https://rubygems.org/gems/wasmtime) gem can run it in-process. This is the
|
|
204
|
+
inverse of ruby.wasm — not Ruby inside a wasm sandbox, but a wasm module inside Ruby — and
|
|
205
|
+
it is the one backend that needs no shared library for the platform it runs on: no
|
|
206
|
+
`dlopen`, no Magnus extension, nothing compiled against this Ruby's ABI. Everything above
|
|
207
|
+
`Runtime` (`Uuid`, the module doors, batch slicing) is the same code the other two backends
|
|
208
|
+
run, and `spec/wasm_backend_spec.rb` pins that the outputs agree with the Fiddle backend
|
|
209
|
+
byte for byte.
|
|
210
|
+
|
|
211
|
+
`wasmtime` is deliberately **not** a dependency of this gem; a consumer who wants this path
|
|
212
|
+
installs it:
|
|
109
213
|
|
|
110
|
-
|
|
214
|
+
```sh
|
|
215
|
+
gem install wasmtime
|
|
216
|
+
HYPERUUID_WASM=1 ruby -rhyperuuid -e 'p HyperUuid::BACKEND' # => :wasm
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
`HYPERUUID_WASM=1` forces the backend (and raises a `LoadError` naming the gem if it is
|
|
220
|
+
missing). Without it, the wasm backend is only ever chosen automatically when there is no
|
|
221
|
+
native library for this platform at all — no Magnus extension and no `libhyperuuid` for the
|
|
222
|
+
RID — and `wasmtime` happens to be installed. No supported platform's behavior changes just
|
|
223
|
+
because this backend exists.
|
|
224
|
+
|
|
225
|
+
Two things are different under the sandbox, both by necessity. A wasm guest only sees its own
|
|
226
|
+
linear memory, so every buffer the core fills comes from the module's own exported `malloc`
|
|
227
|
+
(the same wasi-libc allocator Rust's std uses on that target) and is read back with
|
|
228
|
+
`Memory#read` — using the guest's allocator rather than a host-picked offset is what keeps a
|
|
229
|
+
batch from being clobbered by the guest's next allocation. And a `Wasmtime::Store` is
|
|
230
|
+
single-threaded, so every call is serialized under one Mutex around one shared instance,
|
|
231
|
+
which is also what keeps the core's v7 counter (it lives inside the instance) monotonic
|
|
232
|
+
across threads and batches, exactly as the one dlopen'd library does natively.
|
|
233
|
+
|
|
234
|
+
Measured, same box and same session as the benchmarks above (Ruby 4.0.7, linux-x64,
|
|
235
|
+
wasmtime 48.0.1), all three backends:
|
|
236
|
+
|
|
237
|
+
| Call | wasmtime | Fiddle | Magnus |
|
|
238
|
+
|---|---:|---:|---:|
|
|
239
|
+
| `new_v4` | 1.07 µs | 1.26 µs | 234 ns |
|
|
240
|
+
| `new_v7` (explicit ms) | 1.44 µs | 1.47 µs | 284 ns |
|
|
241
|
+
| `new_v7_batch_bytes(1000)` | 22.6 µs | 13.6 µs | 10.4 µs |
|
|
242
|
+
| `new_v7_batch(1000)` → `Uuid` objects | 233 µs | 227 µs | 210 µs |
|
|
243
|
+
|
|
244
|
+
So a single call costs what it costs on Fiddle, about five times the Magnus extension, and
|
|
245
|
+
the batch doors amortize it the same way they do for Fiddle: the byte batch is twice the
|
|
246
|
+
native cost, and the object batch is nearly the same on all three because building the objects is
|
|
247
|
+
the whole of it. The guest's own work is not where the time goes — the identical module
|
|
248
|
+
fills a thousand UUIDs in 11 µs under GraalWasm's JIT — it is the crossing.
|
|
249
|
+
|
|
250
|
+
## Verifying provenance
|
|
251
|
+
|
|
252
|
+
Every gem RubyGems.org serves — the universal fallback and each of the five precompiled
|
|
253
|
+
platform gems — carries its own GitHub build-provenance attestation, signed directly by
|
|
254
|
+
this repo's own `release.yml` (the `rubygems-publish` job attests `ruby/pkg/*.gem` right
|
|
255
|
+
before the push), so plain `--repo` verifies any of them:
|
|
256
|
+
|
|
257
|
+
```sh
|
|
258
|
+
gem fetch hyperuuid -v X.Y.Z --platform <platform> # or omit --platform for the universal gem
|
|
259
|
+
gh attestation verify hyperuuid-X.Y.Z-<platform>.gem --repo SkunkWerkx/HyperUuid
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
That's the release's second layer of checking, not the only one: before any gem gets built,
|
|
263
|
+
the same job verifies every native binary it packs — the Fiddle libraries, the Magnus
|
|
264
|
+
extensions (one per Ruby ABI per platform) and the wasm module — against *their own*
|
|
265
|
+
attestations — those are signed from `SkunkWerkx/.github` by `hyper-build-native.yml`, so
|
|
266
|
+
that check needs `--signer-repo SkunkWerkx/.github` added — and refuses to proceed on an
|
|
267
|
+
unverified one. RubyGems.org has no unpublish and no
|
|
268
|
+
duplicate-version overwrite, so this all happens while a bad artifact is still reversible.
|
|
269
|
+
The release run's job summary then re-fetches every gem from the CDN and records
|
|
270
|
+
attested-vs-served digests, turning "rubygems.org stores an upload verbatim" into a
|
|
271
|
+
per-release measurement rather than an assumption — see
|
|
272
|
+
[csharp/README.md's provenance section](../csharp/README.md#native-binary-provenance) for
|
|
273
|
+
more on why `--signer-repo` is needed for some artifacts here and not others.
|
|
111
274
|
|
|
112
275
|
## Install
|
|
113
276
|
|
|
@@ -115,12 +278,15 @@ The batch multiplier shrank from 11x to ~3.8x for the best reason available: the
|
|
|
115
278
|
gem install hyperuuid
|
|
116
279
|
```
|
|
117
280
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
linux-
|
|
121
|
-
|
|
122
|
-
`
|
|
123
|
-
|
|
281
|
+
Six gems are published per release: one universal `ruby`-platform gem (Fiddle, with every
|
|
282
|
+
platform's native library and the wasm module bundled) plus five precompiled Magnus platform
|
|
283
|
+
gems (`x86_64-linux`, `aarch64-linux`, `arm64-darwin`, `x64-mingw-ucrt`,
|
|
284
|
+
`aarch64-mingw-ucrt`) that `gem install` and `bundle` auto-select when they match. There is
|
|
285
|
+
no `x86_64-darwin` platform gem: an Intel Mac installs the universal gem and runs on Fiddle
|
|
286
|
+
over the bundled `osx-x64` library, as Alpine does. A platform
|
|
287
|
+
gem carries the same libraries and module beside its extensions, so the Fiddle and wasm
|
|
288
|
+
backends are still there behind `HYPERUUID_PURE` and `HYPERUUID_WASM`. No extra configuration
|
|
289
|
+
needed either way.
|
|
124
290
|
|
|
125
291
|
Selection has **two** axes here, unlike every other binding in this repo. A Magnus extension
|
|
126
292
|
is bound to one Ruby minor ABI — there's no `abi3` equivalent to collapse the version axis the
|
|
@@ -128,11 +294,20 @@ way [the Python binding's](../python/) wheels do — so each platform gem is a "
|
|
|
128
294
|
carrying one compiled extension per supported Ruby, under `lib/hyperuuid/<minor>/`, and picks
|
|
129
295
|
one at `require` time:
|
|
130
296
|
|
|
131
|
-
| Ruby |
|
|
132
|
-
| --- | --- | --- |
|
|
133
|
-
| 4.0 (primary) | Magnus, `
|
|
134
|
-
| 3.4 (
|
|
135
|
-
| 3.
|
|
297
|
+
| Ruby | glibc Linux, macOS, Windows — x64 and arm64 | musl Linux (Alpine) — x64 and arm64 | anywhere else |
|
|
298
|
+
| --- | --- | --- | --- |
|
|
299
|
+
| 4.0 (primary) | Magnus, `BACKEND == :native` | Fiddle | wasm, if `wasmtime` is installed |
|
|
300
|
+
| 3.4 (until its EOL 2028-03-31) | Magnus, `BACKEND == :native` | Fiddle | wasm, if `wasmtime` is installed |
|
|
301
|
+
| 3.3 (the floor) | Fiddle | Fiddle | wasm, if `wasmtime` is installed |
|
|
302
|
+
|
|
303
|
+
What stands behind each cell: CI runs the whole suite for the first column on every push —
|
|
304
|
+
Magnus on Ruby 3.4 and 4.0, Fiddle and wasm on 4.0, on all five platforms with a CI leg
|
|
305
|
+
(Intel macOS has none) — and the Fiddle
|
|
306
|
+
suite for the musl column inside an Alpine container. The 3.3 row is the same Fiddle code
|
|
307
|
+
path, run with the Docker command under [Development](#development) (Ruby 3.3 with the
|
|
308
|
+
Fiddle 1.1.2 it ships) rather than on every push. The last column — wasm chosen
|
|
309
|
+
automatically because nothing native exists — has no leg of its own: CI runs the wasm
|
|
310
|
+
backend's suite forced, on platforms that have native builds too.
|
|
136
311
|
|
|
137
312
|
The platform gems declare `required_ruby_version >= 3.4, < 4.1` precisely so RubyGems
|
|
138
313
|
*declines* them outside that range and resolves the universal gem instead — a wrong-ABI
|
|
@@ -143,43 +318,111 @@ don't link libruby at all, so one can load successfully against the wrong ABI an
|
|
|
143
318
|
later. When 3.4 goes EOL it simply leaves the matrix and its users fall back to Fiddle, which
|
|
144
319
|
is exactly what the fallback is for.
|
|
145
320
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
`
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
`
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
`
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
the
|
|
321
|
+
**musl.** RubyGems does not tell musl from glibc for these gems: on Alpine, `gem install`
|
|
322
|
+
picks the `x86_64-linux` or `aarch64-linux` platform gem exactly as it does on any other
|
|
323
|
+
Linux (checked with RubyGems 4.0.20 on `x86_64-linux-musl`). The Magnus extension inside is
|
|
324
|
+
linked against glibc and cannot load there — `require` fails cleanly on the missing
|
|
325
|
+
`ld-linux` loader — so the gem falls back to Fiddle, which loads the musl build of the core
|
|
326
|
+
(`native/linux-musl-x64` or `native/linux-musl-arm64`, chosen from `RUBY_PLATFORM`) that
|
|
327
|
+
every gem carries. Alpine therefore works as installed, on the Fiddle backend and at Fiddle's
|
|
328
|
+
speed. Releases through 0.3.0 carried no musl library: there the fallback found only the
|
|
329
|
+
glibc one, and the first call raised `Fiddle::DLError`.
|
|
330
|
+
|
|
331
|
+
**Nothing in this gem is ever compiled, on any platform.** Its one dependency can be:
|
|
332
|
+
`fiddle` is a bundled gem on Ruby 4.0 and a default gem on 3.3 and 3.4, and `gem install` is
|
|
333
|
+
satisfied by the copy Ruby ships. Bundler resolves the newest `fiddle` on rubygems.org
|
|
334
|
+
instead, and when that is newer than the one your Ruby ships — true today on 3.3 and 3.4, not
|
|
335
|
+
on 4.0 — it builds Fiddle's own C extension, which takes a compiler and libffi's headers
|
|
336
|
+
(`apk add build-base libffi-dev` on Alpine). Holding `fiddle` at your Ruby's version in the
|
|
337
|
+
`Gemfile.lock` avoids that.
|
|
338
|
+
|
|
339
|
+
Both Windows architectures get a Magnus gem. MinGW is the *only* Windows flavour `rb-sys`
|
|
340
|
+
targets (`x64-mingw-ucrt` and `aarch64-mingw-ucrt`, both `supported: true` in its own
|
|
341
|
+
`data/toolchains.json`); the one it has no support for is MSVC. Windows is also where the
|
|
342
|
+
Fiddle fallback costs the most: measured on win-x64, Ruby 3.4, the Magnus backend does
|
|
343
|
+
`new_v4` in 406ns against Fiddle's 2407ns (**5.9x**) and `new_v7` in 595ns against 2759ns
|
|
344
|
+
(**4.6x**) — a far wider gap than any Linux or macOS leg shows — and on real Windows-on-ARM
|
|
345
|
+
hardware, Ruby 4.0.6, `new_v4` in 416ns against Fiddle's 2299ns (**5.5x**) and `new_v7` in
|
|
346
|
+
621ns against 2474ns (**4.0x**), the same shape. Both extensions are built for the `gnullvm`
|
|
347
|
+
Rust targets rather than `gnu` — the same mingw-w64/UCRT ABI RubyInstaller's Ruby uses,
|
|
348
|
+
linked with LLVM and compiler-rt instead of GCC and a statically-linked libgcc, which is what
|
|
349
|
+
keeps the shipped extension small. The build script, and the two flags that are load-bearing
|
|
350
|
+
on the ARM leg (a static libunwind, and a clang-spelled `--target` for bindgen), live in the
|
|
351
|
+
forge's `ruby-magnus` action (`build-magnus.sh`), shared with every other Hyper* repo.
|
|
352
|
+
|
|
353
|
+
## Development
|
|
354
|
+
|
|
355
|
+
Everything below runs from a checkout, with `rust/` beside `ruby/`; none of it is needed to
|
|
356
|
+
use the gem. The Fiddle and wasm backends find the in-repo builds on their own when nothing
|
|
357
|
+
is staged under `lib/hyperuuid/native/`. The Magnus backend does not — an extension has to be
|
|
358
|
+
built for the Ruby you are running and put where `require` looks — and that is what
|
|
359
|
+
`rake native:dev` is for.
|
|
360
|
+
|
|
361
|
+
```sh
|
|
362
|
+
cd rust
|
|
363
|
+
cargo build --release # libhyperuuid, what the Fiddle backend loads
|
|
364
|
+
cargo build --release --target wasm32-wasip1 # hyperuuid.wasm, what the wasm backend loads
|
|
365
|
+
|
|
366
|
+
cd ../ruby
|
|
367
|
+
bundle install
|
|
368
|
+
bundle exec rake native:dev # build the Magnus extension for this Ruby and stage it
|
|
369
|
+
bundle exec rspec # BACKEND == :native
|
|
370
|
+
HYPERUUID_PURE=1 bundle exec rspec # BACKEND == :fiddle
|
|
371
|
+
HYPERUUID_WASM=1 bundle exec rspec # BACKEND == :wasm
|
|
372
|
+
bundle exec rake docs:check # every public object carries a doc comment
|
|
373
|
+
ruby benchmark/uuid_benchmark.rb # prints the backend it measured
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
`rake native:dev` runs `cargo ruby-ext` (an alias in `rust/.cargo/config.toml` that builds the
|
|
377
|
+
`ruby` feature into `rust/target/ruby/`, never over the plain library in
|
|
378
|
+
`rust/target/release/`) and copies the result to
|
|
379
|
+
`lib/hyperuuid/<minor>/hyperuuid_native.<so|bundle>` — the path `lib/hyperuuid.rb` tries
|
|
380
|
+
first. The copy is also a rename, and it is the step `cargo ruby-ext` alone does not do: cargo
|
|
381
|
+
names its output `libhyperuuid.so`, and Ruby derives the `Init_` function it calls from the
|
|
382
|
+
file name it was asked to `require`. Three things worth knowing:
|
|
383
|
+
|
|
384
|
+
- **Run it again after changing `rust/`.** The staged file is a copy; nothing rebuilds it.
|
|
385
|
+
- **One staging per Ruby.** An extension is bound to one Ruby minor, so under a second Ruby
|
|
386
|
+
(`RBENV_VERSION=3.4.11 bundle exec rake native:dev`, say) the task rebuilds for that ABI
|
|
387
|
+
and stages beside the first, each Ruby loading its own.
|
|
388
|
+
- **Without it, `bundle exec rspec` still passes — on Fiddle.** The examples under "native
|
|
389
|
+
backend" report themselves pending with `BACKEND=fiddle`; that line, or
|
|
390
|
+
`ruby -Ilib -rhyperuuid -e 'p HyperUuid::BACKEND'`, is how to tell which backend a run
|
|
391
|
+
exercised. Deleting `lib/hyperuuid/<minor>/` goes back to Fiddle.
|
|
392
|
+
|
|
393
|
+
The task covers Linux and macOS. The Windows extensions need the `gnullvm` targets and linker
|
|
394
|
+
flags in the forge's `build-magnus.sh`, which is also what CI uses on every platform.
|
|
395
|
+
|
|
396
|
+
The musl build has no host to run on outside a container. With the core built for musl at
|
|
397
|
+
`rust/target/musl/linux-musl-x64/libhyperuuid.so`, this runs the Fiddle suite against it on
|
|
398
|
+
Alpine, with the checkout mounted read-only:
|
|
399
|
+
|
|
400
|
+
```sh
|
|
401
|
+
docker run --rm -v "$PWD/..":/src:ro ruby:4.0-alpine sh -euc '
|
|
402
|
+
mkdir /work && cp -r /src/ruby /work/ruby
|
|
403
|
+
mkdir -p /work/ruby/lib/hyperuuid/native/linux-musl-x64
|
|
404
|
+
cp /src/rust/target/musl/linux-musl-x64/libhyperuuid.so /work/ruby/lib/hyperuuid/native/linux-musl-x64/
|
|
405
|
+
cd /work/ruby && rm -f Gemfile.lock
|
|
406
|
+
BUNDLE_WITHOUT=wasm bundle install --quiet --prefer-local
|
|
407
|
+
HYPERUUID_PURE=1 BUNDLE_WITHOUT=wasm bundle exec rspec'
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
The floor's test is the same container on `ruby:3.3-alpine`. Bundler would compile a newer
|
|
411
|
+
Fiddle there (see [Install](#install)), so this one goes around Bundler and runs against the
|
|
412
|
+
Fiddle that Ruby 3.3 ships:
|
|
413
|
+
|
|
414
|
+
```sh
|
|
415
|
+
docker run --rm -v "$PWD/..":/src:ro ruby:3.3-alpine sh -euc '
|
|
416
|
+
mkdir /work && cp -r /src/ruby /work/ruby
|
|
417
|
+
mkdir -p /work/ruby/lib/hyperuuid/native/linux-musl-x64
|
|
418
|
+
cp /src/rust/target/musl/linux-musl-x64/libhyperuuid.so /work/ruby/lib/hyperuuid/native/linux-musl-x64/
|
|
419
|
+
cd /work/ruby && rm -f Gemfile Gemfile.lock
|
|
420
|
+
gem install rspec -v "~> 3.13" --no-document --silent
|
|
421
|
+
HYPERUUID_PURE=1 rspec'
|
|
422
|
+
```
|
|
184
423
|
|
|
185
424
|
See [the repo root README](../README.md) for the full RFC 9562 coverage table and the state of every other language binding.
|
|
425
|
+
|
|
426
|
+
## License
|
|
427
|
+
|
|
428
|
+
[MIT](https://github.com/SkunkWerkx/HyperUuid/blob/master/LICENSE)
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module HyperUuid
|
|
2
|
+
# Raised when the operating system's random source fails while a UUID is being minted —
|
|
3
|
+
# the one way `new_v4`, `new_v6`, `new_v7` and their batch forms can fail once their
|
|
4
|
+
# arguments are valid. Carries the same message on every backend.
|
|
5
|
+
class RandomSourceError < StandardError; end
|
|
6
|
+
|
|
7
|
+
# Raised when a timestamp cannot be embedded in the UUID being minted: a version 7 value
|
|
8
|
+
# past RFC 9562's 48-bit millisecond field, a version 6 value past its 60-bit field, or
|
|
9
|
+
# any negative one (a `Time` before the Unix epoch included). Carries the same message on
|
|
10
|
+
# every backend.
|
|
11
|
+
class TimestampOutOfRangeError < StandardError; end
|
|
12
|
+
end
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -4,18 +4,24 @@ module HyperUuid
|
|
|
4
4
|
module NativePlatform
|
|
5
5
|
class UnsupportedPlatformError < StandardError; end
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
# +platform+ is a parameter only so the specs can walk the whole table from one host;
|
|
8
|
+
# every real caller takes the default.
|
|
9
|
+
def self.rid_and_library_name(platform = RUBY_PLATFORM)
|
|
10
|
+
arch = platform.match?(/arm64|aarch64/) ? "arm64" : "x64"
|
|
9
11
|
|
|
10
|
-
case
|
|
12
|
+
case platform
|
|
11
13
|
when /mingw|mswin|windows/
|
|
12
|
-
|
|
14
|
+
["win-#{arch}", "hyperuuid.dll"]
|
|
13
15
|
when /darwin/
|
|
14
|
-
|
|
16
|
+
["osx-#{arch}", "libhyperuuid.dylib"]
|
|
15
17
|
when /linux/
|
|
16
|
-
|
|
18
|
+
# Two C libraries, two builds: a glibc-linked library cannot be relied on to dlopen
|
|
19
|
+
# into a musl process (Alpine), so musl gets RIDs of its own. Ruby names the libc
|
|
20
|
+
# in its platform string there ("x86_64-linux-musl") and leaves it off on glibc.
|
|
21
|
+
os = platform.include?("musl") ? "linux-musl" : "linux"
|
|
22
|
+
["#{os}-#{arch}", "libhyperuuid.so"]
|
|
17
23
|
else
|
|
18
|
-
raise UnsupportedPlatformError, "hyperuuid: unsupported platform RUBY_PLATFORM=#{
|
|
24
|
+
raise UnsupportedPlatformError, "hyperuuid: unsupported platform RUBY_PLATFORM=#{platform}"
|
|
19
25
|
end
|
|
20
26
|
end
|
|
21
27
|
end
|