Skip to content

Commit 129cfbe

Browse files
authored
Merge pull request #1410 from yamadashy/perf/use-brotli-for-cache-compression
perf(server): Switch cache compression from deflate to Brotli
2 parents 9d5b928 + 1bdb18c commit 129cfbe

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

  • website/server/src/domains/pack/utils

website/server/src/domains/pack/utils/cache.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { promisify } from 'node:util';
22
import * as zlib from 'node:zlib';
33
import type { PackOptions } from '../../../types.js';
44

5-
const inflateAsync = promisify(zlib.inflate);
6-
const deflateAsync = promisify(zlib.deflate);
5+
const compressAsync = promisify(zlib.brotliCompress);
6+
const decompressAsync = promisify(zlib.brotliDecompress);
7+
8+
// Balanced speed/ratio for in-memory cache
9+
const BROTLI_QUALITY = 4;
710

811
interface CacheEntry {
912
value: Uint8Array; // Compressed data
@@ -43,7 +46,7 @@ export class RequestCache<T> {
4346

4447
try {
4548
// Decompress and return the data
46-
const decompressedData = await inflateAsync(entry.value);
49+
const decompressedData = await decompressAsync(entry.value);
4750
return JSON.parse(decompressedData.toString('utf8'));
4851
} catch (error) {
4952
console.error('Error decompressing cache entry:', error);
@@ -56,7 +59,12 @@ export class RequestCache<T> {
5659
try {
5760
// Convert data to JSON string and compress
5861
const jsonString = JSON.stringify(value);
59-
const compressedData = await deflateAsync(Buffer.from(jsonString, 'utf8'));
62+
const compressedData = await compressAsync(jsonString, {
63+
params: {
64+
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
65+
[zlib.constants.BROTLI_PARAM_QUALITY]: BROTLI_QUALITY,
66+
},
67+
});
6068

6169
this.cache.set(key, {
6270
value: compressedData,

0 commit comments

Comments
 (0)