segmentio / golines

A golang formatter that fixes long lines
MIT License
903 stars 56 forks source link

Extremely slow golines formatting for specific function #106

Open Meelfy opened 1 year ago

Meelfy commented 1 year ago

I've encountered a very peculiar issue with the golines tool when formatting the following function:

func TestInitTestConfig(t *testing.T) {
    assert.Equal(t, LocalConfig.Abase.Cluster, "")
    InitTestConfig()
    assert.Equal(t, LocalConfig.Abase.Cluster, "abase_aurora")
}

Running golines on this file takes an astonishing 6.82 seconds:

%  time golines -d -m 120 -w ./src/settings/local_test.go
[2023-06-06 10:03:25] DEBUG Processing file ./src/settings/local_test.go
[2023-06-06 10:03:28] DEBUG Starting round 0
[2023-06-06 10:03:28] DEBUG Nothing more to shorten or reformat, stopping
[2023-06-06 10:03:31] DEBUG Contents unchanged, skipping write
golines -d -m 120 -w ./src/settings/local_test.go  6.82s user 30.91s system 638% cpu 5.905 total

The complete file is as follows:

package settings

import (
    "testing"

    "github.com/stretchr/testify/assert"
)

func TestInitTestConfig(t *testing.T) {
    assert.Equal(t, LocalConfig.Abase.Cluster, "")
    InitTestConfig()
    assert.Equal(t, LocalConfig.Abase.Cluster, "abase_aurora")
}

func TestInitLocalConfig(t *testing.T) {
    err := InitLocalConfig()
    assert.NoError(t, err)
}

However, when I remove the TestInitTestConfig function, golines only takes 0.01 seconds:

%  time golines -d -m 120 -w ./src/settings/local_test.go
[2023-06-06 10:04:09] DEBUG Processing file ./src/settings/local_test.go
[2023-06-06 10:04:09] DEBUG Starting round 0
[2023-06-06 10:04:09] DEBUG Nothing more to shorten or reformat, stopping
[2023-06-06 10:04:09] DEBUG Contents unchanged, skipping write
golines -d -m 120 -w ./src/settings/local_test.go  0.01s user 0.01s system 86% cpu 0.020 total

The complete file is as follows:

package settings

import (
    "testing"

    "github.com/stretchr/testify/assert"
)

func TestInitLocalConfig(t *testing.T) {
    err := InitLocalConfig()
    assert.NoError(t, err)
}

This significant difference in performance is quite baffling, and I'm not sure what could be causing it. Any insights or suggestions would be greatly appreciated.

telemachus commented 1 year ago

I can't reproduce this here. What versions of go and golines are you using?

# First run
$ time golines -d -m 120 -w .
[2023-06-08 20:16:31] DEBUG Processing file main.go
[2023-06-08 20:16:31] DEBUG Starting round 0
[2023-06-08 20:16:31] DEBUG Nothing more to shorten or reformat, stopping
[2023-06-08 20:16:32] DEBUG Contents changed, writing output to main.go

real    0m0.607s
user    0m1.361s
sys 0m2.490s

# Second run
$ time golines -d -m 120 -w .
[2023-06-08 20:16:47] DEBUG Processing file main.go
[2023-06-08 20:16:48] DEBUG Starting round 0
[2023-06-08 20:16:48] DEBUG Nothing more to shorten or reformat, stopping
[2023-06-08 20:16:48] DEBUG Contents unchanged, skipping write

real    0m0.605s
user    0m1.356s
sys 0m2.452s