-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow changing the implementation of Interface at run-time (#562)
* Allow changing the implementation of Interface at run-time This is primarily necessary during tests, as most production code won't need to change, but this is based on an internal detail of atomic.Value, which asserts the underlying type of the value being Store'd is always identical. Since it doesn't see Interface, it only sees interface{} (aka any), it actually cares about the concrete type. There are various ways we can resolve this, covered in the benchmark, and with the current compiler using atomic.Value with a concrete struct type seems to be the winner. This is faster than the older interface version because the type assertion to a concrete type is faster than the type assertion to an interface type. * Be more cautious about the global load and add a test to ensure it doesn't panic * Remove the 1.18-only (generic) benchmark since it's not actually _doing_ anything * it's 1.19
- Loading branch information
1 parent
a3a5459
commit 316e844
Showing
2 changed files
with
125 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package ecinterface | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
) | ||
|
||
func TestGlobal_doesntPanic(t *testing.T) { | ||
Get() | ||
} | ||
|
||
func BenchmarkAtomics(b *testing.B) { | ||
// Example results: | ||
// | ||
// BenchmarkAtomics/rwmutex-10 85623068 13.76 ns/op | ||
// BenchmarkAtomics/atomicValue-10 283137508 4.204 ns/op | ||
// BenchmarkAtomics/atomicStructValue-10 703642080 1.705 ns/op | ||
// BenchmarkAtomics/atomicPointer-10 568728128 2.070 ns/op | ||
b.Run("current", func(b *testing.B) { | ||
Set(nop{}) | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
var count int | ||
for i := 0; i < b.N; i++ { | ||
if Get() != nil { | ||
count++ | ||
} | ||
} | ||
if count != b.N { | ||
b.Fatalf("this is just to avoid eliding the call") | ||
} | ||
}) | ||
b.Run("rwmutex", func(b *testing.B) { | ||
var scope struct { | ||
rw sync.RWMutex | ||
Interface | ||
} | ||
scope.Interface = nop{} | ||
get := func() Interface { | ||
scope.rw.RLock() | ||
defer scope.rw.RUnlock() | ||
return scope.Interface | ||
} | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
var count int | ||
for i := 0; i < b.N; i++ { | ||
if get() != nil { | ||
count++ | ||
} | ||
} | ||
if count != b.N { | ||
b.Fatalf("this is just to avoid eliding the call") | ||
} | ||
}) | ||
b.Run("atomicValue", func(b *testing.B) { | ||
var global atomic.Value | ||
global.Store(nop{}) | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
var count int | ||
for i := 0; i < b.N; i++ { | ||
if global.Load().(Interface) != nil { | ||
count++ | ||
} | ||
} | ||
if count != b.N { | ||
b.Fatalf("this is just to avoid eliding the call") | ||
} | ||
}) | ||
b.Run("atomicStructValue", func(b *testing.B) { | ||
var global atomic.Value | ||
type current struct{ Interface } | ||
global.Store(current{nop{}}) | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
var count int | ||
for i := 0; i < b.N; i++ { | ||
if global.Load().(current).Interface != nil { | ||
count++ | ||
} | ||
} | ||
if count != b.N { | ||
b.Fatalf("this is just to avoid eliding the call") | ||
} | ||
}) | ||
/* If you have go1.19 with the new atomic APIs, you can throw this one in for fun: | ||
b.Run("atomicPointer", func(b *testing.B) { | ||
var global atomic.Pointer[Interface] | ||
var impl Interface = nop{} | ||
global.Store(&impl) | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
var count int | ||
for i := 0; i < b.N; i++ { | ||
if *global.Load() != nil { | ||
count++ | ||
} | ||
} | ||
if count != b.N { | ||
b.Fatalf("this is just to avoid eliding the call") | ||
} | ||
}) | ||
*/ | ||
} |