Skip to content

Commit b9e198d

Browse files
committed
Merge PR #111
2 parents 06f9e30 + fcb882b commit b9e198d

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

query.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"hash/fnv"
77
"reflect"
8+
"strconv"
89
)
910

1011
// The return type of the XPath expression.
@@ -1364,34 +1365,40 @@ func getHashCode(n NodeNavigator) uint64 {
13641365
var sb bytes.Buffer
13651366
switch n.NodeType() {
13661367
case AttributeNode, TextNode, CommentNode:
1367-
sb.WriteString(fmt.Sprintf("%s=%s", n.LocalName(), n.Value()))
1368+
sb.WriteString(n.LocalName())
1369+
sb.WriteByte('=')
1370+
sb.WriteString(n.Value())
13681371
// https://github.com/antchfx/htmlquery/issues/25
13691372
d := 1
13701373
for n.MoveToPrevious() {
13711374
d++
13721375
}
1373-
sb.WriteString(fmt.Sprintf("-%d", d))
1376+
sb.WriteByte('-')
1377+
sb.WriteString(strconv.Itoa(d))
13741378
for n.MoveToParent() {
13751379
d = 1
13761380
for n.MoveToPrevious() {
13771381
d++
13781382
}
1379-
sb.WriteString(fmt.Sprintf("-%d", d))
1383+
sb.WriteByte('-')
1384+
sb.WriteString(strconv.Itoa(d))
13801385
}
13811386
case ElementNode:
13821387
sb.WriteString(n.Prefix() + n.LocalName())
13831388
d := 1
13841389
for n.MoveToPrevious() {
13851390
d++
13861391
}
1387-
sb.WriteString(fmt.Sprintf("-%d", d))
1392+
sb.WriteByte('-')
1393+
sb.WriteString(strconv.Itoa(d))
13881394

13891395
for n.MoveToParent() {
13901396
d = 1
13911397
for n.MoveToPrevious() {
13921398
d++
13931399
}
1394-
sb.WriteString(fmt.Sprintf("-%d", d))
1400+
sb.WriteByte('-')
1401+
sb.WriteString(strconv.Itoa(d))
13951402
}
13961403
}
13971404
h := fnv.New64a()

xpath_function_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,36 @@ func Benchmark_ConcatFunc(b *testing.B) {
262262
_ = concatFunc(testQuery("a"), testQuery("b"))(nil, nil)
263263
}
264264
}
265+
266+
func Benchmark_GetHashCode(b *testing.B) {
267+
// Create a more complex test node that will actually go through the full getHashCode paths
268+
doc := createNavigator(book_example)
269+
doc.MoveToRoot()
270+
doc.MoveToChild() // Move to the first element
271+
doc.MoveToChild() // Deeper
272+
// Find a node with attributes
273+
var node NodeNavigator
274+
for {
275+
if doc.NodeType() == AttributeNode || doc.NodeType() == TextNode || doc.NodeType() == CommentNode {
276+
node = doc.Copy()
277+
break
278+
}
279+
if !doc.MoveToNext() {
280+
if !doc.MoveToChild() {
281+
// If we can't find a suitable node, default to using the first element
282+
doc.MoveToRoot()
283+
doc.MoveToChild()
284+
node = doc.Copy()
285+
break
286+
}
287+
}
288+
}
289+
290+
b.Run("getHashCode", func(b *testing.B) {
291+
b.ReportAllocs()
292+
for i := 0; i < b.N; i++ {
293+
n := node.Copy()
294+
_ = getHashCode(n)
295+
}
296+
})
297+
}

0 commit comments

Comments
 (0)