File tree Expand file tree Collapse file tree 2 files changed +45
-5
lines changed Expand file tree Collapse file tree 2 files changed +45
-5
lines changed Original file line number Diff line number Diff line change 5
5
"fmt"
6
6
"hash/fnv"
7
7
"reflect"
8
+ "strconv"
8
9
)
9
10
10
11
// The return type of the XPath expression.
@@ -1364,34 +1365,40 @@ func getHashCode(n NodeNavigator) uint64 {
1364
1365
var sb bytes.Buffer
1365
1366
switch n .NodeType () {
1366
1367
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 ())
1368
1371
// https://github.com/antchfx/htmlquery/issues/25
1369
1372
d := 1
1370
1373
for n .MoveToPrevious () {
1371
1374
d ++
1372
1375
}
1373
- sb .WriteString (fmt .Sprintf ("-%d" , d ))
1376
+ sb .WriteByte ('-' )
1377
+ sb .WriteString (strconv .Itoa (d ))
1374
1378
for n .MoveToParent () {
1375
1379
d = 1
1376
1380
for n .MoveToPrevious () {
1377
1381
d ++
1378
1382
}
1379
- sb .WriteString (fmt .Sprintf ("-%d" , d ))
1383
+ sb .WriteByte ('-' )
1384
+ sb .WriteString (strconv .Itoa (d ))
1380
1385
}
1381
1386
case ElementNode :
1382
1387
sb .WriteString (n .Prefix () + n .LocalName ())
1383
1388
d := 1
1384
1389
for n .MoveToPrevious () {
1385
1390
d ++
1386
1391
}
1387
- sb .WriteString (fmt .Sprintf ("-%d" , d ))
1392
+ sb .WriteByte ('-' )
1393
+ sb .WriteString (strconv .Itoa (d ))
1388
1394
1389
1395
for n .MoveToParent () {
1390
1396
d = 1
1391
1397
for n .MoveToPrevious () {
1392
1398
d ++
1393
1399
}
1394
- sb .WriteString (fmt .Sprintf ("-%d" , d ))
1400
+ sb .WriteByte ('-' )
1401
+ sb .WriteString (strconv .Itoa (d ))
1395
1402
}
1396
1403
}
1397
1404
h := fnv .New64a ()
Original file line number Diff line number Diff line change @@ -262,3 +262,36 @@ func Benchmark_ConcatFunc(b *testing.B) {
262
262
_ = concatFunc (testQuery ("a" ), testQuery ("b" ))(nil , nil )
263
263
}
264
264
}
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
+ }
You can’t perform that action at this time.
0 commit comments