Skip to content

Commit

Permalink
Merge pull request #206 from fujiwara/fix/performance
Browse files Browse the repository at this point in the history
Fix/performance
  • Loading branch information
fujiwara authored Sep 17, 2024
2 parents de69972 + 485732a commit 4b5e629
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
30 changes: 15 additions & 15 deletions tfstate/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,32 +242,32 @@ func (s *TFState) Lookup(key string) (*Object, error) {
// we must quote them like `.outputs["repository-arn"]`.
//
// quoteJQQuery does it.
func quoteJQQuery(query string) string {
splitRegex := regexp.MustCompile(`[.\[\]]`)
indexRegex := regexp.MustCompile(`^-?[0-9]+$`)
parts := splitRegex.Split(query, -1)
parts_coalesced := make([]string, 0, len(parts))
var (
quoteSplitRegex = regexp.MustCompile(`[.\[\]]`)
quoteIndexRegex = regexp.MustCompile(`^-?[0-9]+$`)
)

for _, part := range parts {
if part != "" {
parts_coalesced = append(parts_coalesced, part)
}
func quoteJQQuery(query string) string {
if query == "" || !strings.Contains(query, "-") {
// short-circuit if query is empty or doesn't contain hyphen
return query
}

parts := quoteSplitRegex.Split(query, -1)
var builder strings.Builder
builder.Grow(len(query) + 5*len(parts))
builder.WriteByte('.')

for _, part := range parts_coalesced {
for _, part := range parts {
if part == "" {
continue
}
builder.WriteByte('[')
if indexRegex.MatchString(part) {
if quoteIndexRegex.MatchString(part) {
builder.WriteString(part)
} else {
if !strings.HasPrefix(part, `"`) {
builder.WriteByte('"')
}

builder.WriteString(part)

if !strings.HasSuffix(part, `"`) {
builder.WriteByte('"')
}
Expand Down
19 changes: 19 additions & 0 deletions tfstate/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,25 @@ func TestLookupFileURL(t *testing.T) {
testLookupState(t, state)
}

func BenchmarkLookupFile(b *testing.B) {
f, err := os.Open("test/terraform.tfstate")
if err != nil {
b.Error(err)
}
state, err := tfstate.Read(context.Background(), f)
if err != nil {
b.Error(err)
}
for i := 0; i < b.N; i++ {
for _, ts := range TestSuitesOK {
_, err := state.Lookup(ts.Key)
if err != nil {
b.Error(err)
}
}
}
}

func TestLookupHTTPURL(t *testing.T) {
h := http.FileServer(http.Dir("."))
ts := httptest.NewServer(h)
Expand Down

0 comments on commit 4b5e629

Please sign in to comment.