From 7af9f8d74e762e2aad37e571a00c7a1827ad3bd7 Mon Sep 17 00:00:00 2001 From: pei0804 Date: Thu, 21 Mar 2019 21:51:48 +0900 Subject: [PATCH] compile only once for regexp.MustCompile Signed-off-by: pei0804 --- changelogs/unreleased/1306-pei0804 | 1 + pkg/cmd/util/output/backup_printer.go | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 changelogs/unreleased/1306-pei0804 diff --git a/changelogs/unreleased/1306-pei0804 b/changelogs/unreleased/1306-pei0804 new file mode 100644 index 000000000..1ac19e64f --- /dev/null +++ b/changelogs/unreleased/1306-pei0804 @@ -0,0 +1 @@ +compile only once to lower the initialization cost for regexp.MustCompile. diff --git a/pkg/cmd/util/output/backup_printer.go b/pkg/cmd/util/output/backup_printer.go index 2a88bc71f..6e7111b23 100644 --- a/pkg/cmd/util/output/backup_printer.go +++ b/pkg/cmd/util/output/backup_printer.go @@ -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)