zyro / elixir-uuid

UUID generator and utilities for Elixir
Apache License 2.0
361 stars 84 forks source link

Cache MAC address to speed up uuid1/0 #45

Closed whatyouhide closed 4 years ago

whatyouhide commented 4 years ago

Closes #40.

Getting the MAC address (through :inet.getifaddrs/0) is slow, resulting in the uuid1/0 function being around 100x slower than the other UUID functions. With this commit we're starting up a GenServer that caches the MAC address in :persistent_term if available or ETS otherwise. This GenServer refreshes the MAC address every 10 seconds.

Benchmark before:

## UUIDBench
benchmark name     iterations   average time 
binary_to_string!     1000000   1.17 µs/op
uuid3 dns             1000000   1.48 µs/op
uuid5 dns             1000000   1.67 µs/op
uuid4                 1000000   2.10 µs/op
string_to_binary!     1000000   2.98 µs/op
info!                  500000   3.18 µs/op
uuid1                   10000   218.52 µs/op

Benchmark after:

## UUIDBench
benchmark name     iterations   average time 
binary_to_string!     1000000   1.15 µs/op
uuid3 dns             1000000   1.48 µs/op
uuid5 dns             1000000   1.56 µs/op
uuid4                 1000000   2.03 µs/op
uuid1                 1000000   2.21 µs/op
info!                 1000000   2.97 µs/op
string_to_binary!     1000000   3.00 µs/op

cc @zyro, sorry it took a few months to get to this 😄

relistan commented 4 years ago

🥇 This will be great.

whatyouhide commented 4 years ago

Ping @zyro! :bowtie:

whatyouhide commented 4 years ago

Closing this one for inactivity. @zyro if you ever get to it, hit me up and I'll make sure to reopen it :) Thanks!