Skip to content

Commit

Permalink
opt: encoder VM
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed May 31, 2024
1 parent 381aa24 commit dfcddaa
Show file tree
Hide file tree
Showing 93 changed files with 5,399 additions and 8,411 deletions.
27 changes: 0 additions & 27 deletions .github/workflows/benchmark-linux-arm64.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ on: pull_request

jobs:
build:
runs-on: [self-hosted, X64]
strategy:
matrix:
os: [X64, arm]
runs-on: ${{ matrix.os }}
steps:
- name: Clear repository
run: sudo rm -fr $GITHUB_WORKSPACE && mkdir $GITHUB_WORKSPACE
Expand Down
12 changes: 9 additions & 3 deletions .github/workflows/compatibility_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
build:
strategy:
matrix:
go-version: [1.15.x, 1.16.x, 1.17.x, 1.18.x, 1.19.x, 1.20.x, 1.21.x, 1.22.x]
go-version: [1.16.x, 1.17.x, 1.18.x, 1.19.x, 1.20.x, 1.21.x, 1.22.x]
os: [arm, X64]
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -28,10 +28,16 @@ jobs:
${{ runner.os }}-go-
- name: main
run: GOMAXPROCS=4 go test -v -gcflags="all=-l" -race github.com/bytedance/sonic
run: go test -race -v -gcflags="all=-l" github.com/bytedance/sonic

- name: decoder
run: go test -race -v -gcflags="all=-l" github.com/bytedance/sonic/decoder

- name: encoder
run: go test -race -v -gcflags="all=-l" github.com/bytedance/sonic/encoder

- name: ast
run: GOMAXPROCS=4 go test -v -gcflags="all=-l" -race github.com/bytedance/sonic/ast
run: go test -race -v -gcflags="all=-l" github.com/bytedance/sonic/ast

- name: qemu
run: sh scripts/qemu.sh
7 changes: 5 additions & 2 deletions .github/workflows/fuzzing-linux-x64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ on: pull_request

jobs:
build:
runs-on: [self-hosted, X64]
strategy:
matrix:
os: [arm, X64]
runs-on: ${{ matrix.os }}
steps:
- name: Clear repository
run: sudo rm -fr $GITHUB_WORKSPACE && mkdir $GITHUB_WORKSPACE
Expand All @@ -14,7 +17,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
go-version: 1.20.x

