Skip to content

Commit

Permalink
verkle: Add tests for verkle bytes helper (#3441)
Browse files Browse the repository at this point in the history
* Add tests for verkle bytes helper

* Optimize matchingBytesLength

---------

Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com>
  • Loading branch information
scorbajio and acolytec3 committed Jun 10, 2024
1 parent e8297a5 commit 3f9be25
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
22 changes: 18 additions & 4 deletions packages/verkle/src/util/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,29 @@
*/
export function matchingBytesLength(bytes1: Uint8Array, bytes2: Uint8Array): number {
let count = 0

// The minimum length of both arrays
const minLength = Math.min(bytes1.length, bytes2.length)

for (let i = 0; i < minLength; i++) {
// Unroll the loop for better performance
for (let i = 0; i < minLength - 3; i += 4) {
// Compare 4 bytes at a time
if (
bytes1[i] === bytes2[i] &&
bytes1[i + 1] === bytes2[i + 1] &&
bytes1[i + 2] === bytes2[i + 2] &&
bytes1[i + 3] === bytes2[i + 3]
) {
count += 4
} else {
// Break early if a mismatch is found
break
}
}

// Handle any remaining elements
for (let i = minLength - (minLength % 4); i < minLength; i++) {
if (bytes1[i] === bytes2[i]) {
count++
} else {
// Stop counting as soon as a mismatch is found
break
}
}
Expand Down
47 changes: 47 additions & 0 deletions packages/verkle/test/util/bytes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { assert, describe, it } from 'vitest'

import { matchingBytesLength } from '../../src/util/bytes.js'

describe('matchingBytesLength', () => {
it('should return 0 when both arrays are empty', () => {
const bytes1 = new Uint8Array([])
const bytes2 = new Uint8Array([])
assert.equal(matchingBytesLength(bytes1, bytes2), 0)
})

it('should return 0 when one of the arrays is empty', () => {
const bytes1 = new Uint8Array([1, 2, 3])
const bytes2 = new Uint8Array([])
assert.equal(matchingBytesLength(bytes1, bytes2), 0)
})

it('should return 0 when arrays have no matching elements', () => {
const bytes1 = new Uint8Array([1, 2, 3])
const bytes2 = new Uint8Array([4, 5, 6])
assert.equal(matchingBytesLength(bytes1, bytes2), 0)
})

it('should handle arrays with same elements but different lengths', () => {
const bytes1 = new Uint8Array([1, 2, 3])
const bytes2 = new Uint8Array([1, 2, 3, 4])
assert.equal(matchingBytesLength(bytes1, bytes2), 3)
})

it('should handle arrays with matching elements at end', () => {
const bytes1 = new Uint8Array([1, 2, 3])
const bytes2 = new Uint8Array([0, 1, 2, 3])
assert.equal(matchingBytesLength(bytes1, bytes2), 0)
})

it('should handle arrays with matching elements at start', () => {
const bytes1 = new Uint8Array([1, 2, 3])
const bytes2 = new Uint8Array([1, 2, 3, 4, 5])
assert.equal(matchingBytesLength(bytes1, bytes2), 3)
})

it('should handle arrays with large number of elements', () => {
const bytes1 = new Uint8Array(Array.from({ length: 1000000 }, (_, i) => i))
const bytes2 = new Uint8Array(Array.from({ length: 1000000 }, (_, i) => i))
assert.equal(matchingBytesLength(bytes1, bytes2), 1000000)
})
})

0 comments on commit 3f9be25

Please sign in to comment.