Skip to content

Commit c7bc909

Browse files
author
krthr
committed
init
0 parents  commit c7bc909

File tree

10 files changed

+170
-0
lines changed

10 files changed

+170
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*.cr]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 2
9+
trim_trailing_whitespace = true

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/docs/
2+
/lib/
3+
/bin/
4+
/.shards/
5+
*.dwarf
6+
7+
# Libraries don't need dependency lock
8+
# Dependencies will be locked in applications that use them
9+
/shard.lock

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: crystal
2+
3+
# Uncomment the following if you'd like Travis to run specs and check code formatting
4+
# script:
5+
# - crystal spec
6+
# - crystal tool format --check

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 krthr
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# uuidx
2+
3+
A tiny (<1KB), fast, and cryptographically secure UUID (v4) generator for Crystal.
4+
5+
> This is the Crystal version of [@lukeed/uuid](https://github.com/lukeed/uuid)
6+
7+
## Installation
8+
9+
1. Add the dependency to your `shard.yml`:
10+
11+
```yaml
12+
dependencies:
13+
uuix:
14+
github: krthr/uuix
15+
```
16+
17+
2. Run `shards install`
18+
19+
## Usage
20+
21+
```crystal
22+
require "uuix"
23+
24+
puts UUIX.random # bc524cd7-5ec3-48c4-9bcc-e9013c69dd6e
25+
```
26+
27+
## Benchmarks
28+
29+
```bash
30+
Crystal 0.33.0 [612825a53] (2020-02-14)
31+
32+
LLVM: 8.0.0
33+
Default target: x86_64-unknown-linux-gnu
34+
UUIX: 920.14k ( 1.09µs) (± 9.68%) 880B/op fastest
35+
Crystal UUID: 647.32k ( 1.54µs) (± 7.53%) 0.0B/op 1.42× slower
36+
```
37+
38+
## Performance
39+
40+
> Taken from https://github.com/lukeed/uuid
41+
42+
The reason why this UUID.V4 implementation is so much faster is two-fold:
43+
44+
1. It composes an output with hexadecimal pairs (from a cached dictionary) instead of single characters.
45+
2. It allocates a larger Buffer/ArrayBuffer up front (expensive) and slices off chunks as needed (cheap).
46+
47+
The internal ArrayBuffer is 4096 bytes, which supplies **256** `random` invocations.
48+
49+
A larger buffer would result in higher performance over time, but I found this to be a good balance of performance and memory space.
50+
51+
## Contributing
52+
53+
1. Fork it (<https://github.com/krthr/uuix/fork>)
54+
2. Create your feature branch (`git checkout -b my-new-feature`)
55+
3. Commit your changes (`git commit -am 'Add some feature'`)
56+
4. Push to the branch (`git push origin my-new-feature`)
57+
5. Create a new Pull Request
58+
59+
## Contributors
60+
61+
- [krthr](https://github.com/krthr) - creator and maintainer

shard.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: uuid
2+
version: 0.1.0
3+
4+
authors:
5+
6+
7+
crystal: 0.33.0
8+
9+
license: MIT

spec/spec_helper.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require "spec"
2+
require "../src/uuid"

spec/uuid_spec.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "./spec_helper"
2+
3+
describe Uuid do
4+
# TODO: Write tests
5+
6+
it "works" do
7+
false.should eq(true)
8+
end
9+
end

src/benchmark.cr

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require "benchmark"
2+
require "uuid"
3+
require "./uuix.cr"
4+
5+
puts Crystal::DESCRIPTION
6+
7+
Benchmark.ips do |x|
8+
x.report("UUIX:") do
9+
UUIX.random
10+
end
11+
12+
x.report("Crystal UUID:") do
13+
UUID.random
14+
end
15+
end

src/uuix.cr

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module UUIX
2+
VERSION = "0.1.0"
3+
4+
@@SIZE : Int32 = 4096
5+
@@HEX = [] of String
6+
@@IDX : Int32 = 0
7+
@@BUFFER = [] of UInt8
8+
9+
i = 0
10+
while i < 256
11+
@@HEX << (i + 256).to_s(16)[1..]
12+
i += 1
13+
end
14+
15+
def self.random
16+
if @@BUFFER.empty? || ((@@IDX + 16) > @@SIZE)
17+
@@BUFFER = Random.new.random_bytes(@@SIZE).to_a
18+
@@IDX = 0
19+
end
20+
21+
arr = @@BUFFER[@@IDX..(@@IDX += 16)]
22+
23+
@@HEX[arr[0]] + @@HEX[arr[1]] + @@HEX[arr[2]] + @@HEX[arr[3]] +
24+
'-' + @@HEX[arr[4]] + @@HEX[arr[5]] +
25+
'-' + @@HEX[arr[6] & 15 | 64] + @@HEX[arr[7]] +
26+
'-' + @@HEX[arr[8] & 63 | 128] + @@HEX[arr[9]] +
27+
'-' + @@HEX[arr[10]] + @@HEX[arr[11]] + @@HEX[arr[12]] + @@HEX[arr[13]] + @@HEX[arr[14]] + @@HEX[arr[15]]
28+
end
29+
end

0 commit comments

Comments
 (0)