-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Conversation
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.
There was a problem hiding this 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
@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 $ (cd assert ; go test -c && strings assert.test | grep BoolAssertionFunc) |
…nercloud/fleeting-plugin-hetzner!175) This MR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [github.com/stretchr/testify](https://github.com/stretchr/testify) | require | minor | `v1.9.0` -> `v1.10.0` | --- ### Release Notes <details> <summary>stretchr/testify (github.com/stretchr/testify)</summary> ### [`v1.10.0`](https://github.com/stretchr/testify/releases/tag/v1.10.0) [Compare Source](stretchr/testify@v1.9.0...v1.10.0) #### What's Changed ##### Functional Changes - Add PanicAssertionFunc by [@​fahimbagar](https://github.com/fahimbagar) in stretchr/testify#1337 - assert: deprecate CompareType by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1566 - assert: make YAML dependency pluggable via build tags by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1579 - assert: new assertion NotElementsMatch by [@​hendrywiranto](https://github.com/hendrywiranto) in stretchr/testify#1600 - mock: in order mock calls by [@​ReyOrtiz](https://github.com/ReyOrtiz) in stretchr/testify#1637 - Add assertion for NotErrorAs by [@​palsivertsen](https://github.com/palsivertsen) in stretchr/testify#1129 - Record Return Arguments of a Call by [@​jayd3e](https://github.com/jayd3e) in stretchr/testify#1636 - assert.EqualExportedValues: accepts everything by [@​redachl](https://github.com/redachl) in stretchr/testify#1586 ##### Fixes - assert: make tHelper a type alias by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1562 - Do not get argument again unnecessarily in Arguments.Error() by [@​TomWright](https://github.com/TomWright) in stretchr/testify#820 - Fix time.Time compare by [@​myxo](https://github.com/myxo) in stretchr/testify#1582 - assert.Regexp: handle \[]byte array properly by [@​kevinburkesegment](https://github.com/kevinburkesegment) in stretchr/testify#1587 - assert: collect.FailNow() should not panic by [@​marshall-lee](https://github.com/marshall-lee) in stretchr/testify#1481 - mock: simplify implementation of FunctionalOptions by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1571 - mock: caller information for unexpected method call by [@​spirin](https://github.com/spirin) in stretchr/testify#1644 - suite: fix test failures by [@​stevenh](https://github.com/stevenh) in stretchr/testify#1421 - Fix issue [#​1662](stretchr/testify#1662) (comparing infs should fail) by [@​ybrustin](https://github.com/ybrustin) in stretchr/testify#1663 - NotSame should fail if args are not pointers [#​1661](stretchr/testify#1661) by [@​sikehish](https://github.com/sikehish) in stretchr/testify#1664 - Increase timeouts in Test_Mock_Called_blocks to reduce flakiness in CI by [@​sikehish](https://github.com/sikehish) in stretchr/testify#1667 - fix: compare functional option names for indirect calls by [@​arjun-1](https://github.com/arjun-1) in stretchr/testify#1626 ##### Documantation, Build & CI - .gitignore: ignore "go test -c" binaries by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1565 - mock: improve doc by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1570 - mock: fix FunctionalOptions docs by [@​snirye](https://github.com/snirye) in stretchr/testify#1433 - README: link out to the excellent testifylint by [@​brackendawson](https://github.com/brackendawson) in stretchr/testify#1568 - assert: fix typo in comment by [@​JohnEndson](https://github.com/JohnEndson) in stretchr/testify#1580 - Correct the EventuallyWithT and EventuallyWithTf example by [@​JonCrowther](https://github.com/JonCrowther) in stretchr/testify#1588 - CI: bump softprops/action-gh-release from 1 to 2 by [@​dependabot](https://github.com/dependabot) in stretchr/testify#1575 - mock: document more alternatives to deprecated AnythingOfTypeArgument by [@​dolmen](https://github.com/dolmen) in stretchr/testify#1569 - assert: Correctly document EqualValues behavior by [@​brackendawson](https://github.com/brackendawson) in stretchr/testify#1593 - fix: grammar in godoc by [@​miparnisari](https://github.com/miparnisari) in stretchr/testify#1607 - .github/workflows: Run tests for Go 1.22 by [@​HaraldNordgren](https://github.com/HaraldNordgren) in stretchr/testify#1629 - Document suite's lack of support for t.Parallel by [@​brackendawson](https://github.com/brackendawson) in stretchr/testify#1645 - assert: fix typos in comments by [@​alexandear](https://github.com/alexandear) in stretchr/testify#1650 - mock: fix doc comment for NotBefore by [@​alexandear](https://github.com/alexandear) in stretchr/testify#1651 - Generate better comments for require package by [@​Neokil](https://github.com/Neokil) in stretchr/testify#1610 - README: replace Testify V2 notice with [@​dolmen](https://github.com/dolmen)'s V2 manifesto by [@​hendrywiranto](https://github.com/hendrywiranto) in stretchr/testify#1518 #### New Contributors - [@​fahimbagar](https://github.com/fahimbagar) made their first contribution in stretchr/testify#1337 - [@​TomWright](https://github.com/TomWright) made their first contribution in stretchr/testify#820 - [@​snirye](https://github.com/snirye) made their first contribution in stretchr/testify#1433 - [@​myxo](https://github.com/myxo) made their first contribution in stretchr/testify#1582 - [@​JohnEndson](https://github.com/JohnEndson) made their first contribution in stretchr/testify#1580 - [@​JonCrowther](https://github.com/JonCrowther) made their first contribution in stretchr/testify#1588 - [@​miparnisari](https://github.com/miparnisari) made their first contribution in stretchr/testify#1607 - [@​marshall-lee](https://github.com/marshall-lee) made their first contribution in stretchr/testify#1481 - [@​spirin](https://github.com/spirin) made their first contribution in stretchr/testify#1644 - [@​ReyOrtiz](https://github.com/ReyOrtiz) made their first contribution in stretchr/testify#1637 - [@​stevenh](https://github.com/stevenh) made their first contribution in stretchr/testify#1421 - [@​jayd3e](https://github.com/jayd3e) made their first contribution in stretchr/testify#1636 - [@​Neokil](https://github.com/Neokil) made their first contribution in stretchr/testify#1610 - [@​redachl](https://github.com/redachl) made their first contribution in stretchr/testify#1586 - [@​ybrustin](https://github.com/ybrustin) made their first contribution in stretchr/testify#1663 - [@​sikehish](https://github.com/sikehish) made their first contribution in stretchr/testify#1664 - [@​arjun-1](https://github.com/arjun-1) made their first contribution in stretchr/testify#1626 **Full Changelog**: stretchr/testify@v1.9.0...v1.10.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this MR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS4yOC4wIiwidXBkYXRlZEluVmVyIjoiMzkuMjguMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->
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
tHelper
becomes a type aliastHelper
becomes a type aliasMotivation
The
tHelper
symbol is not needed at runtime. This should reduce resource usage (yes, negligible).Related issues
interface { Helper() }
type, but not dependent