Giter VIP home page Giter VIP logo

rusty_ulid's People

Contributors

anelson avatar dxist avatar huxi avatar tyhi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rusty_ulid's Issues

Provide true monotonicity guarantee

Thanks for creating such a great utility. I love how well organized and especially well documented it is.

Would there be interest in an API for this which provides a next_monotonic but does so using a thread-safe context and the value is updated atomically? I understand that this might be a major departure from the scope of this crate, which is why I'm asking. I'm happy to make the enhancements, but only if you are interested in such a scope change/increase.

If so, here's my thoughts:

  1. Ulid isn't actually guaranteed to be monotonically increasing: If 2 Id's are generated in the same millisecond, their date bits will be equal, but the random bits could be ordered incorrectly.
    ulid/spec#11
  2. If we want true monotonicity, we need to have a counter whose significant bits are higher than the random bits.
  3. So we steal 12 bits from random to create a counter. (to legacy ULIDs, this is still valid "random" data)
  4. We atomically compare and swap the date of the previously generated Ulid.
  5. To do so, it needs to live in its own 64bit word that is aligned.
  6. Luckily, we don't need the entire previous Ulid, just the timestamp that was used to create it.
  7. I was thinking we could wrap the Timmestamp in a simple struct that is just UlidContext (and it would need to be wrapped in an Arc in order to be passed across threads safely.
  8. The Timestamp would be shifted 12 bits to the left to allow for the counter.
  9. So a thread-friendly next_monotonic function might be:
    fn threadsafe_next_monotonic(ctx: Arc<UlidContext>) -> Option<Ulid> {
        let mut next = unix_epoch_ms() << 12;
        let mut current = ctx.0.load(Ordering::Acquire);
        let mut counter = current >> 48; 
        while next != ctx.0.compare_and_swap(current, next, Ordering::Acquire) {
          current = ctx.0.load(Ordering::Acquire); 
          next = current + 1;
        }
        return ulid_with_context_and_rng(ctx, &mut rand::thread_rng());
    }

This isn't correct code, and I'm sure the algo could be made to be safer and more efficient.
That's the basic idea, though.
It would be a bit slower than the non-threaded version, but, that is usually the case.

Edit : Man I really typo'd the crap out of this post. I need to get more sleep

BIGINT vs UUID vs ULID performance

Thank you for your work on this library.

Would you have any feedback on the performance difference in Postgres DB for primary key on a table with 1 million records for read, write sorting between:

BIGINT
UUID v7
ULID

Particularly on multiple table join queries with either of above as PK and FK

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.