diff options
| author | Brian Smith <ohohvi@gmail.com> | 2015-02-07 03:51:13 +0000 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2015-02-07 22:09:03 +0000 |
| commit | 8f02df76f962e5ea2daf27108a0c8efed2c8d905 (patch) | |
| tree | bc2aca35119ca906af950e0dff2048e046b88d1c /src/encoding/xml/xml.go | |
| parent | 3d56fe6d9411ace303b18bf930a0200518886ab6 (diff) | |
| download | go-8f02df76f962e5ea2daf27108a0c8efed2c8d905.tar.xz | |
encoding/xml: avoid an allocation for tags without attributes
Before, an array of size 4 would always be allocated even if a tag
doesn't have any attributes. Now that array is allocated only if
needed.
benchmark old allocs new allocs delta
BenchmarkUnmarshal 191 176 -8.5%
Change-Id: I4d214b228883d0a6e892c0d6eb00dfe2da84c116
Reviewed-on: https://go-review.googlesource.com/4160
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/encoding/xml/xml.go')
| -rw-r--r-- | src/encoding/xml/xml.go | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/src/encoding/xml/xml.go b/src/encoding/xml/xml.go index 5690b20256..e9535d7b55 100644 --- a/src/encoding/xml/xml.go +++ b/src/encoding/xml/xml.go @@ -723,7 +723,7 @@ func (d *Decoder) rawToken() (Token, error) { return nil, d.err } - attr = make([]Attr, 0, 4) + attr = []Attr{} for { d.space() if b, ok = d.mustgetc(); !ok { @@ -747,7 +747,11 @@ func (d *Decoder) rawToken() (Token, error) { n := len(attr) if n >= cap(attr) { - nattr := make([]Attr, n, 2*cap(attr)) + nCap := 2 * cap(attr) + if nCap == 0 { + nCap = 4 + } + nattr := make([]Attr, n, nCap) copy(nattr, attr) attr = nattr } |
