Optimize checks for day of week and full month

Reduces CPU usage on large benchmarks by ~2%-3% and prepares for future with international month names in future.
This commit is contained in:
Klondike Dragon 2023-12-16 23:40:14 -07:00
parent fbf07cc274
commit a45d593447
2 changed files with 39 additions and 43 deletions

View File

@ -137,6 +137,13 @@ func BenchmarkParseAmbiguous(b *testing.B) {
} }
} }
func BenchmarkParseWeekdayAndFullMonth(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
MustParse("Monday 02 December 2006 03:04:05 PM UTC")
}
}
/* /*
func BenchmarkParseDateString(b *testing.B) { func BenchmarkParseDateString(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()

View File

@ -19,36 +19,36 @@ import (
// gou.SetColorOutput() // gou.SetColorOutput()
// } // }
var days = []string{ var knownDays = map[string]struct{}{
"mon", "mon": {},
"tue", "tue": {},
"wed", "wed": {},
"thu", "thu": {},
"fri", "fri": {},
"sat", "sat": {},
"sun", "sun": {},
"monday", "monday": {},
"tuesday", "tuesday": {},
"wednesday", "wednesday": {},
"thursday", "thursday": {},
"friday", "friday": {},
"saturday", "saturday": {},
"sunday", "sunday": {},
} }
var months = []string{ var knownMonths = map[string]struct{}{
"january", "january": {},
"february", "february": {},
"march", "march": {},
"april", "april": {},
"may", "may": {},
"june", "june": {},
"july", "july": {},
"august", "august": {},
"september", "september": {},
"october", "october": {},
"november", "november": {},
"december", "december": {},
} }
type dateState uint8 type dateState uint8
@ -3080,21 +3080,10 @@ func (p *parser) parse(originalLoc *time.Location, originalOpts ...ParserOption)
} }
} }
func isDay(alpha string) bool { func isDay(alpha string) bool {
for _, day := range days { _, ok := knownDays[alpha]
if alpha == day { return ok
return true
}
}
return false
} }
func isMonthFull(alpha string) bool { func isMonthFull(alpha string) bool {
if len(alpha) > len("september") { _, ok := knownMonths[alpha]
return false return ok
}
for _, month := range months {
if alpha == month {
return true
}
}
return false
} }