Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UUIDv7 Support #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.'cfg(all())']
rustflags = ["--cfg", "uuid_unstable"]
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fastuuid"
version = "0.9.1"
version = "0.9.2"
authors = ["Omer Katz <omer.drow@gmail.com>"]
license = "BSD3"
description = "Python bindings to Rust's UUID library."
Expand All @@ -14,8 +14,8 @@ version = "0.17.1"
features = ["extension-module"]

[dependencies.uuid]
version = "1"
features = ["v1", "v3", "v4", "v5"]
version = "1.4.1"
features = ["v1", "v3", "v4", "v5", "v7"]

[profile.release]
lto = "fat"
Expand Down
3 changes: 3 additions & 0 deletions fastuuid.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ def uuid4() -> UUID: ...
def uuid4_bulk(n: int) -> List[UUID]: ...
def uuid4_as_strings_bulk(n: int) -> List[str]: ...
def uuid5(namespace: UUID, name: str) -> UUID: ...
def uuid7() -> UUID: ...
def uuid7_bulk(n: int) -> List[UUID]: ...
def uuid7_as_strings_bulk(n: int) -> List[str]: ...
32 changes: 32 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,38 @@ fn fastuuid(_py: Python, m: &PyModule) -> PyResult<()> {
}
}

#[pyfn(m, name = "uuid7_bulk")]
fn uuid7_bulk(py: Python, n: usize) -> Vec<UUID> {
py.allow_threads(|| {
iter::repeat_with(|| UUID {
handle: Uuid::now_v7(),
})
.take(n)
.collect()
})
}

#[pyfn(m, name = "uuid7_as_strings_bulk")]
fn uuid7_as_strings_bulk(py: Python, n: usize) -> Vec<String> {
py.allow_threads(|| {
iter::repeat_with(|| {
(*Uuid::now_v7()
.simple()
.encode_lower(&mut Uuid::encode_buffer()))
.to_string()
})
.take(n)
.collect()
})
}

#[pyfn(m, name = "uuid7")]
fn uuid7() -> UUID {
UUID {
handle: Uuid::now_v7(),
}
}

m.add_class::<UUID>()?;

Ok(())
Expand Down
83 changes: 83 additions & 0 deletions tests/test_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from concurrent.futures import ThreadPoolExecutor
from pickle import dumps, loads
from uuid import UUID, uuid3, uuid4, uuid5
from uuid_extensions import uuid7, uuid7str

import pytest

Expand All @@ -10,6 +11,8 @@
from fastuuid import uuid4 as fastuuid4
from fastuuid import uuid4_as_strings_bulk, uuid4_bulk
from fastuuid import uuid5 as fastuuid5
from fastuuid import uuid7 as fastuuid7
from fastuuid import uuid7_as_strings_bulk, uuid7_bulk


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -102,6 +105,16 @@ def test_fast_uuidv5(benchmark):
benchmark(fastuuid5, fastuuid4(), b"benchmark")


@pytest.mark.benchmark(group="uuidv7")
def test_uuidv7(benchmark):
benchmark(uuid7)


@pytest.mark.benchmark(group="uuidv7")
def test_fast_uuidv7(benchmark):
benchmark(fastuuid7)


@pytest.mark.benchmark(group="uuidv4 8 threads")
def test_uuidv4_threads(benchmark):
@benchmark
Expand Down Expand Up @@ -172,6 +185,76 @@ def b():
pool.shutdown(wait=True)


@pytest.mark.benchmark(group="uuidv7 8 threads")
def test_uuidv7_threads(benchmark):
@benchmark
def b():
pool = ThreadPoolExecutor(max_workers=8)
for _ in range(8):
pool.submit(lambda: [uuid7() for _ in range(1000)])
pool.shutdown(wait=True)


@pytest.mark.benchmark(group="uuidv7 8 threads")
def test_fast_uuidv7_threads(benchmark):
@benchmark
def b():
pool = ThreadPoolExecutor(max_workers=8)
for _ in range(8):
pool.submit(lambda: [fastuuid7() for _ in range(1000)])
pool.shutdown(wait=True)


@pytest.mark.benchmark(group="uuidv7 8 threads")
def test_fast_uuidv7_bulk_threads(benchmark):
@benchmark
def b():
pool = ThreadPoolExecutor(max_workers=8)
for _ in range(8):
pool.submit(uuid7_bulk, 1000)
pool.shutdown(wait=True)


@pytest.mark.benchmark(group="uuidv7 8 threads - strings")
def test_uuidv7_str_threads(benchmark):
@benchmark
def b():
pool = ThreadPoolExecutor(max_workers=8)
for _ in range(8):
pool.submit(lambda: [str(uuid7()) for _ in range(1000)])
pool.shutdown(wait=True)


@pytest.mark.benchmark(group="uuidv7 8 threads - strings")
def test_fast_uuidv7_str_threads(benchmark):
@benchmark
def b():
pool = ThreadPoolExecutor(max_workers=8)
for _ in range(8):
pool.submit(lambda: [str(fastuuid7()) for _ in range(1000)])
pool.shutdown(wait=True)


@pytest.mark.benchmark(group="uuidv7 8 threads - strings")
def test_fast_uuidv7_bulk_convert_to_strings_threads(benchmark):
@benchmark
def b():
pool = ThreadPoolExecutor(max_workers=8)
for _ in range(8):
pool.submit(lambda: map(str, uuid7_bulk(1000)))
pool.shutdown(wait=True)


@pytest.mark.benchmark(group="uuidv7 8 threads - strings")
def test_fast_uuidv7_as_strings_bulk_threads(benchmark):
@benchmark
def b():
pool = ThreadPoolExecutor(max_workers=8)
for _ in range(8):
pool.submit(uuid7_as_strings_bulk, 1000)
pool.shutdown(wait=True)


def _pickle_unpickle(u):
return loads(dumps(u))

Expand Down
10 changes: 9 additions & 1 deletion tests/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from hypothesis import given
from hypothesis.strategies import binary, integers, lists, text, tuples, uuids

from fastuuid import UUID, uuid3, uuid4, uuid5
from fastuuid import UUID, uuid3, uuid4, uuid5, uuid7

UUID_REGEX = re.compile("[0-F]{8}-([0-F]{4}-){3}[0-F]{12}", re.I)

Expand Down Expand Up @@ -320,6 +320,14 @@ def test_uuid5():
assert str(expected) == str(uuid.UUID(str(expected)))


def test_uuid7():
expected = uuid7()
assert expected.version == 7
assert expected.variant == "specified in RFC 4122"
assert UUID_REGEX.match(str(expected))
assert str(expected) == str(uuid.UUID(str(expected)))


@given(uuids())
def test_pickle_unpickle(slow_uuid):
fast_uuid = UUID(int=slow_uuid.int)
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ deps =
hypothesis
pytest>=5.0
pytest-benchmark
uuid7
commands =
pytest -vvvv
black --check tests
Expand Down