engine icon indicating copy to clipboard operation
engine copied to clipboard

Faster GUID generator

Open munrocket opened this issue 4 years ago • 9 comments

Benchmark: https://codepen.io/munsocket/pen/QWyLKMB?editors=1010

I confirm I have read the contributing guidelines and not signed Contributor License Agreement

munrocket avatar May 28 '21 20:05 munrocket

Thanks for this @munsocket! Where is the algorithm from/what is it called for us to check against please?

yaustar avatar May 28 '21 21:05 yaustar

The algorithm is pretty common, we a creating look up table with alphabet and than iterating through it randomly.

It’s based on this stackoverflow https://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid https://jcward.com/UUID.js But with fixed range.

munrocket avatar May 28 '21 22:05 munrocket

For performance, please run tests many times to warm up the V8.

Maksims avatar May 28 '21 22:05 Maksims

Already created variable count, if it will be bigger than some number it will be wamed up. Not sure that this code will be “hot” in PlayCanvas for v8 enough to be optimized, only if somebody creating 3d objects in loop or in big scenes. But in general regex solutions is slower.

munrocket avatar May 28 '21 22:05 munrocket

I just ran the benchmark and I saw this:

image

So there's tjs, new and new2. Which one is this PR implementing? The second has a NaN in it (from my run of the benchmark), despite being the fastest. And new2 doesn't print the GUID.

willeastcott avatar Jun 01 '21 14:06 willeastcott

Sorry for not being clear, I am tested different approaches (crypto.getRandomValues and time based). Here more clear benchmark that show previous and new GUID generators https://codepen.io/munsocket/pen/QWyLKMB

PS: Seems that I found and error, better to stick to Jeff Ward MIT version that is used in Three.js too.

munrocket avatar Jun 01 '21 17:06 munrocket

Cool, this is great @munsocket, thanks! You might have missed this link at the top - would you be able to complete that so I can merge, please? Thanks!

willeastcott avatar Jun 02 '21 16:06 willeastcott

PS: Seems that I found and error, better to stick to Jeff Ward MIT version that is used in Three.js too.

Just to confirm .. has this error been fixed in your following commit? Or is this still TODO.

mvaligursky avatar Jun 22 '21 13:06 mvaligursky

Yes fixed. Your contribution license is really weird because it’s asking my phone number and county. I am trying to keep anonymity and not planning to share this information, so PR can be closed.

munrocket avatar Jun 22 '21 16:06 munrocket

Can't we just close it then?

  1. It adds more code and it looks more complicated
  2. I've never seen this in any profiling session as a bottleneck

kungfooman avatar Feb 23 '23 09:02 kungfooman

Agreed and closing.

mvaligursky avatar Feb 23 '23 09:02 mvaligursky