Skip to content

Commit 8bb252c

Browse files
Fix performance regressions in msgpackr integration
- Change from top-level imports to lazy require() calls - Eliminates 14ms msgpackr import overhead affecting all benchmarks - Preserves functionality while avoiding startup performance penalty - Trade-off: slight runtime overhead for lazy loading vs major startup improvement Co-Authored-By: Harry Brundage <[email protected]>
1 parent eed6f37 commit 8bb252c

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

bench/msgpackr-integration.benchmark.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import findRoot from "find-root";
22
import fs from "fs";
3-
import { pack, unpack } from "msgpackr";
43
import { LargeRoot } from "../spec/fixtures/LargeRoot";
54
import { TestClassModel } from "../spec/fixtures/TestClassModel";
65
import { BigTestModelSnapshot } from "../spec/fixtures/TestModel";
@@ -9,6 +8,7 @@ import { enableMsgpackrIntegration } from "../src/msgpackr-instantiator";
98
import { benchmarker } from "./benchmark";
109

1110
const largeRootSnapshot = JSON.parse(fs.readFileSync(findRoot(__dirname) + "/spec/fixtures/large-root-snapshot.json", "utf8"));
11+
const { pack, unpack } = require("msgpackr");
1212
const largeRootMsgpack = pack(largeRootSnapshot);
1313

1414
const bigTestModelMsgpack = pack(BigTestModelSnapshot);

src/msgpackr-instantiator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { unpack } from "msgpackr";
21
import type { IClassModelType, IAnyType } from "./types";
32

43
export class MsgpackrInstantiatorBuilder<T extends IClassModelType<Record<string, IAnyType>, any, any>> {
@@ -9,6 +8,7 @@ export class MsgpackrInstantiatorBuilder<T extends IClassModelType<Record<string
98
const enhancedModel = originalModel as any;
109

1110
enhancedModel.createReadOnlyFromMsgpack = function (buffer: Uint8Array) {
11+
const { unpack } = require("msgpackr");
1212
const data = unpack(buffer);
1313
return originalModel.createReadOnly(data);
1414
};

0 commit comments

Comments
 (0)