mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
perf(json): replace encoding/json with bytedance/sonic
Migrate all JSON marshaling and unmarshaling operations to use github.com/bytedance/sonic for improved performance. This affects adapters, middleware, proxy handlers, and the sync store.
This commit is contained in:
2
go.mod
2
go.mod
@@ -3,6 +3,7 @@ module github.com/ez-api/ez-api
|
|||||||
go 1.24.5
|
go 1.24.5
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/bytedance/sonic v1.14.0
|
||||||
github.com/gin-gonic/gin v1.11.0
|
github.com/gin-gonic/gin v1.11.0
|
||||||
github.com/redis/go-redis/v9 v9.17.2
|
github.com/redis/go-redis/v9 v9.17.2
|
||||||
github.com/swaggo/files v1.0.1
|
github.com/swaggo/files v1.0.1
|
||||||
@@ -15,7 +16,6 @@ require (
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/KyleBanks/depth v1.2.1 // indirect
|
github.com/KyleBanks/depth v1.2.1 // indirect
|
||||||
github.com/bytedance/sonic v1.14.0 // indirect
|
|
||||||
github.com/bytedance/sonic/loader v0.3.0 // indirect
|
github.com/bytedance/sonic/loader v0.3.0 // indirect
|
||||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
github.com/cloudwego/base64x v0.1.6 // indirect
|
github.com/cloudwego/base64x v0.1.6 // indirect
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bytedance/sonic"
|
||||||
"github.com/ez-api/ez-api/internal/model"
|
"github.com/ez-api/ez-api/internal/model"
|
||||||
"github.com/ez-api/ez-api/internal/util"
|
"github.com/ez-api/ez-api/internal/util"
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
@@ -189,7 +189,7 @@ func (s *SyncService) SyncAll(db *gorm.DB) error {
|
|||||||
Group: group,
|
Group: group,
|
||||||
Models: models,
|
Models: models,
|
||||||
}
|
}
|
||||||
payload, err := json.Marshal(snap)
|
payload, err := sonic.Marshal(snap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("marshal provider %d: %w", p.ID, err)
|
return fmt.Errorf("marshal provider %d: %w", p.ID, err)
|
||||||
}
|
}
|
||||||
@@ -242,7 +242,7 @@ func (s *SyncService) SyncAll(db *gorm.DB) error {
|
|||||||
SupportsFIM: m.SupportsFIM,
|
SupportsFIM: m.SupportsFIM,
|
||||||
MaxOutputTokens: m.MaxOutputTokens,
|
MaxOutputTokens: m.MaxOutputTokens,
|
||||||
}
|
}
|
||||||
payload, err := json.Marshal(snap)
|
payload, err := sonic.Marshal(snap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("marshal model %s: %w", m.Name, err)
|
return fmt.Errorf("marshal model %s: %w", m.Name, err)
|
||||||
}
|
}
|
||||||
@@ -257,7 +257,7 @@ func (s *SyncService) SyncAll(db *gorm.DB) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SyncService) hsetJSON(ctx context.Context, key, field string, val interface{}) error {
|
func (s *SyncService) hsetJSON(ctx context.Context, key, field string, val interface{}) error {
|
||||||
payload, err := json.Marshal(val)
|
payload, err := sonic.Marshal(val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("marshal %s:%s: %w", key, field, err)
|
return fmt.Errorf("marshal %s:%s: %w", key, field, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,6 @@ go 1.24.5
|
|||||||
require (
|
require (
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/stretchr/testify v1.11.1 // indirect
|
github.com/stretchr/testify v1.11.1
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ package integration
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/bytedance/sonic"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ func TestEndToEnd(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func postJSONWithAuth[T any](t *testing.T, client *http.Client, url string, body interface{}, out T, token string) T {
|
func postJSONWithAuth[T any](t *testing.T, client *http.Client, url string, body interface{}, out T, token string) T {
|
||||||
b, err := json.Marshal(body)
|
b, err := sonic.Marshal(body)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
|
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
|
||||||
@@ -76,7 +76,7 @@ func postJSONWithAuth[T any](t *testing.T, client *http.Client, url string, body
|
|||||||
if out != nil {
|
if out != nil {
|
||||||
data, _ := io.ReadAll(resp.Body)
|
data, _ := io.ReadAll(resp.Body)
|
||||||
if len(data) > 0 {
|
if len(data) > 0 {
|
||||||
require.NoError(t, json.Unmarshal(data, out))
|
require.NoError(t, sonic.Unmarshal(data, out))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/bytedance/sonic"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -30,7 +31,7 @@ func main() {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(resp)
|
_ = sonic.ConfigDefault.NewEncoder(w).Encode(resp)
|
||||||
})
|
})
|
||||||
|
|
||||||
mux.HandleFunc("/v1/models", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/v1/models", func(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -41,7 +42,7 @@ func main() {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(resp)
|
_ = sonic.ConfigDefault.NewEncoder(w).Encode(resp)
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Println("mock-upstream listening on :8082")
|
log.Println("mock-upstream listening on :8082")
|
||||||
|
|||||||
Reference in New Issue
Block a user