mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
refactor(bytes): remove equalsNaive
(#2852)
This commit is contained in:
parent
0ca0641842
commit
e86f7cdbba
@ -1,25 +1,11 @@
|
|||||||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||||
// This module is browser compatible.
|
// This module is browser compatible.
|
||||||
|
|
||||||
/** Check whether binary arrays are equal to each other using 8-bit comparisons.
|
/** Check whether binary arrays are equal to each other.
|
||||||
* @private
|
|
||||||
* @param a first array to check equality
|
* @param a first array to check equality
|
||||||
* @param b second array to check equality
|
* @param b second array to check equality
|
||||||
*/
|
*/
|
||||||
export function equalsNaive(a: Uint8Array, b: Uint8Array): boolean {
|
export function equals(a: Uint8Array, b: Uint8Array): boolean {
|
||||||
if (a.length !== b.length) return false;
|
|
||||||
for (let i = 0; i < b.length; i++) {
|
|
||||||
if (a[i] !== b[i]) return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Check whether binary arrays are equal to each other using 32-bit comparisons.
|
|
||||||
* @private
|
|
||||||
* @param a first array to check equality
|
|
||||||
* @param b second array to check equality
|
|
||||||
*/
|
|
||||||
export function equals32Bit(a: Uint8Array, b: Uint8Array): boolean {
|
|
||||||
if (a.length !== b.length) return false;
|
if (a.length !== b.length) return false;
|
||||||
const len = a.length;
|
const len = a.length;
|
||||||
const compressable = Math.floor(len / 4);
|
const compressable = Math.floor(len / 4);
|
||||||
@ -33,12 +19,3 @@ export function equals32Bit(a: Uint8Array, b: Uint8Array): boolean {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Check whether binary arrays are equal to each other.
|
|
||||||
* @param a first array to check equality
|
|
||||||
* @param b second array to check equality
|
|
||||||
*/
|
|
||||||
export function equals(a: Uint8Array, b: Uint8Array): boolean {
|
|
||||||
if (a.length < 1000) return equalsNaive(a, b);
|
|
||||||
return equals32Bit(a, b);
|
|
||||||
}
|
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
|
||||||
import { equals32Bit, equalsNaive } from "./equals.ts";
|
|
||||||
|
|
||||||
console.log("generating benchmarks...");
|
|
||||||
const testCases: [Uint8Array, Uint8Array][] = [];
|
|
||||||
// CHANGE THESE
|
|
||||||
const len = 10000;
|
|
||||||
const nCases = 10000;
|
|
||||||
for (let i = 0; i < nCases; i++) {
|
|
||||||
const arr1 = crypto.getRandomValues(new Uint8Array(len));
|
|
||||||
const arr2 = crypto.getRandomValues(new Uint8Array(len));
|
|
||||||
const arr3 = arr1.slice(0);
|
|
||||||
arr3[arr3.length - 1] = arr1[arr1.length - 1] ^ 1;
|
|
||||||
testCases.push([arr1, arr1.slice(0)]);
|
|
||||||
testCases.push([arr1, arr2]);
|
|
||||||
testCases.push([arr1, arr3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Deno.bench({
|
|
||||||
name: "bench old equals",
|
|
||||||
fn() {
|
|
||||||
for (const [a, b] of testCases) {
|
|
||||||
equalsNaive(a, b);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
Deno.bench({
|
|
||||||
name: "bench simd equals",
|
|
||||||
fn() {
|
|
||||||
for (const [a, b] of testCases) {
|
|
||||||
equals32Bit(a, b);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
Loading…
Reference in New Issue
Block a user