diff options
| author | Leandro López (inkel) <inkel.ar@gmail.com> | 2021-10-20 10:14:21 -0300 |
|---|---|---|
| committer | Martin Atkins <mart@degeneration.co.uk> | 2022-02-16 11:11:03 -0800 |
| commit | 895479c36704726eb8840c8d7ac82e3b23c6371c (patch) | |
| tree | 8fdd7329f6dd09eba8afef79b8609da2570fa4b7 /hclsyntax/token.go | |
| parent | cd7e99def87bb0006cbcf4a2f9e50f452d4bc184 (diff) | |
hclsyntax: Allocate copy of tok.Range only when it's needed
In a similar fashion as the parent commit, here instead of always
copying the tok.Range for later use, we define a function to get this
copied value, and thus we only allocate the copy if it's needed,
otherwise don't.
For the benchmark introduced earlier, the reduction in allocations and
memory usage is outstanding:
name old time/op new time/op delta
LexConfig-12 9.05µs ± 1% 7.83µs ± 1% -13.54% (p=0.000 n=10+10)
name old alloc/op new alloc/op delta
LexConfig-12 7.98kB ± 0% 6.06kB ± 0% -24.07% (p=0.000 n=10+10)
name old allocs/op new allocs/op delta
LexConfig-12 37.0 ± 0% 7.0 ± 0% -81.08% (p=0.000 n=10+10)
Benchmarks were created using:
go test -benchmem -benchtime=200000x -count=10 -bench=.
Diffstat (limited to 'hclsyntax/token.go')
| -rw-r--r-- | hclsyntax/token.go | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/hclsyntax/token.go b/hclsyntax/token.go index 4fe26a1..5ef093f 100644 --- a/hclsyntax/token.go +++ b/hclsyntax/token.go @@ -191,7 +191,10 @@ func checkInvalidTokens(tokens Tokens) hcl.Diagnostics { toldBadUTF8 := 0 for _, tok := range tokens { - tokRange := tok.Range + tokRange := func() *hcl.Range { + r := tok.Range + return &r + } switch tok.Type { case TokenBitwiseAnd, TokenBitwiseOr, TokenBitwiseXor, TokenBitwiseNot: @@ -210,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: &tokRange, + Subject: tokRange(), }) toldBitwise++ } @@ -220,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: &tokRange, + Subject: tokRange(), }) toldExponent++ @@ -233,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: &tokRange, + Subject: tokRange(), }) } if toldBacktick <= 2 { @@ -245,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: &tokRange, + Subject: tokRange(), } diags = append(diags, newDiag) } @@ -258,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: &tokRange, + Subject: tokRange(), }) toldSemicolon++ @@ -269,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: &tokRange, + Subject: tokRange(), }) toldTabs++ @@ -280,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: &tokRange, + Subject: tokRange(), }) toldBadUTF8++ @@ -290,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: &tokRange, + Subject: tokRange(), }) case TokenInvalid: chars := string(tok.Bytes) @@ -300,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: &tokRange, + Subject: tokRange(), }) default: diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Invalid character", Detail: "This character is not used within the language.", - Subject: &tokRange, + Subject: tokRange(), }) } } |
