Comments (4)
Hi @fef1312 👋
Hmm, the result of u16::MAX >> 2
should be 0x3fff
rather than 0x1fff
right? 🤔 Would a method using a bitwise AND actually have lower entropy than the current method that wraps? Or would they be the same? In the end we're mapping 16 bits down to 14, so if they're the same then an AND definitely sounds preferable to a division.
from uuid.
Oops, you're right about the 0x3fff
. My bad!
What I mean by "lower entropy" is that anything modulo N is always less than N, so the largest value this method will ever return in its current implementation is actually 0x3ffe
. In other words, the counter has "only" 16383 rather than 16384 possible states. To wrap at exactly 14 bits, you have two options:
self.count.fetch_add(1, Ordering::AcqRel) % (1 << 14)
self.count.fetch_add(1, Ordering::AcqRel) & (u16::MAX >> 2)
These are mathematically equivalent because it's a power of two, so rustc will pick the AND version either way. But again, this is merely a technicality and a single division here won't make an observable difference in the grand scheme of things. The main reason I brought it up was because it's technically not compliant with the RFC.
from uuid.
Aha, thanks for the explanation. It was simply my ignorance that lead to the original implementation and I think your suggested alternative:
self.count.fetch_add(1, Ordering::AcqRel) & (u16::MAX >> 2)
would be a great fix.
Would you like to submit a PR for it?
from uuid.
Alright, sure!
from uuid.
Related Issues (20)
- 1.3.4 and 1.3.3 failing in Cloudflare Worker env for different reasons
- Unable to activate feature "v7" HOT 6
- Request: add configuration to enable/disable deserializing different formats HOT 2
- Export `error::InvalidUuid` HOT 2
- Make &'a Uuid serializable HOT 3
- UUID no longer supports serde derive attributes HOT 2
- Add bytemuck support HOT 1
- Add `prost::Message` implementation for `Uuid` HOT 3
- v7 should have a counter option? HOT 31
- Macros from 1.5.0 to 1.6.0 break. HOT 4
- Investigate crate size HOT 2
- Idiotmatic naming: drop/replace `get_` prefixes HOT 4
- 1.7.0 breaking change HOT 2
- Initialization of CONTEXT in timestamp::context::shared_context() not thread-safe HOT 1
- the trait Deserialize is not implemented for `Uuid` HOT 2
- TryFrom<Vec<u8>> makes an unnecessary copy HOT 1
- `uuid!` `index is out of bounds` in clippy on rust 1.77 (fixed in uuid 1.6.1)
- Why does Uuid implement Ord, PartialOrd? HOT 1
- uuid!() macro can't handle a constant &str passed in HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from uuid.