Skip to content

Commit

Permalink
Avoid creating new time on invalid time token
Browse files Browse the repository at this point in the history
The parser attempts to parse each string token as time. In case of a non-time token, a time.Now() instance is created, which is immediately discarded. In a tight loop this unnecessary allocation can drastically slow down template parsing.

This PR simply returns time's empty value, which does not cause an allocation.
  • Loading branch information
Richtermeister authored Jun 21, 2023
1 parent 9aa4983 commit a2bbf5c
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ func tryParseTime(candidate string) (time.Time, bool) {
}
}

return time.Now(), false
return ret, false
}

func tryParseExactTime(candidate string, format string) (time.Time, bool) {
Expand All @@ -510,7 +510,7 @@ func tryParseExactTime(candidate string, format string) (time.Time, bool) {

ret, err = time.ParseInLocation(format, candidate, time.Local)
if err != nil {
return time.Now(), false
return ret, false
}

return ret, true
Expand Down

0 comments on commit a2bbf5c

Please sign in to comment.