summaryrefslogtreecommitdiff
path: root/hclsyntax/token.go
diff options
context:
space:
mode:
authorLeandro López (inkel) <inkel.ar@gmail.com>2021-10-19 14:47:56 -0300
committerMartin Atkins <mart@degeneration.co.uk>2022-02-16 11:11:03 -0800
commitcd7e99def87bb0006cbcf4a2f9e50f452d4bc184 (patch)
tree1d1ef63d5a01b04792e3b152782601198c820f3e /hclsyntax/token.go
parent3ce13e5ae5f90962342ee26d0c3c6f4daebf8f09 (diff)
hclsyntax: Copy only tok.Range instead of whole object
Doing this reduces the memory used in ~11%, as the following benchstat comparison shows: name old time/op new time/op delta LexConfig-12 9.27µs ± 0% 9.03µs ± 1% -2.55% (p=0.000 n=9+10) name old alloc/op new alloc/op delta LexConfig-12 8.94kB ± 0% 7.98kB ± 0% -10.74% (p=0.000 n=10+10) name old allocs/op new allocs/op delta LexConfig-12 37.0 ± 0% 37.0 ± 0% ~ (all equal) Benchmarks were created using: go test -benchmem -benchtime=200000x -count=10 -bench=.
Diffstat (limited to 'hclsyntax/token.go')
-rw-r--r--hclsyntax/token.go23
1 files changed, 11 insertions, 12 deletions
diff --git a/hclsyntax/token.go b/hclsyntax/token.go
index f4c6c93..4fe26a1 100644
--- a/hclsyntax/token.go
+++ b/hclsyntax/token.go
@@ -191,8 +191,7 @@ 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 := tok.Range
switch tok.Type {
case TokenBitwiseAnd, TokenBitwiseOr, TokenBitwiseXor, TokenBitwiseNot:
@@ -211,7 +210,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++
}
@@ -221,7 +220,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++
@@ -234,7 +233,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 {
@@ -246,7 +245,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)
}
@@ -259,7 +258,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++
@@ -270,7 +269,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++
@@ -281,7 +280,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++
@@ -291,7 +290,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)
@@ -301,14 +300,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,
})
}
}