How to generate GUID

I need to generate a guid to use in an api that my service integrates with. It won’t be catastrophic if my guid isn’t unique. I only need to generate a couple dozen at most and I can use a hash to make sure they are unique. I tried using SecureRandom, but on my machine it freezes SketchUp for 24 seconds on the first use, after that it is fast. Does anyone know of another solution?

require 'securerandom'
t = Time.now
uuid = SecureRandom.uuid
"#{Time.now - t} seconds to generate guid."

image

This is a known issue in older version of the OpenSSL library (which SecureRandom uses.)
It is (or was discussed) I think in the GitHub issue tracker.


I did post a “trick” using a temporary component definition in this post …
ComponentDefinition#guid at the model level? - #4 by DanRathbun
This thread was entirely devoted to the generation of GUID or UUID for extension usage.

1 Like

By the way there is a UUID gem:

https://rubygems.org/gems/uuid/versions/2.3.9

It has a MacAddr gem as a depenency …

https://rubygems.org/gems/macaddr

Source …

Why not generate several uuids and then store them in your RB file’s code - as strings in an array of say 50 uuid elements.
You say it’s not that important they’re unique anyway…
So I assume reusing one sometimes is acceptable ?
Later on you can access them ‘at random’ using rnd() to pick one from your array…
You could even randomize the uuid further by using ‘next’ on it a random number of iterations, that way you’ll very rarely repeat…

It so happens that I need a guid for each component which I already have by doing instance.guid. Not sure why I didn’t think of that. :bulb:

What version of SketchUp are you seeing this?

The source of this is how the underlying OpenSSL is seeding it’s random bytes. On Windows it’s been doing so using deprecated debug API from the Win32 API that’s only gotten slower over the years. There’s been work towards fixing that and I thought it was picked up by Ruby and SketchUp by now. I would have to dig into my old notes to find what versions were affected.)

We even added a performance warning about this into rubycop-sketchup: Performance Cops - RuboCop SketchUp: SketchUp Extension Best Practices (rubocop-sketchup.readthedocs.io)

1 Like

I was using 2018. 2020, and 2021 are very fast. I don’t have 2019 installed, so I’m not sure on that.

Release notes indicate that the update of Ruby for 2019 also updated the OpenSSL library.
It was again updated for the 2021 release.

1 Like

Thanks. I logged an issue to improve the cop description and logic: Update info for SketchupPerformance/OpenSSL · Issue #149 · SketchUp/rubocop-sketchup (github.com)

1 Like

Just an FYI, … I think this (old closed issue) was some of the discussion on previous OpenSSL issues:

cacert.pem related to another issue - that the list of trusted root certificate issuers was our of date so one couldn’t connect to HTTPS sites.