From 9b75afcf4ae735cb197f0ef835e0052c94cdb666 Mon Sep 17 00:00:00 2001 From: Jack Nagel Date: Fri, 27 Mar 2015 20:07:50 -0400 Subject: [PATCH] Make Version#<=> allocation-free --- Library/Homebrew/version.rb | 49 +++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/Library/Homebrew/version.rb b/Library/Homebrew/version.rb index 5dcfced594..cb15020703 100644 --- a/Library/Homebrew/version.rb +++ b/Library/Homebrew/version.rb @@ -201,8 +201,33 @@ class Version return 1 if head? && !other.head? return -1 if !head? && other.head? - max = [tokens.length, other.tokens.length].max - pad_to(max) <=> other.pad_to(max) + ltokens = tokens + rtokens = other.tokens + max = max(ltokens.length, rtokens.length) + l = r = 0 + + while l < max + a = ltokens[l] || NULL_TOKEN + b = rtokens[r] || NULL_TOKEN + + if a == b + l += 1 + r += 1 + next + elsif a.numeric? && b.numeric? + return a <=> b + elsif a.numeric? + return 1 if a > NULL_TOKEN + l += 1 + elsif b.numeric? + return -1 if b > NULL_TOKEN + r += 1 + else + return a <=> b + end + end + + return 0 end alias_method :eql?, :== @@ -219,24 +244,16 @@ class Version attr_reader :version - def begins_with_numeric? - tokens.first.numeric? - end - - def pad_to(length) - if begins_with_numeric? - nums, rest = tokens.partition(&:numeric?) - nums.fill(NULL_TOKEN, nums.length, length - tokens.length) - nums.concat(rest) - else - tokens.dup.fill(NULL_TOKEN, tokens.length, length - tokens.length) - end - end - def tokens @tokens ||= tokenize end + private + + def max(a, b) + a > b ? a : b + end + def tokenize version.scan(SCAN_PATTERN).map! do |token| case token