Skip to content

Commit b438b86

Browse files
committed
some easy performance
1 parent 58f5937 commit b438b86

File tree

1 file changed

+35
-51
lines changed

1 file changed

+35
-51
lines changed

base/hashing.jl

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -445,50 +445,34 @@ end
445445
# Helper function to concatenate two UInt64 values with a byte shift
446446
# Returns the result of shifting 'low' right by 'shift_bytes' bytes and
447447
# filling the high bits with the low bits of 'high'
448-
@inline function concat_shift(low::UInt64, high::UInt64, shift_bytes::Int)
449-
shift_bits = shift_bytes * 8
450-
return (low >> shift_bits) | (high << (64 - shift_bits))
448+
@inline function concat_shift(low::UInt64, high::UInt64, shift_bytes::UInt8)
449+
shift_bits = (shift_bytes * 0x8) & 0x3f
450+
return (low >> shift_bits) | (high << (0x40 - shift_bits))
451451
end
452452

453-
@eval @inline function read_uint64_from_uint8_iter(iter, state)
453+
@inline function read_uint64_from_uint8_iter(iter, state)
454454
value = zero(UInt64)
455-
bytes_read = 0
456-
shift = 0
457-
458-
for _ in 1:8
455+
@nexprs 8 i -> begin
459456
next_result = iterate(iter, state)
460-
next_result === nothing && break
457+
next_result === nothing && return value, state, UInt8(i - 1)
461458
byte, state = next_result
462-
value |= UInt64(byte) << shift
463-
shift += 8
464-
bytes_read += 1
465-
$(Expr(:loopinfo, (Symbol("llvm.loop.disable_nonforced"))))
466-
$(Expr(:loopinfo, (Symbol("llvm.loop.vectorize.enable"), false)))
459+
value |= UInt64(byte) << ((i - 1) * 8)
467460
end
468-
469-
return value, state, bytes_read
461+
return value, state, 0x8
470462
end
471463

472-
@eval @inline function read_uint64_from_uint8_iter(iter)
464+
@inline function read_uint64_from_uint8_iter(iter)
473465
next_result = iterate(iter)
474466
next_result === nothing && return nothing
475-
476467
byte, state = next_result
477468
value = UInt64(byte)
478-
bytes_read = 1
479-
480-
# Loop for remaining bytes
481-
for i in 2:8
469+
@nexprs 7 i -> begin
482470
next_result = iterate(iter, state)
483-
next_result === nothing && break
471+
next_result === nothing && return value, state, UInt8(i)
484472
byte, state = next_result
485-
value |= UInt64(byte::UInt8) << ((i-1) * 8)
486-
bytes_read += 1
487-
$(Expr(:loopinfo, (Symbol("llvm.loop.disable_nonforced"))))
488-
$(Expr(:loopinfo, (Symbol("llvm.loop.vectorize.enable"), false)))
473+
value |= UInt64(byte::UInt8) << (i * 8)
489474
end
490-
491-
return value, state, bytes_read
475+
return value, state, 0x8
492476
end
493477

494478
@assume_effects :terminates_globally function hash_bytes(
@@ -510,12 +494,12 @@ end
510494
l3 = zero(UInt64)
511495
l4 = zero(UInt64)
512496
l5 = zero(UInt64)
513-
b0 = 0
514-
b1 = 0
515-
b2 = 0
516-
b3 = 0
517-
b4 = 0
518-
b5 = 0
497+
b0 = 0x0
498+
b1 = 0x0
499+
b2 = 0x0
500+
b3 = 0x0
501+
b4 = 0x0
502+
b5 = 0x0
519503
t0 = zero(UInt64)
520504
t1 = zero(UInt64)
521505

@@ -526,18 +510,18 @@ end
526510
# Repeat hashing chunks until a short read
527511
while true
528512
l1, state, b1 = read_uint64_from_uint8_iter(iter, state)
529-
if b1 == 8
513+
if b1 == 0x8
530514
l2, state, b2 = read_uint64_from_uint8_iter(iter, state)
531-
if b2 == 8
515+
if b2 == 0x8
532516
l3, state, b3 = read_uint64_from_uint8_iter(iter, state)
533-
if b3 == 8
517+
if b3 == 0x8
534518
l4, state, b4 = read_uint64_from_uint8_iter(iter, state)
535-
if b4 == 8
519+
if b4 == 0x8
536520
l5, state, b5 = read_uint64_from_uint8_iter(iter, state)
537-
if b5 == 8
521+
if b5 == 0x8
538522
# Read start of next chunk
539523
read = read_uint64_from_uint8_iter(iter, state)
540-
if read[3] == 0
524+
if read[3] == 0x0
541525
# Read exactly 48 bytes
542526
t0 = l4
543527
t1 = l5
@@ -554,7 +538,7 @@ end
554538
b3 = 0
555539
b4 = 0
556540
b5 = 0
557-
if b0 != 8
541+
if b0 < 8
558542
t0 = concat_shift(l4, l5, b0)
559543
t1 = concat_shift(l5, l0, b0)
560544
break
@@ -598,26 +582,26 @@ end
598582
end
599583
buflen += bytes_chunk
600584
if buflen 16
601-
if buflen 4
602-
seed ⊻= buflen
603-
if buflen 8
585+
if bytes_chunk 0x4
586+
seed ⊻= bytes_chunk
587+
if bytes_chunk 0x8
604588
a = l0
605589
b = t1
606590
else
607591
a = UInt64(l0 % UInt32)
608-
b = UInt64((l0 >>> (8 * (bytes_chunk - 4))) % UInt32)
592+
b = UInt64((l0 >>> ((0x8 * (bytes_chunk - 0x4)) % 0x3f)) % UInt32)
609593
end
610-
elseif buflen > 0
594+
elseif bytes_chunk > 0x0
611595
b0 = l0 % UInt8
612-
b1 = (l0 >>> (8 * div(buflen, 2))) % UInt8
613-
b2 = (l0 >>> (8 * (buflen - 1))) % UInt8
596+
b1 = (l0 >>> ((0x8 * div(bytes_chunk, 0x2)) % 0x3f)) % UInt8
597+
b2 = (l0 >>> ((0x8 * (bytes_chunk - 0x1)) % 0x3f)) % UInt8
614598
a = (UInt64(b0) << 45) | UInt64(b2)
615599
b = UInt64(b1)
616600
end
617601
else
618-
if bytes_chunk > 16
602+
if bytes_chunk > 0x10
619603
seed = hash_mix(l0 secret[3], l1 seed)
620-
if bytes_chunk > 32
604+
if bytes_chunk > 0x20
621605
seed = hash_mix(l2 secret[3], l3 seed)
622606
end
623607
end

0 commit comments

Comments
 (0)