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
1=== modified file 'snappy/sort.go'
2--- snappy/sort.go 2015-05-19 14:09:19 +0000
3+++ snappy/sort.go 2015-09-25 11:33:50 +0000
4@@ -35,6 +35,13 @@
5 reHasEpoch = "^[0-9]+:"
6 )
7
8+var (
9+ matchDigit = regexp.MustCompile(reDigit).Match
10+ matchAlpha = regexp.MustCompile(reAlpha).Match
11+ findFrags = regexp.MustCompile(reDigitOrNonDigit).FindAllString
12+ matchEpoch = regexp.MustCompile(reHasEpoch).MatchString
13+)
14+
15 // golang: seriously? that's sad!
16 func max(a, b int) int {
17 if a < b {
18@@ -57,12 +64,12 @@
19 if ch == '~' {
20 return -1
21 }
22- if matched, _ := regexp.MatchString(reAlpha, string(ch)); matched {
23+ if matchAlpha([]byte{ch}) {
24 return int(ch)
25 }
26
27 // can only happen if cmpString sets '0' because there is no fragment
28- if matched, _ := regexp.MatchString(reDigit, string(ch)); matched {
29+ if matchDigit([]byte{ch}) {
30 return 0
31 }
32
33@@ -101,15 +108,13 @@
34 }
35
36 func getFragments(a string) []string {
37- re := regexp.MustCompile(reDigitOrNonDigit)
38- matches := re.FindAllString(a, -1)
39- return matches
40+ return findFrags(a, -1)
41 }
42
43 // VersionIsValid returns true if the given string is a valid snap
44 // version number
45 func VersionIsValid(a string) bool {
46- if matched, _ := regexp.MatchString(reHasEpoch, a); matched {
47+ if matchEpoch(a) {
48 return false
49 }
50 if strings.Count(a, "-") > 1 {

Subscribers

People subscribed via source and target branches