compile only once for regexp.MustCompile

Signed-off-by: pei0804 <peeeei0804@gmail.com>
This commit is contained in:
pei0804
2019-03-21 21:51:48 +09:00
parent bd662ab613
commit 7af9f8d74e
2 changed files with 6 additions and 4 deletions

View File

@@ -0,0 +1 @@
compile only once to lower the initialization cost for regexp.MustCompile.

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)