mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-15 15:15:51 +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>
442 lines
10 KiB
Go
442 lines
10 KiB
Go
//go:build bench
|
|
|
|
package application
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/wailsapp/wails/v3/pkg/events"
|
|
)
|
|
|
|
// Note: This file uses internal package access to benchmark window internals
|
|
// without requiring GUI initialization.
|
|
|
|
// BenchmarkWindowEventRegistration measures the cost of registering window event listeners
|
|
func BenchmarkWindowEventRegistration(b *testing.B) {
|
|
b.Run("SingleListener", func(b *testing.B) {
|
|
for b.Loop() {
|
|
w := &WebviewWindow{
|
|
eventListeners: make(map[uint][]*WindowEventListener),
|
|
}
|
|
w.OnWindowEvent(events.Common.WindowFocus, func(event *WindowEvent) {})
|
|
}
|
|
})
|
|
|
|
b.Run("MultipleListenersSameEvent", func(b *testing.B) {
|
|
for b.Loop() {
|
|
w := &WebviewWindow{
|
|
eventListeners: make(map[uint][]*WindowEventListener),
|
|
}
|
|
for i := 0; i < 10; i++ {
|
|
w.OnWindowEvent(events.Common.WindowFocus, func(event *WindowEvent) {})
|
|
}
|
|
}
|
|
})
|
|
|
|
b.Run("MultipleListenersDifferentEvents", func(b *testing.B) {
|
|
eventTypes := []events.WindowEventType{
|
|
events.Common.WindowFocus,
|
|
events.Common.WindowLostFocus,
|
|
events.Common.WindowShow,
|
|
events.Common.WindowHide,
|
|
events.Common.WindowDidMove,
|
|
}
|
|
for b.Loop() {
|
|
w := &WebviewWindow{
|
|
eventListeners: make(map[uint][]*WindowEventListener),
|
|
}
|
|
for _, evt := range eventTypes {
|
|
w.OnWindowEvent(evt, func(event *WindowEvent) {})
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// BenchmarkWindowHookRegistration measures the cost of registering window event hooks
|
|
func BenchmarkWindowHookRegistration(b *testing.B) {
|
|
b.Run("SingleHook", func(b *testing.B) {
|
|
eventID := uint(events.Common.WindowClosing)
|
|
for b.Loop() {
|
|
w := &WebviewWindow{
|
|
eventHooks: make(map[uint][]*WindowEventListener),
|
|
}
|
|
w.eventHooksLock.Lock()
|
|
w.eventHooks[eventID] = append(w.eventHooks[eventID], &WindowEventListener{
|
|
callback: func(event *WindowEvent) {},
|
|
})
|
|
w.eventHooksLock.Unlock()
|
|
}
|
|
})
|
|
|
|
b.Run("MultipleHooks", func(b *testing.B) {
|
|
eventID := uint(events.Common.WindowClosing)
|
|
for b.Loop() {
|
|
w := &WebviewWindow{
|
|
eventHooks: make(map[uint][]*WindowEventListener),
|
|
}
|
|
for i := 0; i < 5; i++ {
|
|
w.eventHooksLock.Lock()
|
|
w.eventHooks[eventID] = append(w.eventHooks[eventID], &WindowEventListener{
|
|
callback: func(event *WindowEvent) {},
|
|
})
|
|
w.eventHooksLock.Unlock()
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// BenchmarkWindowEventDispatch measures the internal event dispatch mechanism
|
|
func BenchmarkWindowEventDispatch(b *testing.B) {
|
|
listenerCounts := []int{0, 1, 5, 10, 50}
|
|
|
|
for _, count := range listenerCounts {
|
|
b.Run(fmt.Sprintf("Listeners%d", count), func(b *testing.B) {
|
|
w := &WebviewWindow{
|
|
eventListeners: make(map[uint][]*WindowEventListener),
|
|
}
|
|
|
|
eventID := uint(events.Common.WindowFocus)
|
|
|
|
// Register listeners
|
|
for i := 0; i < count; i++ {
|
|
w.eventListeners[eventID] = append(w.eventListeners[eventID], &WindowEventListener{
|
|
callback: func(event *WindowEvent) {
|
|
_ = event.IsCancelled()
|
|
},
|
|
})
|
|
}
|
|
|
|
b.ResetTimer()
|
|
for b.Loop() {
|
|
w.eventListenersLock.RLock()
|
|
listeners := w.eventListeners[eventID]
|
|
w.eventListenersLock.RUnlock()
|
|
_ = listeners
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// BenchmarkKeyBindingLookup measures key binding lookup performance
|
|
func BenchmarkKeyBindingLookup(b *testing.B) {
|
|
bindingCounts := []int{1, 10, 50, 100}
|
|
|
|
for _, count := range bindingCounts {
|
|
b.Run(fmt.Sprintf("Bindings%d", count), func(b *testing.B) {
|
|
w := &WebviewWindow{
|
|
keyBindings: make(map[string]func(Window)),
|
|
}
|
|
|
|
// Register bindings
|
|
for i := 0; i < count; i++ {
|
|
key := fmt.Sprintf("ctrl+shift+%c", 'a'+i%26)
|
|
w.keyBindings[key] = func(Window) {}
|
|
}
|
|
|
|
// Lookup key that exists
|
|
lookupKey := "ctrl+shift+m"
|
|
w.keyBindings[lookupKey] = func(Window) {}
|
|
|
|
b.ResetTimer()
|
|
for b.Loop() {
|
|
w.keyBindingsLock.RLock()
|
|
_ = w.keyBindings[lookupKey]
|
|
w.keyBindingsLock.RUnlock()
|
|
}
|
|
})
|
|
}
|
|
|
|
b.Run("MissLookup", func(b *testing.B) {
|
|
w := &WebviewWindow{
|
|
keyBindings: make(map[string]func(Window)),
|
|
}
|
|
|
|
// Register some bindings
|
|
for i := 0; i < 50; i++ {
|
|
key := fmt.Sprintf("ctrl+shift+%c", 'a'+i%26)
|
|
w.keyBindings[key] = func(Window) {}
|
|
}
|
|
|
|
lookupKey := "ctrl+alt+nonexistent"
|
|
|
|
b.ResetTimer()
|
|
for b.Loop() {
|
|
w.keyBindingsLock.RLock()
|
|
_ = w.keyBindings[lookupKey]
|
|
w.keyBindingsLock.RUnlock()
|
|
}
|
|
})
|
|
}
|
|
|
|
// BenchmarkConcurrentWindowOps measures concurrent access patterns
|
|
func BenchmarkConcurrentWindowOps(b *testing.B) {
|
|
b.Run("ConcurrentEventLookup", func(b *testing.B) {
|
|
w := &WebviewWindow{
|
|
eventListeners: make(map[uint][]*WindowEventListener),
|
|
}
|
|
|
|
eventID := uint(events.Common.WindowFocus)
|
|
for i := 0; i < 10; i++ {
|
|
w.eventListeners[eventID] = append(w.eventListeners[eventID], &WindowEventListener{
|
|
callback: func(event *WindowEvent) {},
|
|
})
|
|
}
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
for pb.Next() {
|
|
w.eventListenersLock.RLock()
|
|
_ = w.eventListeners[eventID]
|
|
w.eventListenersLock.RUnlock()
|
|
}
|
|
})
|
|
})
|
|
|
|
b.Run("ConcurrentKeyBindingLookup", func(b *testing.B) {
|
|
w := &WebviewWindow{
|
|
keyBindings: make(map[string]func(Window)),
|
|
}
|
|
|
|
for i := 0; i < 50; i++ {
|
|
key := fmt.Sprintf("ctrl+shift+%c", 'a'+i%26)
|
|
w.keyBindings[key] = func(Window) {}
|
|
}
|
|
|
|
keys := []string{"ctrl+shift+a", "ctrl+shift+m", "ctrl+shift+z"}
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
i := 0
|
|
for pb.Next() {
|
|
w.keyBindingsLock.RLock()
|
|
_ = w.keyBindings[keys[i%len(keys)]]
|
|
w.keyBindingsLock.RUnlock()
|
|
i++
|
|
}
|
|
})
|
|
})
|
|
|
|
b.Run("MixedReadWrite", func(b *testing.B) {
|
|
w := &WebviewWindow{
|
|
eventListeners: make(map[uint][]*WindowEventListener),
|
|
}
|
|
|
|
eventID := uint(events.Common.WindowFocus)
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
i := 0
|
|
for pb.Next() {
|
|
if i%10 == 0 {
|
|
// Write operation (10% of ops)
|
|
w.eventListenersLock.Lock()
|
|
w.eventListeners[eventID] = append(w.eventListeners[eventID], &WindowEventListener{
|
|
callback: func(event *WindowEvent) {},
|
|
})
|
|
w.eventListenersLock.Unlock()
|
|
} else {
|
|
// Read operation (90% of ops)
|
|
w.eventListenersLock.RLock()
|
|
_ = w.eventListeners[eventID]
|
|
w.eventListenersLock.RUnlock()
|
|
}
|
|
i++
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
// BenchmarkWindowEventCreation measures WindowEvent allocation
|
|
func BenchmarkWindowEventCreation(b *testing.B) {
|
|
for b.Loop() {
|
|
event := NewWindowEvent()
|
|
_ = event
|
|
}
|
|
}
|
|
|
|
// BenchmarkWindowEventCancellation measures cancel/check operations
|
|
func BenchmarkWindowEventCancellation(b *testing.B) {
|
|
b.Run("Cancel", func(b *testing.B) {
|
|
for b.Loop() {
|
|
event := NewWindowEvent()
|
|
event.Cancel()
|
|
}
|
|
})
|
|
|
|
b.Run("IsCancelled", func(b *testing.B) {
|
|
event := NewWindowEvent()
|
|
for b.Loop() {
|
|
_ = event.IsCancelled()
|
|
}
|
|
})
|
|
|
|
b.Run("CancelledCheck", func(b *testing.B) {
|
|
event := NewWindowEvent()
|
|
event.Cancel()
|
|
for b.Loop() {
|
|
_ = event.IsCancelled()
|
|
}
|
|
})
|
|
}
|
|
|
|
// BenchmarkWindowOptionsInit measures window options initialization patterns
|
|
func BenchmarkWindowOptionsInit(b *testing.B) {
|
|
b.Run("DefaultOptions", func(b *testing.B) {
|
|
for b.Loop() {
|
|
opts := WebviewWindowOptions{}
|
|
_ = opts
|
|
}
|
|
})
|
|
|
|
b.Run("CommonOptions", func(b *testing.B) {
|
|
for b.Loop() {
|
|
opts := WebviewWindowOptions{
|
|
Title: "Test Window",
|
|
Width: 800,
|
|
Height: 600,
|
|
MinWidth: 400,
|
|
MinHeight: 300,
|
|
}
|
|
_ = opts
|
|
}
|
|
})
|
|
|
|
b.Run("FullOptions", func(b *testing.B) {
|
|
for b.Loop() {
|
|
opts := WebviewWindowOptions{
|
|
Title: "Full Test Window",
|
|
Width: 1024,
|
|
Height: 768,
|
|
MinWidth: 400,
|
|
MinHeight: 300,
|
|
MaxWidth: 1920,
|
|
MaxHeight: 1080,
|
|
URL: "http://localhost:8080",
|
|
Frameless: false,
|
|
DisableResize: false,
|
|
AlwaysOnTop: false,
|
|
Hidden: false,
|
|
EnableDragAndDrop: true,
|
|
BackgroundColour: RGBA{Red: 255, Green: 255, Blue: 255, Alpha: 255},
|
|
}
|
|
_ = opts
|
|
}
|
|
})
|
|
}
|
|
|
|
// BenchmarkMenuBindingLookup measures menu binding lookups
|
|
func BenchmarkMenuBindingLookup(b *testing.B) {
|
|
w := &WebviewWindow{
|
|
menuBindings: make(map[string]*MenuItem),
|
|
}
|
|
|
|
// Register some menu bindings
|
|
for i := 0; i < 50; i++ {
|
|
id := fmt.Sprintf("menu-item-%d", i)
|
|
w.menuBindings[id] = &MenuItem{id: uint(i)}
|
|
}
|
|
|
|
lookupID := "menu-item-25"
|
|
|
|
b.Run("Hit", func(b *testing.B) {
|
|
for b.Loop() {
|
|
w.menuBindingsLock.RLock()
|
|
_ = w.menuBindings[lookupID]
|
|
w.menuBindingsLock.RUnlock()
|
|
}
|
|
})
|
|
|
|
b.Run("Miss", func(b *testing.B) {
|
|
missID := "nonexistent-menu-item"
|
|
for b.Loop() {
|
|
w.menuBindingsLock.RLock()
|
|
_ = w.menuBindings[missID]
|
|
w.menuBindingsLock.RUnlock()
|
|
}
|
|
})
|
|
}
|
|
|
|
// BenchmarkWindowDestroyedCheck measures the destroyed flag check pattern
|
|
func BenchmarkWindowDestroyedCheck(b *testing.B) {
|
|
w := &WebviewWindow{}
|
|
|
|
b.Run("NotDestroyed", func(b *testing.B) {
|
|
for b.Loop() {
|
|
w.destroyedLock.RLock()
|
|
_ = w.destroyed
|
|
w.destroyedLock.RUnlock()
|
|
}
|
|
})
|
|
|
|
b.Run("Destroyed", func(b *testing.B) {
|
|
w.destroyed = true
|
|
for b.Loop() {
|
|
w.destroyedLock.RLock()
|
|
_ = w.destroyed
|
|
w.destroyedLock.RUnlock()
|
|
}
|
|
})
|
|
}
|
|
|
|
// BenchmarkCancellerManagement measures canceller function management
|
|
func BenchmarkCancellerManagement(b *testing.B) {
|
|
b.Run("AddCanceller", func(b *testing.B) {
|
|
for b.Loop() {
|
|
w := &WebviewWindow{
|
|
cancellers: make([]func(), 0),
|
|
}
|
|
for i := 0; i < 10; i++ {
|
|
w.cancellersLock.Lock()
|
|
w.cancellers = append(w.cancellers, func() {})
|
|
w.cancellersLock.Unlock()
|
|
}
|
|
}
|
|
})
|
|
|
|
b.Run("ExecuteCancellers", func(b *testing.B) {
|
|
w := &WebviewWindow{
|
|
cancellers: make([]func(), 100),
|
|
}
|
|
for i := 0; i < 100; i++ {
|
|
w.cancellers[i] = func() {}
|
|
}
|
|
|
|
b.ResetTimer()
|
|
for b.Loop() {
|
|
w.cancellersLock.RLock()
|
|
cancellers := w.cancellers
|
|
w.cancellersLock.RUnlock()
|
|
for _, cancel := range cancellers {
|
|
cancel()
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// BenchmarkRWMutexPatterns compares different locking patterns
|
|
func BenchmarkRWMutexPatterns(b *testing.B) {
|
|
b.Run("RLockUnlock", func(b *testing.B) {
|
|
var mu sync.RWMutex
|
|
for b.Loop() {
|
|
mu.RLock()
|
|
mu.RUnlock()
|
|
}
|
|
})
|
|
|
|
b.Run("LockUnlock", func(b *testing.B) {
|
|
var mu sync.RWMutex
|
|
for b.Loop() {
|
|
mu.Lock()
|
|
mu.Unlock()
|
|
}
|
|
})
|
|
|
|
b.Run("DeferredRLock", func(b *testing.B) {
|
|
var mu sync.RWMutex
|
|
for b.Loop() {
|
|
func() {
|
|
mu.RLock()
|
|
defer mu.RUnlock()
|
|
}()
|
|
}
|
|
})
|
|
}
|