https://github.com/jamescook/ulid_fast
Fast ULID generation
https://github.com/jamescook/ulid_fast
c ruby ulid
Last synced: about 1 year ago
JSON representation
Fast ULID generation
- Host: GitHub
- URL: https://github.com/jamescook/ulid_fast
- Owner: jamescook
- Created: 2020-02-11T13:24:21.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-11T13:41:46.000Z (over 6 years ago)
- Last Synced: 2025-03-26T21:22:54.372Z (over 1 year ago)
- Topics: c, ruby, ulid
- Language: C
- Size: 6.84 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ulid_fast - Fast ULID Generator
`ulid_fast` wraps the [ulid-c](https://github.com/skeeto/ulid-c/) library to provide fast ULID generation.
### Example Usage
```ruby
require 'ulid_fast'
ULID::Generator.new.generate
=> "01E0T6ZE28TSXRQTJC9AR9P362"
```
For long-lived Ruby applications (e.g. Rails), I recommend a different pattern to improve performance:
```ruby
require 'ulid_fast'
# Put this in config/initializers/ulid.rb, for example
ULID_GENERATOR = ULID::Generator.new
# Wherever you need a ULID, do this:
ULID_GENERATOR.generate
```
You don't need to create a generator instance for each ULID. When the instance is allocated, we call
`ulid_generator_init` which bootstraps the randomness feature of each generated ULID.
### Performance
Comparing to the pure-Ruby [ULID](https://github.com/rafaelsales/ulid) it's a lot faster provided
you are not re-initializing the generator for every ULID (see usage notes above).
```
Comparison:
ULID::Generator#generate: 2650174.9 i/s
ULID.generate: 41236.1 i/s - 64.27x slower
```
### Todo
- Integrate `Enumerator` so we can generate batches of ULIDs