Skip to content

Commit

Permalink
Allocate copy of tok.Range only when it's needed
Browse files Browse the repository at this point in the history
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=.

Signed-off-by: Leandro López (inkel) <[email protected]>
  • Loading branch information
inkel authored and apparentlymart committed Feb 15, 2022
1 parent 39050fc commit a2faa08
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions hclsyntax/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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++
}
Expand All @@ -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++
Expand All @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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++
Expand All @@ -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++
Expand All @@ -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++
Expand All @@ -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)
Expand All @@ -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(),
})
}
}
Expand Down

0 comments on commit a2faa08

Please sign in to comment.