Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verkle: Add tests for verkle bytes helper #3441

Merged
merged 3 commits into from
Jun 10, 2024
Merged
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you do performance testing to show that this is faster? It's not obvious to me why this is any faster than iterating element by element.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the benchmark file is in the above comment: #3441 (comment). Since multiple bytes are being compared at a time, the loop runs less times and handles the remaining comparisons outside of the first loop.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, didn't see this. Thanks for the comparison!

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)
})
})
Loading