Merge pull request #137 from goccy/feature/fix-predef-ptr

Use pre allocated pointer if exists it
This commit is contained in:
Masaaki Goshima 2021-02-17 00:18:21 +09:00 committed by GitHub
commit d8c3c8d209
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
return nil
}
newptr := unsafe_New(d.typ)
var newptr unsafe.Pointer
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 {
return err
}
@ -74,8 +79,13 @@ func (d *ptrDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64,
cursor += 4
return cursor, nil
}
newptr := unsafe_New(d.typ)
var newptr unsafe.Pointer
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)
if err != nil {
return 0, err

View File

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