happy path perf optimization

move goroutineID() inside the defer, guarded by !finished. It's safe — Goexit runs defers in the same goroutine, so the ID is stable.
This commit is contained in:
djmil 2026-04-23 20:02:34 +00:00
parent 9599b8c0a3
commit 974fed55d3

View File

@ -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"))