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

Slow rule speed #412

Open
lcd1232-epam opened this issue Sep 26, 2022 · 1 comment
Open

Slow rule speed #412

lcd1232-epam opened this issue Sep 26, 2022 · 1 comment

Comments

@lcd1232-epam
Copy link

Example code:

//doc:summary reports that you create mocks in an old-fashioned way
//doc:before mockReader := &iomocks.Reader{}
//doc:after mockReader := iomocks.NewReader(t)
func mockConstructor(m dsl.Matcher) {
	m.Import("github.com/stretchr/testify/mock")

	m.Match(
		`$x := &$y{}`,
		`$x := $y{}`,
		`$x = &$y{}`,
		`$x = $y{}`,
	).
		Where(
			m["x"].Type.Underlying().Is(`struct{$*_; mock.Mock; $*_}`) ||
				m["x"].Filter(embedsMock),
		).
		Report("Use the mock constructor instead of struct initialization")
}

func embedsMock(ctx *dsl.VarFilterContext) bool {
	// copy from here - https://github.com/quasilyte/go-ruleguard/blob/7968289708448cdc8ea2fa21d0db203a75d93994/analyzer/testdata/src/quasigo/rules.go#L11-L31
	typ := ctx.Type.Underlying()
	asPointer := types.AsPointer(typ)
	if asPointer != nil {
		typ = asPointer.Elem().Underlying()
	}
	asStruct := types.AsStruct(typ)
	if asStruct == nil {
		return false
	}
	mutexType := ctx.GetType(`github.com/stretchr/testify/mock.Mock`)
	i := 0
	for i < asStruct.NumFields() {
		field := asStruct.Field(i)
		if field.Embedded() && types.Identical(field.Type(), mutexType) {
			return true
		}
		i++
	}
	return false
}

Before using this rule: ~7m
After this rule: ~14m

Are there any way to improve it?

@quasilyte
Copy link
Owner

I have a few questions:

  1. What method do you use to execute the rules? Are you running them under the golangci-lint?
  2. Can you collect the cpu+mem profiles while running the linter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants