github.com/segmentio/encoding@v0.4.0/json/reflect_optimize.go (about)

     1  //go:build !go1.20
     2  // +build !go1.20
     3  
     4  package json
     5  
     6  import (
     7  	"reflect"
     8  	"unsafe"
     9  )
    10  
    11  //go:linkname unsafe_NewArray reflect.unsafe_NewArray
    12  func unsafe_NewArray(rtype unsafe.Pointer, length int) unsafe.Pointer
    13  
    14  //go:linkname typedslicecopy reflect.typedslicecopy
    15  //go:noescape
    16  func typedslicecopy(elemType unsafe.Pointer, dst, src slice) int
    17  
    18  func extendSlice(t reflect.Type, s *slice, n int) slice {
    19  	elemTypeRef := t.Elem()
    20  	elemTypePtr := ((*iface)(unsafe.Pointer(&elemTypeRef))).ptr
    21  
    22  	d := slice{
    23  		data: unsafe_NewArray(elemTypePtr, n),
    24  		len:  s.len,
    25  		cap:  n,
    26  	}
    27  
    28  	typedslicecopy(elemTypePtr, d, *s)
    29  	return d
    30  }