- uses: actions/cache@v2
with:
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/unit_test-linux-x64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
build:
strategy:
matrix:
go-version: [1.16.x, 1.17.x, 1.18.x, 1.19.x, 1.20.x, 1.21.x, 1.22.x]
go-version: [1.17.x, 1.18.x, 1.19.x, 1.20.x, 1.21.x, 1.22.x]
runs-on: [self-hosted, X64]
steps:
- name: Clear repository
Expand All @@ -30,17 +30,17 @@ jobs:
- name: Unit Test
run: |
go test -race -covermode=atomic -coverprofile=coverage.txt ./...
GOMAXPROCS=4 go test -race -covermode=atomic -coverprofile=coverage.txt ./...
- name: external
run: |
cd ./external_jsonlib_test
GOMAXPROCS=4 go test -v -race ./...
go test -v -race ./...
- name: external
run: |
cd ./loader
GOMAXPROCS=4 go test -v -race ./...
go test -v -race ./...
- name: Codecov
run: bash <(curl -s https://codecov.io/bash)
3 changes: 1 addition & 2 deletions ast/api.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build (amd64 && go1.16 && !go1.23) || (arm64 && go1.20 && !go1.23)
// +build amd64,go1.16,!go1.23 arm64,go1.20,!go1.23
// +build amd64,go1.17,!go1.23 arm64,go1.20,!go1.23

/*
* Copyright 2022 ByteDance Inc.
Expand Down
2 changes: 1 addition & 1 deletion ast/api_compat.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !amd64,!arm64 go1.23 !go1.16 arm64,!go1.20
// +build !amd64,!arm64 go1.23 !go1.17 arm64,!go1.20

/*
* Copyright 2022 ByteDance Inc.
Expand Down
3 changes: 1 addition & 2 deletions ast/api_native_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build (amd64 && go1.16 && !go1.23) || (arm64 && go1.20 && !go1.23)
// +build amd64,go1.16,!go1.23 arm64,go1.20,!go1.23
// +build amd64,go1.17,!go1.23 arm64,go1.20,!go1.23

/*
* Copyright 2022 ByteDance Inc.
Expand Down
2 changes: 1 addition & 1 deletion compat.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !amd64 !go1.16 go1.23
// +build !amd64 go1.23 !go1.17

/*
* Copyright 2021 ByteDance Inc.
Expand Down
2 changes: 1 addition & 1 deletion decode_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build amd64,go1.16,!go1.23
// +build amd64,go1.17,!go1.23

/*
* Copyright 2021 ByteDance Inc.
Expand Down
2 changes: 1 addition & 1 deletion decoder/decoder_amd64.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build amd64,go1.16,!go1.23
// +build amd64,go1.17,!go1.23

/*
* Copyright 2023 ByteDance Inc.
Expand Down
2 changes: 1 addition & 1 deletion decoder/decoder_amd64_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build amd64,go1.16,!go1.23
// +build amd64,go1.17,!go1.23

/*
* Copyright 2021 ByteDance Inc.
Expand Down
4 changes: 2 additions & 2 deletions decoder/decoder_compat.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !amd64 !go1.16 go1.23
// +build !amd64 !go1.17 go1.23

/*
* Copyright 2023 ByteDance Inc.
Expand Down Expand Up @@ -30,7 +30,7 @@ import (
)

func init() {
println("WARNING: sonic only supports Go1.16~1.22 && CPU amd64, but your environment is not suitable")
println("WARNING: sonic only supports Go1.17~1.22 && CPU amd64, but your environment is not suitable")
}

const (
Expand Down
2 changes: 1 addition & 1 deletion encode_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build amd64,go1.16,!go1.23
// +build amd64,go1.17,!go1.23

/*
* Copyright 2021 ByteDance Inc.
Expand Down
4 changes: 2 additions & 2 deletions encoder/encoder_compat.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !amd64 !go1.16 go1.23
// +build !amd64,!arm64 go1.23 !go1.17 arm64,!go1.20

/*
* Copyright 2023 ByteDance Inc.
Expand Down Expand Up @@ -28,7 +28,7 @@ import (
)

func init() {
println("WARNING:(encoder) sonic only supports Go1.16~1.22 && CPU amd64, but your environment is not suitable")
println("WARNING:(encoder) sonic only supports Go1.17~1.22 && CPU amd64, but your environment is not suitable")
}

// EnableFallback indicates if encoder use fallback
Expand Down
2 changes: 1 addition & 1 deletion encoder/encoder_amd64.go → encoder/encoder_native.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build amd64,go1.16,!go1.23
// +build amd64,go1.17,!go1.23 arm64,go1.20,!go1.23

/*
* Copyright 2023 ByteDance Inc.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build amd64,go1.16,!go1.23
// +build amd64,go1.17,!go1.23 arm64,go1.20,!go1.23

/*
* Copyright 2021 ByteDance Inc.
Expand Down
2 changes: 1 addition & 1 deletion encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func TestEncoder_Marshal_EscapeHTML_LargeJson(t *testing.T) {
require.NoError(t, err1)
buf2, err2 :=json.Marshal(&_BindingValue)
require.NoError(t, err2)
require.Equal(t, buf1, buf2)
require.Equal(t, string(buf1), string(buf2))
}

var _GenericValue interface{}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fuzz:
file2fuzz -o ${corpusdir} ./go-fuzz-corpus/json/corpus/* ./corpus/*

run:
SONIC_FUZZ_MEM_LIMIT=2 GOMAXPROCS=4 GOARCH=amd64 go test -fuzz=${testname} -v -fuzztime 5m
SONIC_FUZZ_MEM_LIMIT=2 GOMAXPROCS=4 go test -fuzz=${testname} -v -fuzztime 5m

clean:
rm -rf ./go-fuzz-corpus/
Expand Down
46 changes: 46 additions & 0 deletions internal/base64/b64_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// +build amd64,go1.16

/**
* Copyright 2023 ByteDance Inc.
*
* 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 base64

import (
"github.com/cloudwego/base64x"
)

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

func EncodeBase64(buf []byte, src []byte) []byte {
if len(src) == 0 {
return append(buf, '"', '"')
}
buf = append(buf, '"')
need := base64x.StdEncoding.EncodedLen(len(src))
if cap(buf) - len(buf) < need {
tmp := make([]byte, len(buf), len(buf) + need*2)
copy(tmp, buf)
buf = tmp
}
base64x.StdEncoding.Encode(buf[len(buf):cap(buf)], src)
buf = buf[:len(buf) + need]
buf = append(buf, '"')
return buf
}


44 changes: 44 additions & 0 deletions internal/base64/b64_compat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// +build !amd64 !go1.16

/*
* Copyright 2022 ByteDance Inc.
*
* 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 base64

import (
"encoding/base64"
)

func EncodeBase64(buf []byte, src []byte) []byte {
if len(src) == 0 {
return append(buf, '"', '"')
}
buf = append(buf, '"')
need := base64.StdEncoding.EncodedLen(len(src))
if cap(buf) - len(buf) < need {
tmp := make([]byte, len(buf), len(buf) + need*2)
copy(tmp, buf)
buf = tmp
}
base64.StdEncoding.Encode(buf[len(buf):cap(buf)], src)
buf = buf[:len(buf) + need]
buf = append(buf, '"')
return buf
}

func DecodeBase64(src string) ([]byte, error) {
return base64.StdEncoding.DecodeString(src)
}
Loading

0 comments on commit dfcddaa

Please sign in to comment.