Skip to content

Commit

Permalink
build: compile native with new asm2asm (#34)
Browse files Browse the repository at this point in the history
* feat: compile native with new asm2asm

* feat: remove unnecessary json api

* update
  • Loading branch information
AsterDY authored Oct 26, 2023
1 parent d811de5 commit 3612741
Show file tree
Hide file tree
Showing 58 changed files with 57,161 additions and 72,077 deletions.
28 changes: 15 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ SRC_FILE := native/native.c

CPU_avx := amd64
CPU_avx2 := amd64
CPU_sse := amd64
CPU_sse := amd64

TMPL_avx := fastint_amd64_test fastfloat_amd64_test native_amd64_test native_export_amd64
TMPL_avx2 := fastint_amd64_test fastfloat_amd64_test native_amd64_test native_export_amd64
TMPL_sse := fastint_amd64_test fastfloat_amd64_test native_amd64_test native_export_amd64
TMPL_avx := native_amd64_test
TMPL_avx2 := native_amd64_test
TMPL_sse := native_amd64_test

CFLAGS_avx := -msse -mno-sse4 -mavx -mno-avx2 -DUSE_AVX=1 -DUSE_AVX2=0
CFLAGS_avx2 := -msse -mno-sse4 -mavx -mavx2 -DUSE_AVX=0 -DUSE_AVX2=1
CFLAGS_sse := -msse -mno-sse4 -mno-avx -mno-avx2
CFLAGS_avx := -msse -mno-sse4 -mavx -mpclmul -mno-avx2 -mstack-alignment=0 -DUSE_AVX=1 -DUSE_AVX2=0
CFLAGS_avx2 := -msse -mno-sse4 -mavx -mpclmul -mavx2 -mstack-alignment=0 -DUSE_AVX=1 -DUSE_AVX2=1
CFLAGS_sse := -msse -mno-sse4 -mno-avx -mno-avx2 -mpclmul

CC_amd64 := clang
ASM2ASM_amd64 := tools/asm2asm/asm2asm.py
Expand All @@ -43,6 +43,7 @@ CFLAGS += -fno-rtti
CFLAGS += -fno-stack-protector
CFLAGS += -nostdlib
CFLAGS += -O3
CFLAGS += -Wall

NATIVE_SRC := $(wildcard native/*.h)
NATIVE_SRC += $(wildcard native/*.c)
Expand All @@ -65,7 +66,7 @@ define build_arch
$(eval @cpu := $(value CPU_$(1)))
$(eval @deps := $(foreach tmpl,$(value TMPL_$(1)),${OUT_DIR}/$(1)/${tmpl}.go))
$(eval @asmin := ${TMP_DIR}/$(1)/native.s)
$(eval @asmout := ${OUT_DIR}/$(1)/native_${@cpu}.s)
$(eval @asmout := ${OUT_DIR}/$(1)/native_text_${@cpu}.go)
$(eval @stubin := ${OUT_DIR}/native_${@cpu}.tmpl)
$(eval @stubout := ${OUT_DIR}/$(1)/native_${@cpu}.go)

Expand All @@ -74,8 +75,7 @@ $(1): ${@asmout} ${@deps}
${@asmout}: ${@stubout} ${NATIVE_SRC}
mkdir -p ${TMP_DIR}/$(1)
$${CC_${@cpu}} $${CFLAGS} $${CFLAGS_$(1)} -S -o ${TMP_DIR}/$(1)/native.s ${SRC_FILE}
python3 $${ASM2ASM_${@cpu}} ${@asmout} ${TMP_DIR}/$(1)/native.s
asmfmt -w ${@asmout}
python3 $${ASM2ASM_${@cpu}} -r ${@stubout} ${TMP_DIR}/$(1)/native.s

$(eval $(call \
build_tmpl, \
Expand All @@ -99,11 +99,13 @@ endef
all: ${ARCH}

clean:
rm -vfr ${TMP_DIR}/{sse,avx,avx2}
rm -vfr ${OUT_DIR}/{sse,avx,avx2}
for arch in ${ARCH}; do \
rm -vfr ${TMP_DIR}/$${arch}; \
rm -vfr ${OUT_DIR}/$${arch}; \
done

$(foreach \
arch, \
${ARCH}, \
$(eval $(call build_arch,${arch})) \
)
)
2 changes: 1 addition & 1 deletion conv/j2t/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (

"github.com/cloudwego/dynamicgo/conv"
"github.com/cloudwego/dynamicgo/http"
sjson "github.com/cloudwego/dynamicgo/internal/json"
sjson "github.com/bytedance/sonic/ast"
"github.com/cloudwego/dynamicgo/internal/native"
"github.com/cloudwego/dynamicgo/internal/native/types"
"github.com/cloudwego/dynamicgo/internal/rt"
Expand Down
4 changes: 3 additions & 1 deletion conv/t2j/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ import (
"time"
"unsafe"

sjson "github.com/bytedance/sonic/ast"
"github.com/cloudwego/dynamicgo/conv"
"github.com/cloudwego/dynamicgo/http"
sjson "github.com/cloudwego/dynamicgo/internal/json"
"github.com/cloudwego/dynamicgo/internal/util_test"
"github.com/cloudwego/dynamicgo/meta"
kbase "github.com/cloudwego/dynamicgo/testdata/kitex_gen/base"
"github.com/cloudwego/dynamicgo/testdata/kitex_gen/example3"
"github.com/cloudwego/dynamicgo/thrift"
"github.com/cloudwego/dynamicgo/thrift/base"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -642,6 +643,7 @@ func TestOptionalDefaultValue(t *testing.T) {
ctx := context.WithValue(context.Background(), conv.CtxKeyHTTPResponse, resp)
out, err := cv.Do(ctx, desc, in)
require.NoError(t, err)
spew.Dump(string(out))
act := &example3.ExampleOptionalDefaultValue{}
err = json.Unmarshal(out, act)
require.NoError(t, err)
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ go 1.16

require (
github.com/apache/thrift v0.13.0
github.com/bytedance/sonic v1.8.8
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311
github.com/bytedance/sonic v1.10.2
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d
github.com/cloudwego/kitex v0.6.1
github.com/cloudwego/thriftgo v0.2.11
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/fatih/structtag v1.2.0
github.com/iancoleman/strcase v0.2.0
github.com/klauspost/cpuid/v2 v2.2.4
github.com/stretchr/testify v1.8.2

)
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ github.com/bytedance/gopkg v0.0.0-20230531144706-a12972768317 h1:SReMVmTCeJ5Nf0h
github.com/bytedance/gopkg v0.0.0-20230531144706-a12972768317/go.mod h1:FtQG3YbQG9L/91pbKSw787yBQPutC+457AvDW77fgUQ=
github.com/bytedance/mockey v1.2.0/go.mod h1:+Jm/fzWZAuhEDrPXVjDf/jLM2BlLXJkwk94zf2JZ3X4=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.8.8 h1:Kj4AYbZSeENfyXicsYppYKO0K2YWab+i2UTSY7Ukz9Q=
github.com/bytedance/sonic v1.8.8/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE=
github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA=
github.com/chenzhuoyu/iasm v0.0.0-20220818063314-28c361dae733/go.mod h1:wOQ0nsbeOLa2awv8bUYFW/EHXbjQMlZ10fAlXDB2sz8=
github.com/chenzhuoyu/iasm v0.0.0-20230222070914-0b1b64b0e762/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo=
github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
github.com/choleraehyq/pid v0.0.13/go.mod h1:uhzeFgxJZWQsZulelVQZwdASxQ9TIPZYL4TPkQMtL/U=
github.com/choleraehyq/pid v0.0.15/go.mod h1:uhzeFgxJZWQsZulelVQZwdASxQ9TIPZYL4TPkQMtL/U=
github.com/choleraehyq/pid v0.0.16 h1:1/714sMH9IBlE/aK6xM0acTagGKSzpiR0bDt7l0cG7o=
Expand Down
83 changes: 20 additions & 63 deletions internal/json/api_amd64.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,34 @@
//go:build amd64 && go1.16
// +build amd64,go1.16

/**
* Copyright 2023 CloudWeGo Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2023 CloudWeGo Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package json

import (
"runtime"
"unsafe"

"github.com/bytedance/sonic/encoder"
"github.com/chenzhuoyu/base64x"
"github.com/cloudwego/dynamicgo/internal/native"
"github.com/cloudwego/dynamicgo/internal/native/types"
"github.com/cloudwego/dynamicgo/internal/rt"
uq "github.com/cloudwego/dynamicgo/internal/unquote"
)

var typeByte = rt.UnpackEface(byte(0)).Type

func quote(buf *[]byte, val string) {
*buf = append(*buf, '"')
if len(val) == 0 {
*buf = append(*buf, '"')
}
NoQuote(buf, val)

*buf = append(*buf, '"')
}
var typeByte = rt.UnpackEface(byte(0)).Type

//go:nocheckptr
// NoQuote only escape inner '\' and '"' of one string, but it does add quotes besides string.
Expand Down Expand Up @@ -78,44 +65,10 @@ func NoQuote(buf *[]byte, val string) {
runtime.KeepAlive(sp)
}

func unquote(src string) (string, types.ParsingError) {
return uq.String(src)
}

func decodeBase64(src string) ([]byte, error) {
return base64x.StdEncoding.DecodeString(src)
}

func encodeBase64(src []byte) string {
return base64x.StdEncoding.EncodeToString(src)
}

func (self *Parser) decodeValue() (val types.JsonState) {
sv := (*rt.GoString)(unsafe.Pointer(&self.s))
self.p = native.Value(sv.Ptr, sv.Len, self.p, &val, 0)
return
}

func (self *Parser) skip() (int, types.ParsingError) {
fsm := types.NewStateMachine()
start := native.SkipOne(&self.s, &self.p, fsm)
types.FreeStateMachine(fsm)

if start < 0 {
return self.p, types.ParsingError(-start)
}
return start, 0
}

func (self *Node) encodeInterface(buf *[]byte) error {
//WARN: NOT compatible with json.Encoder
return encoder.EncodeInto(buf, self.packAny(), 0)
}

func i64toa(buf *[]byte, val int64) int {
rt.GuardSlice(buf, types.MaxInt64StringLen)
s := len(*buf)
ret := native.I64toa((*byte)(rt.IndexPtr((*rt.GoSlice)(unsafe.Pointer(buf)).Ptr, byteType.Size, s)), val)
ret := native.I64toa((*byte)(rt.IndexPtr((*rt.GoSlice)(unsafe.Pointer(buf)).Ptr, typeByte.Size, s)), val)
if ret < 0 {
*buf = append((*buf)[s:], '0')
return 1
Expand All @@ -127,11 +80,15 @@ func i64toa(buf *[]byte, val int64) int {
func f64toa(buf *[]byte, val float64) int {
rt.GuardSlice(buf, types.MaxFloat64StringLen)
s := len(*buf)
ret := native.F64toa((*byte)(rt.IndexPtr((*rt.GoSlice)(unsafe.Pointer(buf)).Ptr, byteType.Size, s)), val)
ret := native.F64toa((*byte)(rt.IndexPtr((*rt.GoSlice)(unsafe.Pointer(buf)).Ptr, typeByte.Size, s)), val)
if ret < 0 {
*buf = append((*buf)[s:], '0')
return 1
}
*buf = (*buf)[:s+ret]
return ret
}

func encodeBase64(src []byte) string {
return base64x.StdEncoding.EncodeToString(src)
}
71 changes: 0 additions & 71 deletions internal/json/api_amd64_test.go

This file was deleted.

Loading

0 comments on commit 3612741

Please sign in to comment.