Skip to content

Increase speed of UUID generation#11

Merged
nobu merged 1 commit intoruby:masterfrom
robotblake:master
Dec 29, 2023
Merged

Increase speed of UUID generation#11
nobu merged 1 commit intoruby:masterfrom
robotblake:master

Conversation

@robotblake
Copy link
Copy Markdown
Contributor

@robotblake robotblake commented Mar 14, 2022

This change speeds up UUID generation by somewhere between 10-40%. Originally implemented as part of https://github.com/robotblake/hakusho.

Benchmark Methodology

  • First install the benchmark-ips gem and then create a file called benchmark.rb in the root of the repo with the following content.

    require "benchmark/ips"
    
    lib = File.expand_path("../lib", __FILE__)
    $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
    require "securerandom"
    
    Benchmark.ips do |x|
      x.report(`git rev-parse --short HEAD`.strip) do
        SecureRandom.uuid
      end
      x.save! "uuid.out"
      x.compare!
    end
  • Checkout the v0.2.0 tag, and run ruby benchmark.rb.

  • Checkout the b587b8c commit, and run ruby benchmark.rb.

Benchmark Results

~/Projects/securerandom ❯ git checkout v0.2.0
HEAD is now at 62ca282 Bump up v0.2.0

~/Projects/securerandom ❯ ruby benchmark.rb 
Warming up --------------------------------------
             62ca282    93.716k i/100ms
Calculating -------------------------------------
             62ca282    941.790k (± 0.3%) i/s -      4.780M in   5.074975s

~/Projects/securerandom ❯ git checkout b587b8c
Previous HEAD position was 62ca282 Bump up v0.2.0
HEAD is now at b587b8c Increase speed of UUID generation

~/Projects/securerandom ❯ ruby benchmark.rb
Warming up --------------------------------------
             b587b8c   131.060k i/100ms
Calculating -------------------------------------
             b587b8c      1.312M (± 0.4%) i/s -      6.684M in   5.093688s

Comparison:
             b587b8c:  1312244.3 i/s
             62ca282:   941790.2 i/s - 1.39x  (± 0.00) slower

@BenMorganMY
Copy link
Copy Markdown

@robotblake would you be able to explain how this improved the performance? As a generally curious programmer and seeing the dependabot updates on my projects, I'm sure other devs will be coming here to learn about the improvements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants