Use pre allocated pointer if exists it

This commit is contained in:
Masaaki Goshima 2021-02-17 00:03:07 +09:00
parent 72cdc2a1d5
commit e09027425c
2 changed files with 15 additions and 10 deletions

View File

@ -44,8 +44,13 @@ func (d *ptrDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
*(*unsafe.Pointer)(p) = nil *(*unsafe.Pointer)(p) = nil
return nil return nil
} }
newptr := unsafe_New(d.typ) var newptr unsafe.Pointer
*(*unsafe.Pointer)(p) = newptr if *(*unsafe.Pointer)(p) == nil {
newptr = unsafe_New(d.typ)
*(*unsafe.Pointer)(p) = newptr
} else {
newptr = *(*unsafe.Pointer)(p)
}
if err := d.dec.decodeStream(s, newptr); err != nil { if err := d.dec.decodeStream(s, newptr); err != nil {
return err return err
} }
@ -74,8 +79,13 @@ func (d *ptrDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64,
cursor += 4 cursor += 4
return cursor, nil return cursor, nil
} }
newptr := unsafe_New(d.typ) var newptr unsafe.Pointer
*(*unsafe.Pointer)(p) = newptr if *(*unsafe.Pointer)(p) == nil {
newptr = unsafe_New(d.typ)
*(*unsafe.Pointer)(p) = newptr
} else {
newptr = *(*unsafe.Pointer)(p)
}
c, err := d.dec.decode(buf, cursor, newptr) c, err := d.dec.decode(buf, cursor, newptr)
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -1864,7 +1864,6 @@ func TestUnmarshal(t *testing.T) {
} }
} }
/*
func TestUnmarshalMarshal(t *testing.T) { func TestUnmarshalMarshal(t *testing.T) {
initBig() initBig()
var v interface{} var v interface{}
@ -1881,7 +1880,6 @@ func TestUnmarshalMarshal(t *testing.T) {
return return
} }
} }
*/
var numberTests = []struct { var numberTests = []struct {
in string in string
@ -1938,7 +1936,6 @@ type Xint struct {
X int X int
} }
/*
func TestUnmarshalInterface(t *testing.T) { func TestUnmarshalInterface(t *testing.T) {
var xint Xint var xint Xint
var i interface{} = &xint var i interface{} = &xint
@ -1949,8 +1946,7 @@ func TestUnmarshalInterface(t *testing.T) {
t.Fatalf("Did not write to xint") t.Fatalf("Did not write to xint")
} }
} }
*/
/*
func TestUnmarshalPtrPtr(t *testing.T) { func TestUnmarshalPtrPtr(t *testing.T) {
var xint Xint var xint Xint
pxint := &xint pxint := &xint
@ -1961,7 +1957,6 @@ func TestUnmarshalPtrPtr(t *testing.T) {
t.Fatalf("Did not write to xint") t.Fatalf("Did not write to xint")
} }
} }
*/
func TestEscape(t *testing.T) { func TestEscape(t *testing.T) {
const input = `"foobar"<html>` + " [\u2028 \u2029]" const input = `"foobar"<html>` + " [\u2028 \u2029]"