From 974fed55d3ddd1b7d1e8a2e2e0d4daddcb57749a Mon Sep 17 00:00:00 2001 From: djmil Date: Thu, 23 Apr 2026 20:02:34 +0000 Subject: [PATCH] happy path perf optimization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit move goroutineID() inside the defer, guarded by !finished. It's safe — Goexit runs defers in the same goroutine, so the ID is stable. --- pkg/result/result.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/result/result.go b/pkg/result/result.go index 4f2ddb2..edf677a 100644 --- a/pkg/result/result.go +++ b/pkg/result/result.go @@ -151,7 +151,6 @@ func Async[T any](fn func() T) <-chan Expect[T] { val T finished bool ) - id := goroutineID() defer func() { if v := recover(); v != nil { if err, ok := v.(*stackError); ok { @@ -165,7 +164,8 @@ func Async[T any](fn func() T) <-chan Expect[T] { ch <- Ok(val) return } - if stored, ok := gErrors.LoadAndDelete(id); ok { + // goroutineID is looked up here, on the error path only. + if stored, ok := gErrors.LoadAndDelete(goroutineID()); ok { ch <- Fail[T](stored.(error)) } else { ch <- Fail[T](errors.New("goroutine exited unexpectedly"))