Merge pull request #1306 from pei0804/mustcompile

Improvement: regex faster
This commit is contained in:
Steve Kriss
2019-03-21 09:07:20 -06:00
committed by GitHub
2 changed files with 6 additions and 4 deletions
+1
View File
@@ -0,0 +1 @@
compile only once to lower the initialization cost for regexp.MustCompile.
+5 -4
View File
@@ -45,11 +45,12 @@ func printBackupList(list *velerov1api.BackupList, w io.Writer, options printers
return nil
}
// sort by default alphabetically, but if backups stem from a common schedule
// (detected by the presence of a 14-digit timestamp suffix), then within that
// group, sort by newest to oldest (i.e. prefix ASC, suffix DESC)
var timestampSuffix = regexp.MustCompile("-[0-9]{14}$")
func sortBackupsByPrefixAndTimestamp(list *velerov1api.BackupList) {
// sort by default alphabetically, but if backups stem from a common schedule
// (detected by the presence of a 14-digit timestamp suffix), then within that
// group, sort by newest to oldest (i.e. prefix ASC, suffix DESC)
timestampSuffix := regexp.MustCompile("-[0-9]{14}$")
sort.Slice(list.Items, func(i, j int) bool {
iSuffixIndex := timestampSuffix.FindStringIndex(list.Items[i].Name)