Skip to content

Commit

Permalink
Fixes #31228 - Improve UUID check performance
Browse files Browse the repository at this point in the history
Doing a full match for regexp including saving match data is slow when
all we care about is whether it is a UUID or not. Instead first rule out
quick checks (not a string, wrong length) and only then check with
`.match?` instead of `=~` which just returns a boolean without saving
the match data.
  • Loading branch information
tbrisker authored and ekohl committed Nov 1, 2020
1 parent 58b0b69 commit 4705d72
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/foreman.rb
Expand Up @@ -2,14 +2,14 @@
module Foreman
# generate a UUID
def self.uuid
SecureRandom.uuid.to_s
SecureRandom.uuid
end

UUID_REGEXP = Regexp.new("^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-" +
"([0-9a-f]{2})([0-9a-f]{2})-([0-9a-f]{12})$")
"([0-9a-f]{4})-([0-9a-f]{12})$")
# does this look like a UUID?
def self.is_uuid?(str)
!!(str =~ UUID_REGEXP)
str.is_a?(String) && str.length == 36 && str.match?(UUID_REGEXP)
end

def self.in_rake?(*rake_tasks)
Expand Down

0 comments on commit 4705d72

Please sign in to comment.