mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-14 14:45:49 +01:00
* perf(v3): optimize JSON processing and reduce allocations in hot paths - Switch to goccy/go-json for method binding, events, and HTTP transport (21-63% faster, 40-60% less memory for method calls) - Optimize BoundMethod struct layout to reduce padding (144 -> 136 bytes) - Cache isVariadic flag at registration to avoid reflect call per invocation - Use stack-allocated buffer for method arguments (<=8 args) - Optimize result collection to avoid slice allocation for single return values - Use sync.Map for MIME cache to improve concurrent read performance - Use buffer pool for HTTP transport request body reading - Lazily allocate CloseNotify channel in content type sniffer - Remove debug CSS logging from asset server - Add comprehensive benchmark tests (build tag: bench) Performance improvements for BoundMethod.Call: - SimpleCall: 1290ns -> 930ns (28% faster), 240B -> 80B (67% less memory) - ComplexCall: 10500ns -> 3900ns (63% faster), 1192B -> 1020B (14% less) - VariadicCall: 3460ns -> 1600ns (54% faster), 512B -> 289B (44% less) * perf(v3): add max size limit to buffer pool to prevent memory bloat Buffers larger than 512KB are not returned to the pool, allowing GC to reclaim memory after large requests (e.g., base64 encoded images). * perf(v3): remove mimetype library dependency, saving ~208KB binary size - Replace github.com/wailsapp/mimetype with expanded extension map + stdlib - Expand MIME type map from 16 to 50+ common web formats (fonts, audio, video, etc.) - Add comprehensive test suite validating MIME detection for all web formats - Use http.DetectContentType as fallback for unknown extensions - Actual binary size reduction: 1.2MB (11MB -> 9.8MB in test app) * perf(v3): migrate all runtime code to goccy/go-json Migrate remaining encoding/json usages to goccy/go-json in: - pkg/application (android, darwin, ios, single_instance, webview_window) - pkg/services (kvstore, notifications on all platforms) - internal/assetserver/webview (request/response handling) - internal/runtime and internal/capabilities Note: encoding/json (110KB) remains in binary because: 1. goccy/go-json imports it for interface compatibility (json.Marshaler, etc.) 2. log/slog (stdlib) uses it for JSON output The performance benefit is in the hot paths which now use the faster library. * perf(v3): replace gopkg.in/ini.v1 with minimal .desktop file parser Replace the gopkg.in/ini.v1 dependency with a purpose-built minimal parser for Linux .desktop files. The new parser: - Only extracts the Exec key from [Desktop Entry] section (all we need) - Follows the Desktop Entry Specification - Has comprehensive test coverage (40 tests) including: - All major file managers (Nautilus, Dolphin, Thunar, PCManFM, Caja, Nemo) - Edge cases (UTF-8, special chars, comments, empty files, etc.) - Buffer limit handling Binary size reduction: 45KB (10.22MB -> 10.18MB) * perf(v3): remove samber/lo from runtime code, saving ~310KB binary size Replace samber/lo with Go 1.21+ stdlib slices package and minimal internal helpers in all runtime code paths. This removes 80 transitive dependencies from the production binary. Changes: - Create internal/sliceutil package with Unique and FindMapKey helpers - Replace lo.Without with slices.DeleteFunc in event handling - Replace lo.Ternary with inline if/else in Windows code - Replace lo.Uniq with sliceutil.Unique for feature flags - Replace lo.FindKey with sliceutil.FindMapKey for method aliases - Replace lo.Filter with slices.DeleteFunc in event listeners - Replace lo.Must with inline panic in w32 package Binary size: 10.18MB -> 9.87MB (~310KB / 3% reduction) Note: CLI tools still use samber/lo since they don't affect production binary size. The application_debug.go file also retains lo usage as it has //go:build !production tag. * fix: address CodeRabbit review comments - Use application/x-typescript MIME type (not IANA-registered text/typescript) - Fix potential panic in mimetype_stdlib_test.go for short MIME strings - Use cached isVariadic flag in bindings_optimized_bench_test.go * fix: initialize goccy/go-json decoder early to fix Windows test failure On Windows, goccy/go-json's type address calculation can fail if the decoder is first invoked during test execution rather than at init time. Force early initialization by unmarshaling a []int during package init. See: https://github.com/goccy/go-json/issues/474 * 📝 Add docstrings to `v3/performance-improvements` (#4844) * fix: initialize goccy/go-json decoder early to fix Windows test failure On Windows, goccy/go-json's type address calculation can fail if the decoder is first invoked during test execution rather than at init time. Force early initialization by unmarshaling a []int during package init. See: https://github.com/goccy/go-json/issues/474 * 📝 Add docstrings to `v3/performance-improvements` Docstrings generation was requested by @leaanthony. * https://github.com/wailsapp/wails/pull/4843#issuecomment-3703472562 The following files were modified: * `v3/internal/assetserver/common.go` * `v3/internal/assetserver/content_type_sniffer.go` * `v3/internal/assetserver/mimecache.go` * `v3/internal/fileexplorer/desktopfile.go` * `v3/internal/fileexplorer/fileexplorer_linux.go` * `v3/internal/sliceutil/sliceutil.go` * `v3/pkg/application/application_ios.go` * `v3/pkg/application/bindings.go` * `v3/pkg/application/ios_runtime_ios.go` * `v3/pkg/w32/window.go` --------- Co-authored-by: Lea Anthony <lea.anthony@gmail.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
270 lines
5.7 KiB
Go
270 lines
5.7 KiB
Go
//go:build bench && goexperiment.jsonv2
|
|
|
|
package application_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"encoding/json/jsontext"
|
|
jsonv2 "encoding/json/v2"
|
|
"testing"
|
|
)
|
|
|
|
// Benchmark structures matching real Wails usage patterns
|
|
|
|
type SimpleArg struct {
|
|
Name string `json:"name"`
|
|
Value int `json:"value"`
|
|
}
|
|
|
|
type ComplexArg struct {
|
|
ID int `json:"id"`
|
|
Name string `json:"name"`
|
|
Tags []string `json:"tags"`
|
|
Metadata map[string]interface{} `json:"metadata"`
|
|
Nested *NestedArg `json:"nested,omitempty"`
|
|
}
|
|
|
|
type NestedArg struct {
|
|
Value float64 `json:"value"`
|
|
Enabled bool `json:"enabled"`
|
|
}
|
|
|
|
type CallResult struct {
|
|
Success bool `json:"success"`
|
|
Data interface{} `json:"data,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
// Test data
|
|
var (
|
|
simpleArgJSON = []byte(`{"name":"test","value":42}`)
|
|
complexArgJSON = []byte(`{
|
|
"id": 12345,
|
|
"name": "Test Complex Data",
|
|
"tags": ["tag1", "tag2", "tag3", "tag4", "tag5"],
|
|
"metadata": {"key1": "value1", "key2": 42, "key3": true},
|
|
"nested": {"value": 3.14159, "enabled": true}
|
|
}`)
|
|
|
|
simpleResult = CallResult{
|
|
Success: true,
|
|
Data: "hello world",
|
|
}
|
|
|
|
complexResult = CallResult{
|
|
Success: true,
|
|
Data: ComplexArg{
|
|
ID: 12345,
|
|
Name: "Result Data",
|
|
Tags: []string{"a", "b", "c"},
|
|
Metadata: map[string]interface{}{
|
|
"processed": true,
|
|
"count": 100,
|
|
},
|
|
Nested: &NestedArg{Value: 2.718, Enabled: true},
|
|
},
|
|
}
|
|
)
|
|
|
|
// === UNMARSHAL BENCHMARKS (argument parsing) ===
|
|
|
|
func BenchmarkJSONv1_Unmarshal_Simple(b *testing.B) {
|
|
for b.Loop() {
|
|
var arg SimpleArg
|
|
_ = json.Unmarshal(simpleArgJSON, &arg)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJSONv2_Unmarshal_Simple(b *testing.B) {
|
|
for b.Loop() {
|
|
var arg SimpleArg
|
|
_ = jsonv2.Unmarshal(simpleArgJSON, &arg)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJSONv1_Unmarshal_Complex(b *testing.B) {
|
|
for b.Loop() {
|
|
var arg ComplexArg
|
|
_ = json.Unmarshal(complexArgJSON, &arg)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJSONv2_Unmarshal_Complex(b *testing.B) {
|
|
for b.Loop() {
|
|
var arg ComplexArg
|
|
_ = jsonv2.Unmarshal(complexArgJSON, &arg)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJSONv1_Unmarshal_Interface(b *testing.B) {
|
|
for b.Loop() {
|
|
var arg interface{}
|
|
_ = json.Unmarshal(complexArgJSON, &arg)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJSONv2_Unmarshal_Interface(b *testing.B) {
|
|
for b.Loop() {
|
|
var arg interface{}
|
|
_ = jsonv2.Unmarshal(complexArgJSON, &arg)
|
|
}
|
|
}
|
|
|
|
// === MARSHAL BENCHMARKS (result serialization) ===
|
|
|
|
func BenchmarkJSONv1_Marshal_Simple(b *testing.B) {
|
|
for b.Loop() {
|
|
_, _ = json.Marshal(simpleResult)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJSONv2_Marshal_Simple(b *testing.B) {
|
|
for b.Loop() {
|
|
_, _ = jsonv2.Marshal(simpleResult)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJSONv1_Marshal_Complex(b *testing.B) {
|
|
for b.Loop() {
|
|
_, _ = json.Marshal(complexResult)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJSONv2_Marshal_Complex(b *testing.B) {
|
|
for b.Loop() {
|
|
_, _ = jsonv2.Marshal(complexResult)
|
|
}
|
|
}
|
|
|
|
// === RAW MESSAGE HANDLING (common in Wails bindings) ===
|
|
|
|
func BenchmarkJSONv1_RawMessage_Unmarshal(b *testing.B) {
|
|
raw := json.RawMessage(complexArgJSON)
|
|
for b.Loop() {
|
|
var arg ComplexArg
|
|
_ = json.Unmarshal(raw, &arg)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJSONv2_RawMessage_Unmarshal(b *testing.B) {
|
|
raw := jsontext.Value(complexArgJSON)
|
|
for b.Loop() {
|
|
var arg ComplexArg
|
|
_ = jsonv2.Unmarshal(raw, &arg)
|
|
}
|
|
}
|
|
|
|
// === SLICE ARGUMENTS (common pattern) ===
|
|
|
|
var sliceArgJSON = []byte(`[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`)
|
|
var largeSliceArgJSON = func() []byte {
|
|
data, _ := json.Marshal(make([]int, 100))
|
|
return data
|
|
}()
|
|
|
|
func BenchmarkJSONv1_Unmarshal_Slice(b *testing.B) {
|
|
for b.Loop() {
|
|
var arg []int
|
|
_ = json.Unmarshal(sliceArgJSON, &arg)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJSONv2_Unmarshal_Slice(b *testing.B) {
|
|
for b.Loop() {
|
|
var arg []int
|
|
_ = jsonv2.Unmarshal(sliceArgJSON, &arg)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJSONv1_Unmarshal_LargeSlice(b *testing.B) {
|
|
for b.Loop() {
|
|
var arg []int
|
|
_ = json.Unmarshal(largeSliceArgJSON, &arg)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJSONv2_Unmarshal_LargeSlice(b *testing.B) {
|
|
for b.Loop() {
|
|
var arg []int
|
|
_ = jsonv2.Unmarshal(largeSliceArgJSON, &arg)
|
|
}
|
|
}
|
|
|
|
// === STRING ARGUMENT (most common) ===
|
|
|
|
var stringArgJSON = []byte(`"hello world this is a test string"`)
|
|
|
|
func BenchmarkJSONv1_Unmarshal_String(b *testing.B) {
|
|
for b.Loop() {
|
|
var arg string
|
|
_ = json.Unmarshal(stringArgJSON, &arg)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJSONv2_Unmarshal_String(b *testing.B) {
|
|
for b.Loop() {
|
|
var arg string
|
|
_ = jsonv2.Unmarshal(stringArgJSON, &arg)
|
|
}
|
|
}
|
|
|
|
// === MULTIPLE ARGUMENTS (simulating method call) ===
|
|
|
|
var multiArgJSON = [][]byte{
|
|
[]byte(`"arg1"`),
|
|
[]byte(`42`),
|
|
[]byte(`true`),
|
|
[]byte(`{"key": "value"}`),
|
|
}
|
|
|
|
func BenchmarkJSONv1_Unmarshal_MultiArgs(b *testing.B) {
|
|
for b.Loop() {
|
|
var s string
|
|
var i int
|
|
var bl bool
|
|
var m map[string]string
|
|
_ = json.Unmarshal(multiArgJSON[0], &s)
|
|
_ = json.Unmarshal(multiArgJSON[1], &i)
|
|
_ = json.Unmarshal(multiArgJSON[2], &bl)
|
|
_ = json.Unmarshal(multiArgJSON[3], &m)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJSONv2_Unmarshal_MultiArgs(b *testing.B) {
|
|
for b.Loop() {
|
|
var s string
|
|
var i int
|
|
var bl bool
|
|
var m map[string]string
|
|
_ = jsonv2.Unmarshal(multiArgJSON[0], &s)
|
|
_ = jsonv2.Unmarshal(multiArgJSON[1], &i)
|
|
_ = jsonv2.Unmarshal(multiArgJSON[2], &bl)
|
|
_ = jsonv2.Unmarshal(multiArgJSON[3], &m)
|
|
}
|
|
}
|
|
|
|
// === ERROR RESPONSE MARSHALING ===
|
|
|
|
type ErrorResponse struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Details string `json:"details,omitempty"`
|
|
}
|
|
|
|
var errorResp = ErrorResponse{
|
|
Code: 500,
|
|
Message: "Internal server error",
|
|
Details: "Something went wrong while processing the request",
|
|
}
|
|
|
|
func BenchmarkJSONv1_Marshal_Error(b *testing.B) {
|
|
for b.Loop() {
|
|
_, _ = json.Marshal(errorResp)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJSONv2_Marshal_Error(b *testing.B) {
|
|
for b.Loop() {
|
|
_, _ = jsonv2.Marshal(errorResp)
|
|
}
|
|
}
|