1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-09-15 18:57:04 +00:00

perf(xml): eliminate bound checks in filterValidXMLChars

Optimizes the filterValidXMLChars function by changing the loop variable type from int to uint to eliminate bound checks during compilation, resulting in a ~4% performance improvement.

- Changes loop variable i from int to uint to remove compiler-generated bound checks
- Adjusts type conversions accordingly to maintain correctness

```
goos: linux
goarch: arm64
pkg: miniflux.app/v2/internal/reader/parser
        │   old.txt   │              new.txt               │
        │   sec/op    │   sec/op     vs base               │
Parse-8   40.91m ± 3%   39.30m ± 2%  -3.94% (p=0.000 n=50)
```
This commit is contained in:
Julien Voisin 2025-09-09 00:33:55 +02:00 committed by GitHub
parent 5f38054965
commit 3051acf369
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -54,8 +54,10 @@ func NewXMLDecoder(data io.ReadSeeker) *xml.Decoder {
// filterValidXMLChars filters inplace invalid XML characters. // filterValidXMLChars filters inplace invalid XML characters.
// This function is inspired from bytes.Map // This function is inspired from bytes.Map
func filterValidXMLChars(s []byte) []byte { func filterValidXMLChars(s []byte) []byte {
j := 0 var i uint // declaring it as an uint removes a bound check in the loop.
for i := 0; i < len(s); { var j int
for i = 0; i < uint(len(s)); {
wid := 1 wid := 1
r := rune(s[i]) r := rune(s[i])
if r >= utf8.RuneSelf { if r >= utf8.RuneSelf {
@ -67,7 +69,7 @@ func filterValidXMLChars(s []byte) []byte {
j += wid j += wid
} }
} }
i += wid i += uint(wid)
} }
return s[:j] return s[:j]
} }