Selaa lähdekoodia

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)
```
Julien Voisin 7 kuukautta sitten
vanhempi
commit
3051acf369
1 muutettua tiedostoa jossa 5 lisäystä ja 3 poistoa
  1. 5 3
      internal/reader/xml/decoder.go

+ 5 - 3
internal/reader/xml/decoder.go

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