Commit 437ec6b3 authored by Dmitry Vyukov's avatar Dmitry Vyukov

encoding/xml: remove unnecessary memory allocation in Unmarshal

benchmark              old ns/op     new ns/op     delta
BenchmarkUnmarshal     75256         72626         -3.49%

benchmark              old allocs     new allocs     delta
BenchmarkUnmarshal     259            219            -15.44%

Change-Id: I7fd30739b045e35b95e6ef6a8ef2f15b0dd6839c
Reviewed-on: https://go-review.googlesource.com/2758Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent a25af2e9
...@@ -1148,12 +1148,14 @@ func TestMarshalFlush(t *testing.T) { ...@@ -1148,12 +1148,14 @@ func TestMarshalFlush(t *testing.T) {
} }
func BenchmarkMarshal(b *testing.B) { func BenchmarkMarshal(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Marshal(atomValue) Marshal(atomValue)
} }
} }
func BenchmarkUnmarshal(b *testing.B) { func BenchmarkUnmarshal(b *testing.B) {
b.ReportAllocs()
xml := []byte(atomXml) xml := []byte(atomXml)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Unmarshal(xml, &Feed{}) Unmarshal(xml, &Feed{})
......
...@@ -1119,12 +1119,12 @@ func (d *Decoder) name() (s string, ok bool) { ...@@ -1119,12 +1119,12 @@ func (d *Decoder) name() (s string, ok bool) {
} }
// Now we check the characters. // Now we check the characters.
s = d.buf.String() b := d.buf.Bytes()
if !isName([]byte(s)) { if !isName(b) {
d.err = d.syntaxError("invalid XML name: " + s) d.err = d.syntaxError("invalid XML name: " + string(b))
return "", false return "", false
} }
return s, true return string(b), true
} }
// Read a name and append its bytes to d.buf. // Read a name and append its bytes to d.buf.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment