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

History id start #391

Merged
merged 2 commits into from
Oct 14, 2023
Merged
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
4 changes: 4 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/history/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ thiserror = { version = "1.0", optional = true }
version = "0.3"
features = ["History", "Window", "Location", "Url"]

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.10", features = ["js"] }

[dev-dependencies]
wasm-bindgen-test = "0.3"
gloo-timers = { version = "0.3.0", features = ["futures"], path = "../timers" }
Expand Down
18 changes: 18 additions & 0 deletions crates/history/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,30 @@ use std::sync::atomic::{AtomicU32, Ordering};

use wasm_bindgen::throw_str;

#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn get_id() -> u32 {
static ID_CTR: AtomicU32 = AtomicU32::new(0);

ID_CTR.fetch_add(1, Ordering::SeqCst)
}

#[cfg(target_arch = "wasm32")]
pub(crate) fn get_id() -> u32 {
static ID_CTR: AtomicU32 = AtomicU32::new(0);
static INIT: std::sync::Once = std::sync::Once::new();

INIT.call_once(|| {
let mut start: [u8; 4] = [0; 4];
// If it fails then the start is not or only partly filled.
// But since this method should not fail, we take what we get.
let _ = getrandom::getrandom(&mut start);
// Using a high initial value is not an issue as `fetch_add` does wrap around.
ID_CTR.store(u32::from_ne_bytes(start), Ordering::SeqCst);
});

ID_CTR.fetch_add(1, Ordering::SeqCst)
Comment on lines +16 to +28
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static ID_CTR: AtomicU32 = AtomicU32::new(0);
static INIT: std::sync::Once = std::sync::Once::new();
INIT.call_once(|| {
let mut start: [u8; 4] = [0; 4];
// If it fails then the start is not or only partly filled.
// But since this method should not fail, we take what we get.
let _ = getrandom::getrandom(&mut start);
// Using a high initial value is not an issue as `fetch_add` does wrap around.
ID_CTR.store(u32::from_ne_bytes(start), Ordering::SeqCst);
});
ID_CTR.fetch_add(1, Ordering::SeqCst)
let mut start: [u8; 4] = [0; 4];
let _ = getrandom::getrandom(&mut start);
u32::from_ne_bytes(start)

I think we can omit the initialisation logic and get a random value as id every time.
This might help us to save bundle size and I feel this might be safer.

However, the 2 methods should have the same probability for the scenarios I can come up with.
So I am fine with the current method as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a good idea to use a random value each time because then there could be a collision, which is currently impossible (unless 2^32 id's are generated).

So I'll leave it at the current method.

Copy link
Collaborator

@futursolo futursolo Oct 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a good idea to use a random value each time because then there could be a collision, which is currently impossible (unless 2^32 id's are generated).

2^32 is only the best case scenario, where it only happens if the start id is assigned the last id + 1.

Suppose 64 is selected for a start id with 10 navigation, then ids 64~73 are given out.

For another page to collide with currently assigned id within 10 navigations, the start id needs to be within 55~73.

Suppose the id is u8, which will make the calculation easier.
Assume the start id is selected based on 1/256 for every possibility, then the probability is (73 - 55 + 1 (both ends are inclusive) + 1 (assigning 11th id for the second navigation)) / 256, which is 20 / 256.

If every id is random, then for each unique id given out, the likelihood of collision increases by 1/256. After 20 ids are assigned, the 21st id is 20/256.

So the probability is the same.

}

pub(crate) fn assert_absolute_path(path: &str) {
if !path.starts_with('/') {
throw_str("You cannot use relative path with this history type.");
Expand Down