Skip to content

chore: scripts: make go imports at least 2X faster#11695

Merged
Stebalien merged 1 commit intomasterfrom
masih/fix-go-imports-faster
Mar 14, 2024
Merged

chore: scripts: make go imports at least 2X faster#11695
Stebalien merged 1 commit intomasterfrom
masih/fix-go-imports-faster

Conversation

@masih
Copy link
Copy Markdown
Member

@masih masih commented Mar 7, 2024

Related Issues

None.

Proposed Changes

fiximports is run up to four times across various make targets. The previous implementation used various commands to find go files, exclude irrelevant ones, etc. all in bash. It was taking ~25 seconds every time on my machine.

The changes here re-implement the bash script in pure go, with 1:1 functionality. The work here uses native go implementation to sort and adjust imports, and uses goimports under the hood to format imports just like the bash script. Compared to the previous bash version, this is at least 2X faster, taking ~10 seconds to run on my machine.

Additional Info

The convention this repository follows is to group filecoin-project imports across 2 additional separate groups. The purpose of fixing imports is to indeed have a consistent formatting across the files. However, it is unclear why the choice of grouping is very particular. Relaxing the grouping to simply SDK and non-SDK groups can reduce the time it takes to fix them to ~5 seconds. Additionally, this means a lot of the generated files would not need import reformatting at all.

I think we should consider a simpler more straightforward import formatting. To be discussed with wider dev team.

Checklist

Before you mark the PR ready for review, please make sure that:

  • Commits have a clear commit message.
  • PR title is in the form of of <PR type>: <area>: <change being made>
    • example: fix: mempool: Introduce a cache for valid signatures
    • PR type: fix, feat, build, chore, ci, docs, perf, refactor, revert, style, test
    • area, e.g. api, chain, state, market, mempool, multisig, networking, paych, proving, sealing, wallet, deps
  • If the PR affects users (e.g., new feature, bug fix, system requirements change), update the CHANGELOG.md and add details to the UNRELEASED section.
  • New features have usage guidelines and / or documentation updates in
  • Tests exist for new functionality or change in behavior
  • CI is green

@masih masih changed the title Fix go imports at least 2x faster chore: scripts: Fix go imports at least 2X faster Mar 7, 2024
@masih masih force-pushed the masih/fix-go-imports-faster branch from 726e96a to f6b6c73 Compare March 7, 2024 19:59
@masih masih marked this pull request as ready for review March 7, 2024 20:24
@snadrus
Copy link
Copy Markdown
Collaborator

snadrus commented Mar 7, 2024

Yes I think we should go with the standard approach for imports. We don't need to be doing anything real fancy.

Copy link
Copy Markdown
Collaborator

@snadrus snadrus left a comment

Choose a reason for hiding this comment

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

Consider the simpler goimports default layout.

@rvagg
Copy link
Copy Markdown
Member

rvagg commented Mar 8, 2024

I think we should consider a simpler more straightforward import formatting. To be discussed with wider dev team.

yeah, I agree, I did a quick pass on this script recently to make it slightly faster and less messy by changing the sed invocation, but the whole purpose of it is questionable. @Stebalien did some more investigation and has opinions.

I like consistency, but this script is super annoying and slows down development.

@Stebalien
Copy link
Copy Markdown
Member

Honestly, I'm fine if we just garble them up for generated files as long as the sorting is consistent.

@Stebalien
Copy link
Copy Markdown
Member

(if we only apply the import sorting to generated files, that is)

Comment thread scripts/fiximports/main.go
Copy link
Copy Markdown
Member

@Stebalien Stebalien left a comment

Choose a reason for hiding this comment

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

Only critical change is the non-zero exit code. But it would be nice to make the other changes as well (including @magik6k's point about doing all of this in parallel).

Comment thread scripts/fiximports/main.go
Comment thread scripts/fiximports/main.go Outdated
Comment thread scripts/fiximports/main.go Outdated
Use native go implementation to sort and adjust imports. Compared to the
previous bash version, this is at least 2X faster.
@masih masih force-pushed the masih/fix-go-imports-faster branch from f6b6c73 to ac0d623 Compare March 14, 2024 20:48
@masih masih requested a review from Stebalien March 14, 2024 20:51
Comment on lines +80 to +84
written, err := target.Write(replacement)
if err != nil {
return err
}
return target.Truncate(int64(written))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: I'd generally recommend that you truncate then write. It shouldn't make any difference at the OS level, but it'll reduce the chances of someone observing a garbage version.

(well, actually, it might make a difference with a CoW filesystem...)

But this is getting deep into nit forest.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Interesting; thank you for pointing that out

@Stebalien Stebalien enabled auto-merge (squash) March 14, 2024 21:03
@Stebalien Stebalien merged commit b56af9b into master Mar 14, 2024
@Stebalien Stebalien deleted the masih/fix-go-imports-faster branch March 14, 2024 21:03
@rvagg
Copy link
Copy Markdown
Member

rvagg commented Mar 15, 2024

👌 nice, almost exactly 2x faster

ribasushi pushed a commit to ribasushi/ci-abusing-lotus-fork that referenced this pull request May 11, 2024
Use native go implementation to sort and adjust imports. Compared to the
previous bash version, this is at least 2X faster.
ribasushi pushed a commit to ribasushi/ci-abusing-lotus-fork that referenced this pull request May 16, 2024
Use native go implementation to sort and adjust imports. Compared to the
previous bash version, this is at least 2X faster.
masih added a commit that referenced this pull request Sep 24, 2024
Lotus imports fixing was optimised to run as a go script for a faster
turnaround. The changes here remove the leftover direct calls to
`goimports` such that all imports are now fixed with the same script.
This removes the need for installing `goimports` during CI workflows or
local development environment.

Relates to:
  * #11695
@masih masih changed the title chore: scripts: Fix go imports at least 2X faster chore: scripts: make go imports at least 2X faster Sep 24, 2024
Stebalien pushed a commit that referenced this pull request Sep 24, 2024
Lotus imports fixing was optimised to run as a go script for a faster
turnaround. The changes here remove the leftover direct calls to
`goimports` such that all imports are now fixed with the same script.
This removes the need for installing `goimports` during CI workflows or
local development environment.

Relates to:
  * #11695
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: ☑️ Done (Archive)

Development

Successfully merging this pull request may close these issues.

5 participants