]> Cypherpunks repositories - gostls13.git/commitdiff
net/mail: lazily initialize dateLayouts
authorCholerae Hu <choleraehyq@gmail.com>
Fri, 3 Aug 2018 06:49:47 +0000 (14:49 +0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 22 Aug 2018 16:08:20 +0000 (16:08 +0000)
Saves 6KB of memory in stdlib packages.

Updates #26775

Change-Id: I1a6184cefa78e9a3c034fa84506fdfe0fec27add
Reviewed-on: https://go-review.googlesource.com/127736
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/mail/message.go

index 5912b9033477e1f495bbfbb69148f41a1741dce6..554377aa1da989a06eaee5070db4a79bfaf50971 100644 (file)
@@ -26,6 +26,7 @@ import (
        "mime"
        "net/textproto"
        "strings"
+       "sync"
        "time"
        "unicode/utf8"
 )
@@ -65,9 +66,12 @@ func ReadMessage(r io.Reader) (msg *Message, err error) {
 
 // Layouts suitable for passing to time.Parse.
 // These are tried in order.
-var dateLayouts []string
+var (
+       dateLayoutsBuildOnce sync.Once
+       dateLayouts          []string
+)
 
-func init() {
+func buildDateLayouts() {
        // Generate layouts based on RFC 5322, section 3.3.
 
        dows := [...]string{"", "Mon, "}   // day-of-week
@@ -93,6 +97,7 @@ func init() {
 
 // ParseDate parses an RFC 5322 date string.
 func ParseDate(date string) (time.Time, error) {
+       dateLayoutsBuildOnce.Do(buildDateLayouts)
        for _, layout := range dateLayouts {
                t, err := time.Parse(layout, date)
                if err == nil {