feat(json): improve JSON performance with bytedance/sonic

This commit is contained in:
yusing
2025-09-29 17:43:34 +08:00
parent 024100aa8c
commit f411e17d80
29 changed files with 86 additions and 78 deletions

View File

@@ -2,10 +2,10 @@ package homepage
import (
"encoding/base64"
"encoding/json"
"sync"
"time"
"github.com/bytedance/sonic"
"github.com/rs/zerolog/log"
"github.com/yusing/godoxy/internal/common"
"github.com/yusing/godoxy/internal/jsonstore"
@@ -123,8 +123,8 @@ func (e *cacheEntry) UnmarshalJSON(data []byte) error {
LastAccess time.Time `json:"last_access"`
}
// check if data is json
if json.Valid(data) {
err := json.Unmarshal(data, &tmp)
if sonic.Valid(data) {
err := sonic.Unmarshal(data, &tmp)
// return only if unmarshal is successful
// otherwise fallback to base64
if err == nil {

View File

@@ -2,11 +2,11 @@ package qbittorrent
import (
"context"
"encoding/json"
"io"
"net/http"
"net/url"
"github.com/bytedance/sonic"
"github.com/yusing/godoxy/internal/homepage/widgets"
gperr "github.com/yusing/goutils/errs"
)
@@ -59,7 +59,7 @@ func jsonRequest[T any](ctx context.Context, client *Client, endpoint string, qu
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&result)
err = sonic.ConfigDefault.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return result, err
}

View File

@@ -2,10 +2,11 @@ package qbittorrent
import (
"context"
"encoding/json"
"net/url"
"strconv"
"time"
"github.com/bytedance/sonic"
)
const endpointLogs = "/api/v2/log/main"
@@ -44,7 +45,7 @@ func (l *LogEntry) Level() string {
}
func (l *LogEntry) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
return sonic.Marshal(map[string]any{
"id": l.ID,
"timestamp": l.Timestamp,
"level": l.Level(),

View File

@@ -2,7 +2,6 @@ package homepage
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
@@ -11,6 +10,7 @@ import (
"sync"
"time"
"github.com/bytedance/sonic"
"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/rs/zerolog/log"
"github.com/yusing/godoxy/internal/common"
@@ -310,7 +310,7 @@ func UpdateWalkxCodeIcons() error {
}
data := make(map[string][]string)
err = json.Unmarshal(body, &data)
err = sonic.Unmarshal(body, &data)
if err != nil {
return err
}
@@ -381,7 +381,7 @@ func UpdateSelfhstIcons() error {
}
data := make([]SelfhStIcon, 0)
err = json.Unmarshal(body, &data) //nolint:musttag
err = sonic.Unmarshal(body, &data) //nolint:musttag
if err != nil {
return err
}