Skip to content

Commit

Permalink
chore: bump pyo3 dependency (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlambson authored Aug 11, 2024
1 parent 575f213 commit cfa7c27
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 150 deletions.
152 changes: 14 additions & 138 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fastnanoid"
version = "0.3.0"
version = "0.3.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -9,5 +9,5 @@ name = "fastnanoid"
crate-type = ["cdylib"]

[dependencies]
pyo3 = "0.21.1"
pyo3 = "0.22.2"
rand = "0.8.5"
10 changes: 5 additions & 5 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Results:
```
$ python benchmarks/benchmark.py
Generating 1,000,000 IDs
- nanoid: 2.22s (2.215884291974362e-06 s/id)
- fastnanoid: 0.86s (8.563055000267923e-07 s/id) - 2.59x faster
- uuid (generate + base64encode): 1.28s (1.2837962500052527e-06 s/id) - 1.73x faster
- uuid (generate only): 0.98s (9.750179999973624e-07 s/id) - 2.27x faster
- uuid (base64encode only): 0.26s (2.631903750007041e-07 s/id) - 8.42x faster
- nanoid: 2.19s (2.1881258339853956e-06 s/id)
- fastnanoid: 0.86s (8.555265420000069e-07 s/id) - 2.56x faster
- uuid (generate + base64encode): 1.28s (1.2831176670151764e-06 s/id) - 1.71x faster
- uuid (generate only): 0.97s (9.65138916973956e-07 s/id) - 2.27x faster
- uuid (base64encode only): 0.26s (2.62122415995691e-07 s/id) - 8.35x faster
```
12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use pyo3::{prelude::*, types::PyString};
use pyo3::prelude::*;
use rand::{distributions::Uniform, rngs::StdRng, Rng, SeedableRng};

/// Generates a nanoid string.
/// this is a drop in replacement for py-nanoid's nanoid.generate() function.
#[pyfunction]
fn generate(alphabet: Option<Bound<PyString>>, size: Option<usize>) -> PyResult<String> {
#[pyo3(signature = (alphabet="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-", size=21))]
fn generate(alphabet: Option<&str>, size: Option<usize>) -> PyResult<String> {
let alphabet = match alphabet {
Some(alphabet) => alphabet.to_string(),
None => "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-".to_string(),
};
let size = size.unwrap_or(21);
let mut alphabet_vec = Vec::with_capacity(size); // Not sure what size this should be but this
// should guarantee no reallocs

let mut alphabet_len = 0;
for char in alphabet.chars() {
alphabet_vec.push(char);
Expand Down Expand Up @@ -41,18 +43,18 @@ mod tests {
#[test]
fn test() {
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| {
Python::with_gil(|_| {
assert_eq!(generate(None, None).unwrap().chars().count(), 21);
assert_eq!(generate(None, Some(11)).unwrap().chars().count(), 11);
assert_eq!(
generate(Some(PyString::new_bound(py, "asdf🌍")), None)
generate(Some("asdf🌍".to_string()), None)
.unwrap()
.chars()
.count(),
21
);
assert_eq!(
generate(Some(PyString::new_bound(py, "asdf🌍")), Some(11))
generate(Some("asdf🌍".to_string()), Some(11))
.unwrap()
.chars()
.count(),
Expand Down

0 comments on commit cfa7c27

Please sign in to comment.