Closed thibaultcha closed 3 years ago
@thibaultCha @Tieske Thanks for this patch. I observed this issue and was looking for a fix. Have you done any performance analysis on this uuid generation? During my testing, it seem to be slower compared to native/c++ version.
This module is not tuned for performance as much as it could be (string concatenation, asserts (with more concats), table creation, bit manipulation...) but at the same time, it is aimed for a pure Lua compatibility.
And yes, I don't think any Lua equivalent will beat C performance, especially considering that C bindings provide more complete uuid generation options (not only the random v4 one). The tradeoff of course is the need to have the dynamic library on your system. Here is my benchmark (LuaJIT is a custom one I wrote with as many optimizations as I could think of):
-- generating 10^6 uuids for each
lua uuid (this module): 1.837744ms
LuaJIT uuid: 0.786335ms
C uuid: 0.238516ms
FFI uuid: 0.109115ms
(Forgot to add those benchmarks are on LuaJIT 2.1). I also just added the FFI binding at https://github.com/bungle/lua-resty-uuid which, as expected, is the fastest.
This would be a great addition!
@rohitjoshi Checkout https://github.com/thibaultCha/lua-resty-jit-uuid if performance is your concern (LuaJIT only) and you don't want to rely on libuuid. I believe it fills a different need and hope it won't be seen as fragmentation :)
@thibaultCha thanks a lot. This is perfect where it meets performance without requiring libuuid. Can it be auto initialized on first use?
Hm yeah maybe! I will give that a try. Let's talk about it there instead to not go off-topic on this PR :)
I tested this with:
And hitting it with:
As expected, the original seeding technique results in conflicts as soon as a second worker is hit, and the conflicts are constant from this point on. With the PID technique, no conflicts occurred.