Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions bench/hash_bench_test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2026 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
test "bench Hash for Bytes (small)" (it : @bench.T) {
let data = Bytes::make(16, 0xAB)
it.bench(fn() { it.keep(Hash::hash(data)) })
}

///|
test "bench Hash for Bytes (medium)" (it : @bench.T) {
let data = Bytes::make(256, 0xAB)
it.bench(fn() { it.keep(Hash::hash(data)) })
}

///|
test "bench Hash for Bytes (large)" (it : @bench.T) {
let data = Bytes::make(4096, 0xAB)
it.bench(fn() { it.keep(Hash::hash(data)) })
}

///|
test "bench Hash for BytesView (small)" (it : @bench.T) {
let data = Bytes::make(16, 0xAB)
let view = data.view()
it.bench(fn() { it.keep(Hash::hash(view)) })
}

///|
test "bench Hash for BytesView (medium)" (it : @bench.T) {
let data = Bytes::make(256, 0xAB)
let view = data.view()
it.bench(fn() { it.keep(Hash::hash(view)) })
}

///|
test "bench Hash for BytesView (large)" (it : @bench.T) {
let data = Bytes::make(4096, 0xAB)
let view = data.view()
it.bench(fn() { it.keep(Hash::hash(view)) })
}
65 changes: 32 additions & 33 deletions builtin/hasher.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,22 @@ pub fn Hasher::combine_byte(self : Hasher, value : Byte) -> Unit {
/// }
/// ```
pub fn Hasher::combine_bytes(self : Hasher, value : Bytes) -> Unit {
let (cur, remain) = for cur = 0, remain = value.length(); remain >= 4; {
self.consume4(endian32(value, cur))
continue cur + 4, remain - 4
} nobreak {
(cur, remain)
}
for cur = cur, remain = remain; remain >= 1; {
self.consume1(value[cur])
continue cur + 1, remain - 1
for data = value.view() {
if data is [u32le(x), .. rest] {
self.consume4(x)
continue rest
} else {
if data is [b0, .. data] {
self.consume1(b0)
if data is [b1, .. data] {
self.consume1(b1)
if data is [b2, ..] {
self.consume1(b2)
}
}
}
break
}
}
}

Expand Down Expand Up @@ -462,16 +469,6 @@ fn rotl(x : UInt, r : Int) -> UInt {
(x << r) | (x >> (32 - r))
}

///|
fn endian32(input : Bytes, cur : Int) -> UInt {
input[cur + 0].to_uint() |
(
(input[cur + 1].to_uint() << 8) |
(input[cur + 2].to_uint() << 16) |
(input[cur + 3].to_uint() << 24)
)
}

///|
/// Implements the `Hash` trait for `String` type, providing a method to combine
/// a string's hash value with a hasher's state.
Expand Down Expand Up @@ -651,20 +648,22 @@ pub impl[T : Hash, E : Hash] Hash for Result[T, E] with hash_combine(

///|
pub impl Hash for BytesView with hash_combine(self : BytesView, hasher : Hasher) {
let data = self.bytes()
let (start, rest) = for start = self.start(), rest = self.len(); rest >= 4; {
let result = for i in 0..<=3; result = (0 : UInt) {
continue result | (data.unsafe_get(i + start).to_uint() << (8 * i))
} nobreak {
result
for data = self {
if data is [u32le(x), .. rest] {
hasher.combine_uint(x)
continue rest
} else {
// only 0-3 bytes left
if data is [b0, .. data] {
hasher.combine_byte(b0)
if data is [b1, .. data] {
hasher.combine_byte(b1)
if data is [b2, ..] {
hasher.combine_byte(b2)
}
}
}
break
}
hasher.combine_uint(result)
continue start + 4, rest - 4
} nobreak {
(start, rest)
}
for start = start, rest = rest; rest >= 1; {
hasher.combine_byte(data.unsafe_get(start))
continue start + 1, rest - 1
}
}
Loading