refactor: move expressionAttributeValueURL check to separate function rather than have it inline#1206
Conversation
a-h
approved these changes
Jul 20, 2025
Owner
|
I just looked at this again, and realised that this creates a new map object every time the function is called, which probably creates an allocation each time, so performance-wise, it is probably a lot worse than doing a few if statements. |
Owner
package main
import (
"testing"
)
func isExpressionAttributeValueURL(elementName, attrName string) bool {
var elementAttributeLookup = map[string]string{
"a": "href",
"form": "action",
"link": "href",
"object": "data",
}
expectedAttribute, exists := elementAttributeLookup[elementName]
if !exists {
return false
}
return attrName == expectedAttribute
}
func isExpressionAttributeValueURL2(elementName, attrName string) bool {
switch elementName {
case "a", "link":
return attrName == "href"
case "form":
return attrName == "action"
case "object":
return attrName == "data"
default:
return false
}
}
var elementAttributeLookup3 = map[string]string{
"a": "href",
"form": "action",
"link": "href",
"object": "data",
}
func isExpressionAttributeValueURL3(elementName, attrName string) bool {
expectedAttribute, exists := elementAttributeLookup3[elementName]
if !exists {
return false
}
return attrName == expectedAttribute
}
func BenchmarkIsExpressionAttributeValueURL(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = isExpressionAttributeValueURL("a", "href")
}
}
func BenchmarkIsExpressionAttributeValueURL2(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = isExpressionAttributeValueURL2("a", "href")
}
}
func BenchmarkIsExpressionAttributeValueURL3(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = isExpressionAttributeValueURL3("a", "href")
}
}So, doesn't create an allocation, but creates a new map each time (fixed by #3), but the 2nd one is much faster. |
Owner
|
I've used version 2 in e2a87c1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
based on #1091
I replaced the inline check in
generator.gotempl/generator/generator.go
Lines 1347 to 1350 in 15199ff
With a separate check function
isExpressionAttributeValueURLthat can be extended to check for any combination of an HTML element type and name of an HTML attributeThe function uses a
map[string]stringto store the element/attribute combinations so it is very easy to add more if needed.