Skip to content

Commit 4651a5d

Browse files
committed
perf(hash): use lookup-table hex encoding in sha256 paths
1 parent 59c6bb0 commit 4651a5d

4 files changed

Lines changed: 25 additions & 15 deletions

File tree

server/src/chunkedDocStore.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Y from "yjs";
2+
import { sha256Hex } from "./hex";
23

34
const CHECKPOINT_FORMAT = "yaos-doc-checkpoint-v2";
45
const JOURNAL_META_FORMAT = "yaos-doc-journal-v1";
@@ -179,13 +180,6 @@ function chunkArray<T>(arr: T[], size: number): T[][] {
179180
return out;
180181
}
181182

182-
async function sha256Hex(bytes: Uint8Array): Promise<string> {
183-
const digest = await crypto.subtle.digest("SHA-256", bytes);
184-
return Array.from(new Uint8Array(digest))
185-
.map((value) => value.toString(16).padStart(2, "0"))
186-
.join("");
187-
}
188-
189183
async function getManyBatched<T>(
190184
storage: StorageLike | TransactionLike,
191185
keys: string[],

server/src/hex.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const BYTE_TO_HEX: string[] = (() => {
2+
const table = new Array<string>(256);
3+
for (let i = 0; i < 256; i++) {
4+
table[i] = i.toString(16).padStart(2, "0");
5+
}
6+
return table;
7+
})();
8+
9+
export function bytesToHex(bytes: Uint8Array): string {
10+
let out = "";
11+
for (let i = 0; i < bytes.length; i++) {
12+
out += BYTE_TO_HEX[bytes[i]!]!;
13+
}
14+
return out;
15+
}
16+
17+
export async function sha256Hex(bytes: Uint8Array): Promise<string> {
18+
const digest = await crypto.subtle.digest("SHA-256", bytes);
19+
return bytesToHex(new Uint8Array(digest));
20+
}

server/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getServerByName } from "partyserver";
22
import * as Y from "yjs";
33
import { mapWithConcurrency } from "./concurrency";
4+
import { sha256Hex } from "./hex";
45
import { ServerConfig, type StoredServerConfig } from "./config";
56
import {
67
blobKey,
@@ -167,10 +168,7 @@ function rejectSocket(
167168

168169
async function hashToken(token: string): Promise<string> {
169170
const bytes = new TextEncoder().encode(token);
170-
const digest = await crypto.subtle.digest("SHA-256", bytes);
171-
return Array.from(new Uint8Array(digest))
172-
.map((value) => value.toString(16).padStart(2, "0"))
173-
.join("");
171+
return sha256Hex(bytes);
174172
}
175173

176174
function supportsBuckets(env: Env): boolean {

src/main.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MarkdownView, Modal, Notice, Plugin, TFile, normalizePath } from "obsidian";
1+
import { MarkdownView, Modal, Notice, Plugin, TFile, arrayBufferToHex, normalizePath } from "obsidian";
22
import {
33
DEFAULT_SETTINGS,
44
VaultSyncSettingTab,
@@ -2661,9 +2661,7 @@ export default class VaultCrdtSyncPlugin extends Plugin {
26612661
private async sha256Hex(text: string): Promise<string> {
26622662
const data = new TextEncoder().encode(text);
26632663
const digest = await crypto.subtle.digest("SHA-256", data);
2664-
return Array.from(new Uint8Array(digest))
2665-
.map((b) => b.toString(16).padStart(2, "0"))
2666-
.join("");
2664+
return arrayBufferToHex(digest);
26672665
}
26682666

26692667
private async exportDiagnostics(): Promise<void> {

0 commit comments

Comments
 (0)