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

Increase performance and reduce allocations in hclsyntax.LexConfig #490

Merged
merged 3 commits into from Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions hclsyntax/public_test.go
Expand Up @@ -2,6 +2,8 @@ package hclsyntax

import (
"testing"

"github.com/hashicorp/hcl/v2"
)

func TestValidIdentifier(t *testing.T) {
Expand Down Expand Up @@ -44,3 +46,19 @@ func TestValidIdentifier(t *testing.T) {
})
}
}

var T Tokens

func BenchmarkLexConfig(b *testing.B) {
src := []byte("module \"once\" {\n source = \"../modules/foo\"\n}\n\nmodule \"twice\" {\n source = \"../modules/foo\"\n}\n")
filename := "testdata/dave/main.tf"
start := hcl.Pos{Line: 1, Column: 1, Byte: 0}

var tokens Tokens

for i := 0; i < b.N; i++ {
tokens, _ = LexConfig(src, filename, start)
}

T = tokens
}
26 changes: 14 additions & 12 deletions hclsyntax/token.go
Expand Up @@ -191,8 +191,10 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics {
toldBadUTF8 := 0

for _, tok := range tokens {
// copy token so it's safe to point to it
tok := tok
tokRange := func() *hcl.Range {
r := tok.Range
return &r
}

switch tok.Type {
case TokenBitwiseAnd, TokenBitwiseOr, TokenBitwiseXor, TokenBitwiseNot:
Expand All @@ -211,7 +213,7 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics {
Severity: hcl.DiagError,
Summary: "Unsupported operator",
Detail: fmt.Sprintf("Bitwise operators are not supported.%s", suggestion),
Subject: &tok.Range,
Subject: tokRange(),
})
toldBitwise++
}
Expand All @@ -221,7 +223,7 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics {
Severity: hcl.DiagError,
Summary: "Unsupported operator",
Detail: "\"**\" is not a supported operator. Exponentiation is not supported as an operator.",
Subject: &tok.Range,
Subject: tokRange(),
})

toldExponent++
Expand All @@ -234,7 +236,7 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics {
Severity: hcl.DiagError,
Summary: "Invalid character",
Detail: "The \"`\" character is not valid. To create a multi-line string, use the \"heredoc\" syntax, like \"<<EOT\".",
Subject: &tok.Range,
Subject: tokRange(),
})
}
if toldBacktick <= 2 {
Expand All @@ -246,7 +248,7 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics {
Severity: hcl.DiagError,
Summary: "Invalid character",
Detail: "Single quotes are not valid. Use double quotes (\") to enclose strings.",
Subject: &tok.Range,
Subject: tokRange(),
}
diags = append(diags, newDiag)
}
Expand All @@ -259,7 +261,7 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics {
Severity: hcl.DiagError,
Summary: "Invalid character",
Detail: "The \";\" character is not valid. Use newlines to separate arguments and blocks, and commas to separate items in collection values.",
Subject: &tok.Range,
Subject: tokRange(),
})

toldSemicolon++
Expand All @@ -270,7 +272,7 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics {
Severity: hcl.DiagError,
Summary: "Invalid character",
Detail: "Tab characters may not be used. The recommended indentation style is two spaces per indent.",
Subject: &tok.Range,
Subject: tokRange(),
})

toldTabs++
Expand All @@ -281,7 +283,7 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics {
Severity: hcl.DiagError,
Summary: "Invalid character encoding",
Detail: "All input files must be UTF-8 encoded. Ensure that UTF-8 encoding is selected in your editor.",
Subject: &tok.Range,
Subject: tokRange(),
})

toldBadUTF8++
Expand All @@ -291,7 +293,7 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics {
Severity: hcl.DiagError,
Summary: "Invalid multi-line string",
Detail: "Quoted strings may not be split over multiple lines. To produce a multi-line string, either use the \\n escape to represent a newline character or use the \"heredoc\" multi-line template syntax.",
Subject: &tok.Range,
Subject: tokRange(),
})
case TokenInvalid:
chars := string(tok.Bytes)
Expand All @@ -301,14 +303,14 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics {
Severity: hcl.DiagError,
Summary: "Invalid character",
Detail: "\"Curly quotes\" are not valid here. These can sometimes be inadvertently introduced when sharing code via documents or discussion forums. It might help to replace the character with a \"straight quote\".",
Subject: &tok.Range,
Subject: tokRange(),
})
default:
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid character",
Detail: "This character is not used within the language.",
Subject: &tok.Range,
Subject: tokRange(),
})
}
}
Expand Down