Skip to content

Commit

Permalink
Simplify hasRequiredAttributes
Browse files Browse the repository at this point in the history
This function takes around 1.5% of the total CPU time on my instance, and most
of it is spent in `mapassign_faststr` to initialize the `map`. This is replaced
with a switch-case construct, that should be both significantly faster as well
as pretty dull in term of memory consumption.
  • Loading branch information
jvoisin committed Dec 7, 2024
1 parent b7b0ccb commit 99b4f0c
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions internal/reader/sanitizer/sanitizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,24 +294,16 @@ func isPixelTracker(tagName string, attributes []html.Attribute) bool {
}

func hasRequiredAttributes(tagName string, attributes []string) bool {
elements := map[string][]string{
"a": {"href"},
"iframe": {"src"},
"img": {"src"},
"source": {"src", "srcset"},
}

if attrs, ok := elements[tagName]; ok {
for _, attribute := range attributes {
if slices.Contains(attrs, attribute) {
return true
}
}

return false
switch tagName {
case "a":
return slices.Contains(attributes, "href")
case "iframe", "img":
return slices.Contains(attributes, "src")
case "source":
return slices.Contains(attributes, "src") || slices.Contains(attributes, "srcset")
default:
return true
}

return true
}

// See https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
Expand Down

0 comments on commit 99b4f0c

Please sign in to comment.