Skip to content

Commit

Permalink
hclsyntax: Copy only tok.Range instead of whole object
Browse files Browse the repository at this point in the history
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=.
  • Loading branch information
inkel authored and apparentlymart committed Feb 16, 2022
1 parent 3ce13e5 commit cd7e99d
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions hclsyntax/token.go
Expand Up @@ -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:
Expand All @@ -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++
}
Expand All @@ -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++
Expand All @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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++
Expand All @@ -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++
Expand All @@ -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++
Expand All @@ -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)
Expand All @@ -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,
})
}
}
Expand Down

0 comments on commit cd7e99d

Please sign in to comment.