Skip to content

Commit ee18c63

Browse files
authored
perf(fmt): use one-shot crypto hash (#243)
1 parent 330e027 commit ee18c63

2 files changed

Lines changed: 4 additions & 6 deletions

File tree

packages/rstack/src/fmt/cacheIdentity.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createHash } from 'node:crypto';
1+
import { hash } from 'node:crypto';
22
import { isAbsolute } from 'node:path';
33
import stableStringify from 'fast-json-stable-stringify';
44
import { fmtCacheVersion } from './cacheStore.ts';
@@ -12,8 +12,7 @@ type CacheKeyResolver = (filePath: string) => string | undefined;
1212
type OptionsHasher = (options: ResolvedFmtOptions) => string | undefined;
1313
type PluginFingerprints = ReadonlyMap<string, string>;
1414

15-
const sha256 = (content: string | Uint8Array): string =>
16-
createHash('sha256').update(content).digest('hex');
15+
const sha256 = (content: string | Uint8Array): string => hash('sha256', content, 'hex');
1716

1817
/** Identifies formatter behavior shared by all cache entries in this process. */
1918
const cacheNamespace: string = JSON.stringify([fmtCacheVersion, RSTACK_VERSION, PRETTIER_VERSION]);

packages/rstack/src/fmt/worker.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Derived from @prettier/cli, see THIRD_PARTY_NOTICES.md
22

3-
import { createHash } from 'node:crypto';
3+
import { hash } from 'node:crypto';
44
import { readFileSync, writeFileSync } from 'node:fs';
55
import type { FmtCacheEntry } from './cacheStore.ts';
66
import type { FmtFileCache, FmtFileRequest, FmtWorkerResult } from './types.ts';
@@ -11,8 +11,7 @@ interface FormatFileTask {
1111
cache?: FmtFileCache;
1212
}
1313

14-
const hashContent = (content: string | Uint8Array): string =>
15-
createHash('sha256').update(content).digest('hex');
14+
const hashContent = (content: string | Uint8Array): string => hash('sha256', content, 'hex');
1615

1716
/**
1817
* Use synchronous direct I/O inside the dedicated worker to avoid libuv

0 commit comments

Comments
 (0)