From: Chris Duncan Date: Thu, 2 Jul 2026 15:40:34 +0000 (-0700) Subject: Refactor bytes-to-hex converter to eliminate function call overhead for big performan... X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=08694358bb58258c972ef62b79ec05aaa69338f1;p=libnemo.git Refactor bytes-to-hex converter to eliminate function call overhead for big performance boost. --- diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 7889cb2..1706e69 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -12,6 +12,7 @@ export const BURN_PUBLIC_KEY = '000000000000000000000000000000000000000000000000 export const DIFFICULTY_RECEIVE = 0xfffffe0000000000n export const DIFFICULTY_SEND = 0xfffffff800000000n export const HARDENED_OFFSET = 0x80000000 +export const HEXCHAR = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'] export const MAX_RAW = 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFFn export const MAX_SUPPLY = 133_248_297_920_938_463_463_374_607_431_768_211_455n export const NONCE_LENGTH = 24 diff --git a/src/lib/convert/bytes.ts b/src/lib/convert/bytes.ts index acb6031..52e2d95 100644 --- a/src/lib/convert/bytes.ts +++ b/src/lib/convert/bytes.ts @@ -1,7 +1,7 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { ALPHABET } from '../constants' +import { ALPHABET, HEXCHAR } from '../constants' const decoder: TextDecoder = new TextDecoder() @@ -76,11 +76,11 @@ export const bytes = Object.freeze({ */ toHex (bytes: ArrayBuffer | Uint8Array): string { if (bytes instanceof ArrayBuffer) bytes = new Uint8Array(bytes) - if (bytes.buffer instanceof ArrayBuffer && bytes.buffer.byteLength === 0) return '' - return [...bytes] - .map(byte => byte.toString(16).padStart(2, '0')) - .join('') - .toUpperCase() + let hex: string = '' + for (const byte of bytes) { + hex += HEXCHAR[byte >> 4] + HEXCHAR[byte & 15] + } + return hex }, /**