-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Closed
Labels
Milestone
Description
As suggested here #54861 by add these two new methods to time package, we achieve this goals:
- Changes don't break any existing code and respect backward compatibility.
- prevent two heap allocation (unnecessary) request, when a Timer can be part of other structs (embed or as a field).
- Due to NewTimer can't inline and return the pointer of a Timer struct,
If devs want to add a Timer to other structs (embed or as a field) to allocate both structs in one request,
existing NewTimer() don't let us do this. - Init method can inline now and the Start() method sets the when field. In this way, functions respect the single responsibility principle.
/time> go build -gcflags="-m"
./sleep.go:64:6: can inline (*Timer).Init
./sleep.go:50:8: inlining call to (*Timer).Init
- Improve code readability of NewTimer() function.