Merge lp:~chipaca/snappy/sort-perf into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by John Lenton
Status: Merged
Approved by: Michael Vogt
Approved revision: 716
Merged at revision: 716
Proposed branch: lp:~chipaca/snappy/sort-perf
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Diff against target: 50 lines (+11/-6)
1 file modified
snappy/sort.go (+11/-6)
To merge this branch: bzr merge lp:~chipaca/snappy/sort-perf
Reviewer Review Type Date Requested Status
Michael Vogt (community) Approve
Review via email: mp+272372@code.launchpad.net

Commit message

Speed up VersionCompare.

Description of the change

This speeds up VersionCompare 100× when sorting /usr/share/dict/words.

It also serves as reminder to pre-compile your regexps.

To post a comment you must log in.
Revision history for this message
Michael Vogt (mvo) wrote :

Very nice!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'snappy/sort.go'
--- snappy/sort.go 2015-05-19 14:09:19 +0000
+++ snappy/sort.go 2015-09-25 11:33:50 +0000
@@ -35,6 +35,13 @@
35 reHasEpoch = "^[0-9]+:"35 reHasEpoch = "^[0-9]+:"
36)36)
3737
38var (
39 matchDigit = regexp.MustCompile(reDigit).Match
40 matchAlpha = regexp.MustCompile(reAlpha).Match
41 findFrags = regexp.MustCompile(reDigitOrNonDigit).FindAllString
42 matchEpoch = regexp.MustCompile(reHasEpoch).MatchString
43)
44
38// golang: seriously? that's sad!45// golang: seriously? that's sad!
39func max(a, b int) int {46func max(a, b int) int {
40 if a < b {47 if a < b {
@@ -57,12 +64,12 @@
57 if ch == '~' {64 if ch == '~' {
58 return -165 return -1
59 }66 }
60 if matched, _ := regexp.MatchString(reAlpha, string(ch)); matched {67 if matchAlpha([]byte{ch}) {
61 return int(ch)68 return int(ch)
62 }69 }
6370
64 // can only happen if cmpString sets '0' because there is no fragment71 // can only happen if cmpString sets '0' because there is no fragment
65 if matched, _ := regexp.MatchString(reDigit, string(ch)); matched {72 if matchDigit([]byte{ch}) {
66 return 073 return 0
67 }74 }
6875
@@ -101,15 +108,13 @@
101}108}
102109
103func getFragments(a string) []string {110func getFragments(a string) []string {
104 re := regexp.MustCompile(reDigitOrNonDigit)111 return findFrags(a, -1)
105 matches := re.FindAllString(a, -1)
106 return matches
107}112}
108113
109// VersionIsValid returns true if the given string is a valid snap114// VersionIsValid returns true if the given string is a valid snap
110// version number115// version number
111func VersionIsValid(a string) bool {116func VersionIsValid(a string) bool {
112 if matched, _ := regexp.MatchString(reHasEpoch, a); matched {117 if matchEpoch(a) {
113 return false118 return false
114 }119 }
115 if strings.Count(a, "-") > 1 {120 if strings.Count(a, "-") > 1 {

Subscribers

People subscribed via source and target branches