Introduce build tags to disable unsafe package usage

Package `unsafe` is used in jlexer to prevent copying when casting from []byte to string, speeding up the marshalling process to a large extent. However, `unsafe` is not allowed in environments such as Google Appengine.

Therefore two build tags are introduced, which either of them can disable the use of unsafe package, falling back to conventional copy-based []byte to string conversion. The build tags are `easyjson_nounsafe` and `appengine` (which is set when building for Google Appengine)
This commit is contained in:
Long Hoang
2017-02-26 18:11:07 +08:00
parent e187266de6
commit 8d50f5104d
4 changed files with 34 additions and 14 deletions
+4 -1
View File
@@ -1,4 +1,6 @@
//+build use_codec
//+build !easyjson_nounsafe
//+build !appengine
// ************************************************************
// DO NOT EDIT.
@@ -10,10 +12,11 @@ package benchmark
import (
"errors"
"fmt"
codec1978 "github.com/ugorji/go/codec"
"reflect"
"runtime"
"unsafe"
codec1978 "github.com/ugorji/go/codec"
)
const (
+20
View File
@@ -0,0 +1,20 @@
//+build !easyjson_nounsafe
//+build !appengine
package jlexer
import (
"reflect"
"unsafe"
)
// bytesToStr creates a string pointing at the slice to avoid copying.
//
// Warning: the string returned by the function should be used with care, as the whole input data
// chunk may be either blocked from being freed by GC because of a single string or the buffer.Data
// may be garbage-collected even when the string exists.
func bytesToStr(data []byte) string {
h := (*reflect.SliceHeader)(unsafe.Pointer(&data))
shdr := reflect.StringHeader{h.Data, h.Len}
return *(*string)(unsafe.Pointer(&shdr))
}
+10
View File
@@ -0,0 +1,10 @@
//+build easyjson_nounsafe appengine
package jlexer
// bytesToStr creates a string normally from []byte
//
// Note that this method is roughly 1.5x slower than using the 'unsafe' method.
func bytesToStr(data []byte) string {
return string(data)
}
-13
View File
@@ -9,12 +9,10 @@ import (
"errors"
"fmt"
"io"
"reflect"
"strconv"
"unicode"
"unicode/utf16"
"unicode/utf8"
"unsafe"
)
// tokenKind determines type of a token.
@@ -205,17 +203,6 @@ func (r *Lexer) fetchFalse() {
}
}
// bytesToStr creates a string pointing at the slice to avoid copying.
//
// Warning: the string returned by the function should be used with care, as the whole input data
// chunk may be either blocked from being freed by GC because of a single string or the buffer.Data
// may be garbage-collected even when the string exists.
func bytesToStr(data []byte) string {
h := (*reflect.SliceHeader)(unsafe.Pointer(&data))
shdr := reflect.StringHeader{h.Data, h.Len}
return *(*string)(unsafe.Pointer(&shdr))
}
// fetchNumber scans a number literal token.
func (r *Lexer) fetchNumber() {
hasE := false