bk / Data-ULID

Perl implementation of ULID (Universally Unique Lexicographically Sortable Identifier)
7 stars 6 forks source link

Refactor _zero_pad and b32 functions - performance improvements #7

Closed bbrtj closed 2 years ago

bbrtj commented 2 years ago

Used better algorithm for _zero_pad and some minor refactors in encoder and decoder.

This set of changes improves ulid generation performance by 15 - 20%:

               Rate old (text) new (text)
old (text) 113652/s         --       -17%
new (text) 136741/s        20%         --
                 Rate old (binary) new (binary)
old (binary) 333076/s           --         -16%
new (binary) 397348/s          19%           --
bbrtj commented 2 years ago

My last commit decreases readability of _zero_pad a bit, but I now pretty consistently get over 20% speedup in text ulid and close to 30% in binary ulid, for example:

               Rate old (text) new (text)
old (text) 103910/s         --       -18%
new (text) 126929/s        22%         --
                 Rate old (binary) new (binary)
old (binary) 304464/s           --         -22%
new (binary) 390046/s          28%           --

(not creating lexical variable $value would make for an even bigger gain, but it would be hard to read. I think current code is a nice middle ground)