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

terraform_deprecated_index: improve perf for files with many expressions #49

Merged
merged 2 commits into from
Dec 18, 2022

Conversation

bendrucker
Copy link
Member

@bendrucker bendrucker commented Dec 2, 2022

A coworker at @observeinc alerted me to a module where new versions of TFLint were running extremely slowly. Some initial investigation revealed that terraform_deprecated_index was spending a really long time when there were a very large number of expressions to walk in a module. In this case, there were a few large JSON objects being encoded with jsonencode and all the key/value pairs are expressions, though mostly static.

# times out
tflint --only terraform_deprecated_index

Most of that time was going into repeatedly calling runner.GetFile(), which in the example case was being called hundreds of times for the same file. There's no parser instance stored on the runner so each GetFile call meant the file was read from disk and parsed as HCL.

Since WalkExpressions calls GetFiles() anyway, calling it once before walking is a straightforward fix that brings the performance to an acceptable point. In the module in question, previously TFLint ran for multiple minutes on my Mac Studio at 200% CPU before I'd give up after 5m and interrupt it before it printed any output. After, it finishes in ~8s. Caching files after load would probably further boost performance by avoiding multiple rounds of loading from disk and parsing. The memory penalty would probably be worthwhile for the reduced CPU usage.

@bendrucker bendrucker changed the title terraform_deprecated_index: improve perf for files with many expressions terraform_deprecated_index: improve perf for files with many expressions Dec 15, 2022
@bendrucker bendrucker marked this pull request as ready for review December 15, 2022 23:49
Copy link
Member

@wata727 wata727 left a comment

Choose a reason for hiding this comment

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

Makes sense. Thanks!

@wata727
Copy link
Member

wata727 commented Dec 17, 2022

There's no parser instance stored on the runner so each GetFile call meant the file was read from disk and parsed as HCL.

In my understanding, the host process keeps the map of files in the memory, so I think there is no I/O or parsing cost.
https://github.com/terraform-linters/tflint/blob/v0.43.0/plugin/server.go#L64-L67

However, the RPC cost will not be negligible. This fix is perfectly valid.

@bendrucker bendrucker merged commit 002ad8d into main Dec 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants