Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: make tHelper a type alias #1562

Merged
merged 1 commit into from Mar 5, 2024
Merged

Conversation

dolmen
Copy link
Collaborator

@dolmen dolmen commented Mar 4, 2024

Summary

The tHelper interface is defined only for internal usage. We don't need a "hard" type for it as we don't define methods on it. Let's make is just a alias to the anonymous interface it represents.

Note: we are already using type aliases elswhere, and type aliases were introduced a long time ago in Go 1.9.

Changes

  • assert: tHelper becomes a type alias
  • require: tHelper becomes a type alias

Motivation

The tHelper symbol is not needed at runtime. This should reduce resource usage (yes, negligible).

Related issues

The tHelper interface is defined only for internal usage. We don't need
a "hard" type for it as we don't define methods on it. Let's make is
just a alias to the anonymous interface it represents.

Note: we are already using type aliases elswhere, and type aliases were
introduced a long time ago in Go 1.9.
@dolmen dolmen self-assigned this Mar 4, 2024
@dolmen dolmen added pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require internal/refactor Refactor internals with no external visible changes labels Mar 4, 2024
Copy link
Collaborator

@brackendawson brackendawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You would normally do this to facilitate moving/combining previously declared types, which we aren't. If there is a performance difference, it is too small to measure. But I have no other reason to object to this.

Benchmarks
type impl struct{}

func (impl) Helper() {
	_ = math.Abs(-1)
}

func BenchmarkTypeAssertionWithDeclaredType(b *testing.B) {
	type helper interface {
		Helper()
	}

	var t any = &impl{}

	for i := 0; i < b.N; i++ {
		if t, ok := t.(helper); ok {
			t.Helper()
		}
	}
}

func BenchmarkTypeAssertionWithTypeAlias(b *testing.B) {
	type helper = interface {
		Helper()
	}

	var t any = &impl{}

	for i := 0; i < b.N; i++ {
		if t, ok := t.(helper); ok {
			t.Helper()
		}
	}
}
goos: darwin
goarch: arm64
pkg: github.com/brackendawson/kata
BenchmarkTypeAssertionWithDeclaredType-10       525305594                2.220 ns/op           0 B/op          0 allocs/op
BenchmarkTypeAssertionWithTypeAlias-10          544804904                2.235 ns/op           0 B/op          0 allocs/op
PASS
ok      github.com/brackendawson/kata   2.963s

@dolmen dolmen requested a review from arjunmahishi March 5, 2024 09:15
@dolmen
Copy link
Collaborator Author

dolmen commented Mar 5, 2024

@brackendawson There is no significant performance improvement (or degradation) expected. The resource usage I claim is just the absence of the type definition in the final binary, so a smaller binary.

Compare the output of this command on this branch vs master:

$ (cd assert ; go test -c && strings assert.test | grep BoolAssertionFunc)

@dolmen dolmen merged commit 89ffab0 into master Mar 5, 2024
14 checks passed
@dolmen dolmen deleted the make-tHelper-a-type-alias branch March 5, 2024 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal/refactor Refactor internals with no external visible changes pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants