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

Memoization of rule arguments #595

Merged
merged 4 commits into from
Oct 17, 2021
Merged

Memoization of rule arguments #595

merged 4 commits into from
Oct 17, 2021

Conversation

chavacava
Copy link
Collaborator

In the current implementation of rules, the configuration of rules is read and checked each time the rule is applied to a file.

For example in

func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []lint.Failure
checkNumberOfArguments(1, arguments, bannedCharsRuleName)
bannedCharList := r.getBannedCharsList(arguments)
onFailure := func(failure lint.Failure) {
failures = append(failures, failure)
}
w := lintBannedCharsRule{
bannedChars: bannedCharList,
onFailure: onFailure,
}
ast.Walk(w, file.AST)
return failures
}

lines 20 and 21 will be executed for each file with the very same arguments argument, thus the function calls will return always the same result.

This PR modifies the way argument of rules are handled by memoizing them and avoid checking and analyzing them for each file.

rule/line-length-limit.go Outdated Show resolved Hide resolved

// Apply applies the rule to given file.
func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
checkNumberOfArguments(1, arguments, r.Name())
if r.maxComplexity < 1 {
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe == 0 for consistency?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done


// Apply applies the rule to given file.
func (r *ArgumentsLimitRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
checkNumberOfArguments(1, arguments, r.Name())
if r.total < 1 {
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe == 0 for consistency?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

Copy link
Owner

@mgechev mgechev left a comment

Choose a reason for hiding this comment

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

LGTM. Left minor comments. Both > 1 and == 0 sound good. To me == 0 is a bit better, more deterministic.

rule/file-header.go Outdated Show resolved Hide resolved
rule/cyclomatic.go Outdated Show resolved Hide resolved
@chavacava chavacava merged commit 76b8c57 into master Oct 17, 2021
@chavacava chavacava deleted the rule-config-memoization branch October 17, 2021 18:35
@zeripath zeripath mentioned this pull request Oct 17, 2021
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

Successfully merging this pull request may close these issues.

3 participants