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) {
b.ReportAllocs()

View File

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