Skip to content

Commit 48750a0

Browse files
committed
rm X() functions & cleanup + MKey OK idiom bench for ref
1 parent e670166 commit 48750a0

File tree

2 files changed

+30
-81
lines changed

2 files changed

+30
-81
lines changed

assert/assert.go

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -291,37 +291,6 @@ func ThatNot(term bool, a ...any) {
291291
}
292292
}
293293

294-
func ZeroX[T Number](val T, a ...any) {
295-
if val != 0 {
296-
doZeroX(val, a)
297-
}
298-
}
299-
300-
func doZeroX[T Number](val T, a []any) {
301-
defMsg := fmt.Sprintf(assertionMsg+": got '%v', want (== '0')", val)
302-
currentX().reportAssertionFault(1, defMsg, a)
303-
}
304-
305-
func currentX() (curAsserter asserter) {
306-
// we need thread local storage, maybe we'll implement that to x.package?
307-
// study `tester` and copy ideas from it.
308-
tlsID := goid()
309-
asserterMap.Rx(func(m map[int]asserter) {
310-
aster, found := m[tlsID]
311-
if found {
312-
curAsserter = aster
313-
} else {
314-
// use pkg lvl asserter if asserter is not set
315-
curAsserter = defAsserter[def]
316-
}
317-
})
318-
return curAsserter
319-
}
320-
321-
func SetDefaultX(i defInd) {
322-
asserterMap.Set(goid(), defAsserter[i])
323-
}
324-
325294
// That asserts that the term is true. If not it panics with the given
326295
// formatting string. Thanks to inlining, the performance penalty is equal to a
327296
// single 'if-statement' that is almost nothing.
@@ -886,12 +855,6 @@ func MNotEmpty[M ~map[T]U, T comparable, U any](obj M, a ...any) {
886855
}
887856
}
888857

889-
func doEmptyNamed(not, name string, a []any) {
890-
not = x.Whom(not == assertionNot, " not ", "")
891-
defMsg := assertionMsg + ": " + name + " should" + not + "be empty"
892-
current().reportAssertionFault(1, defMsg, a)
893-
}
894-
895858
func doNamed(not, tname, got string, a []any) {
896859
not = x.Whom(not == assertionNot, " not ", "")
897860
defMsg := assertionMsg + ": " + tname + " should" + not + "be " + got
@@ -922,25 +885,14 @@ func NoError(err error, a ...any) {
922885
// Note that when [Plain] asserter is used ([SetDefault]), optional arguments
923886
// are used to override the auto-generated assert violation message.
924887
func Error(err error, a ...any) {
925-
if err == nil {
926-
doErrorX(a)
927-
}
928-
}
929-
930-
func ErrorX(err error, a ...any) {
931888
if err == nil {
932889
doError(a)
933890
}
934891
}
935892

936-
func doErrorX(a []any) {
937-
defMsg := "Error:" + assertionMsg + ": missing error"
938-
currentX().reportAssertionFault(0, defMsg, a)
939-
}
940-
941893
func doError(a []any) {
942894
defMsg := "Error:" + assertionMsg + ": missing error"
943-
current().reportAssertionFault(0, defMsg, a)
895+
current().reportAssertionFault(1, defMsg, a)
944896
}
945897

946898
// Greater asserts that the value is greater than want. If it is not it panics
@@ -1011,12 +963,14 @@ func doNotZero[T Number](val T, a []any) {
1011963
current().reportAssertionFault(1, defMsg, a)
1012964
}
1013965

1014-
// current returns a current default asserter used for package-level
1015-
// functions like assert.That().
966+
// current returns a current default asserter used for assert functions like
967+
// assert.That() in this gorounine.
1016968
//
1017-
// Note, this indexing stuff is done because of race detection to work on client
969+
// NOTE this indexing stuff is done because of race detection to work on client
1018970
// packages. And, yes, we have tested it. This is fastest way to make it without
1019971
// locks HERE. Only the setting the index is secured with the mutex.
972+
//
973+
// NOTE that since our TLS [asserterMap] we still continue to use indexing.
1020974
func current() (curAsserter asserter) {
1021975
// we need thread local storage, maybe we'll implement that to x.package?
1022976
// study `tester` and copy ideas from it.
@@ -1068,14 +1022,20 @@ func SetDefault(i defInd) (old defInd) {
10681022
return
10691023
}
10701024

1071-
// SetTLS set asserter index for the current thread (Tread Local Storage). That
1072-
// allows us to have multiple different asserter in use in the same app running.
1073-
// Let's say that in some function and its sub-functions want to return plain
1074-
// error messages instead of the panic asserts, they can use following:
1025+
// SetAsserter set asserter index for the current thread (Tread Local
1026+
// Storage). That allows us to have multiple different asserter in use in the
1027+
// same app running. Let's say that in some function and its sub-functions want
1028+
// to return plain error messages instead of the panic asserts, they can use
1029+
// following:
10751030
//
1076-
// assert.SetTLS(assert.Plain)
1077-
func SetTLS(i defInd) {
1031+
// assert.SetAsserter(assert.Plain)
1032+
func SetAsserter(i defInd) func() {
10781033
asserterMap.Set(goid(), defAsserter[i])
1034+
return popCurrentAsserter
1035+
}
1036+
1037+
func popCurrentAsserter() {
1038+
asserterMap.Del(goid())
10791039
}
10801040

10811041
// mapDefInd runtime asserters, that's why test asserts are removed for now.

assert/assert_test.go

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,18 @@ func BenchmarkMKeyExists(b *testing.B) {
268268
}
269269
}
270270

271+
func BenchmarkMKeyExistsOKIdiom(b *testing.B) {
272+
bs := map[int]int{0: 0, 1: 1}
273+
found := false
274+
for n := 0; n < b.N; n++ {
275+
_, ok := bs[1]
276+
if ok {
277+
found = ok
278+
}
279+
}
280+
_ = found
281+
}
282+
271283
func BenchmarkMNotEmpty(b *testing.B) {
272284
bs := map[int]int{0: 0, 1: 1}
273285
for n := 0; n < b.N; n++ {
@@ -401,22 +413,6 @@ func BenchmarkThat(b *testing.B) {
401413
}
402414
}
403415

404-
func BenchmarkThatX(b *testing.B) {
405-
assert.SetTLS(assert.Production)
406-
const four = 4
407-
for n := 0; n < b.N; n++ {
408-
assert.That(four == 2+2)
409-
}
410-
}
411-
412-
func BenchmarkZeroX(b *testing.B) {
413-
assert.SetTLS(assert.Production)
414-
const zero = 0
415-
for n := 0; n < b.N; n++ {
416-
assert.ZeroX(zero)
417-
}
418-
}
419-
420416
func BenchmarkZero(b *testing.B) {
421417
const zero = 0
422418
for n := 0; n < b.N; n++ {
@@ -436,13 +432,6 @@ func BenchmarkLess(b *testing.B) {
436432
}
437433
}
438434

439-
func BenchmarkErrorX(b *testing.B) {
440-
assert.SetDefaultX(assert.Production)
441-
for n := 0; n < b.N; n++ {
442-
assert.ErrorX(err2.ErrNotAccess)
443-
}
444-
}
445-
446435
func BenchmarkError(b *testing.B) {
447436
for n := 0; n < b.N; n++ {
448437
assert.Error(err2.ErrNotAccess)

0 commit comments

Comments
 (0)