Skip to content

Commit adc9cc9

Browse files
committed
perf(server): resolve model aliases in one memoized traversal
1 parent a21d788 commit adc9cc9

2 files changed

Lines changed: 84 additions & 22 deletions

File tree

‎apps/server/src/usage/usageAggregation.test.ts‎

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -212,20 +212,71 @@ describe("UsageAggregator", () => {
212212

213213
describe("resolveModelAliases", () => {
214214
it("follows chains to the final model and drops chains that enter a loop", () => {
215-
expect(
216-
resolveModelAliases({
217-
"preview[1m]": "preview",
218-
preview: "example-model",
219-
loop: "back",
220-
back: "loop",
221-
intoLoop: "loop",
222-
self: "self",
223-
}),
224-
).toEqual(
225-
new Map([
226-
["preview[1m]", "example-model"],
227-
["preview", "example-model"],
228-
]),
229-
);
215+
const aliases = Object.freeze({
216+
"preview[1m]": "preview",
217+
unrelated: "other-model",
218+
preview: "example-model",
219+
shared: "preview",
220+
intoLoop: "loop",
221+
loop: "back",
222+
back: "loop",
223+
laterIntoLoop: "back",
224+
deepIntoLoop: "intoLoop",
225+
self: "self",
226+
intoSelf: "self",
227+
validAfterLoops: "shared",
228+
});
229+
230+
expect([...resolveModelAliases(aliases)]).toEqual([
231+
["preview[1m]", "example-model"],
232+
["unrelated", "other-model"],
233+
["preview", "example-model"],
234+
["shared", "example-model"],
235+
["validAfterLoops", "example-model"],
236+
]);
237+
expect([...resolveModelAliases({})]).toEqual([]);
238+
});
239+
240+
it("keeps Object.keys order and follows only own targets, including non-enumerable ones", () => {
241+
const aliases = {
242+
"10": "constructor",
243+
"2": "toString",
244+
first: "hidden",
245+
["__proto__"]: "hasOwnProperty",
246+
constructor: "terminal",
247+
hasOwnProperty: "constructor",
248+
inheritedTarget: "inherited",
249+
};
250+
Object.setPrototypeOf(aliases, { inherited: "must-not-follow" });
251+
Object.defineProperty(aliases, "hidden", { value: "constructor" });
252+
253+
expect([...resolveModelAliases(aliases)]).toEqual([
254+
["2", "toString"],
255+
["10", "terminal"],
256+
["first", "terminal"],
257+
["__proto__", "terminal"],
258+
["constructor", "terminal"],
259+
["hasOwnProperty", "terminal"],
260+
["inheritedTarget", "inherited"],
261+
]);
262+
expect([...resolveModelAliases({ prototypeTarget: "__proto__" })]).toEqual([
263+
["prototypeTarget", "__proto__"],
264+
]);
265+
});
266+
267+
it("preserves empty terminal strings and follows an own empty-string alias", () => {
268+
expect([...resolveModelAliases({ first: "next", next: "", last: "next" })]).toEqual([
269+
["first", ""],
270+
["next", ""],
271+
["last", ""],
272+
]);
273+
expect([...resolveModelAliases({ first: "", "": "terminal", last: "first" })]).toEqual([
274+
["first", "terminal"],
275+
["", "terminal"],
276+
["last", "terminal"],
277+
]);
278+
expect([...resolveModelAliases({ first: "", "": "", valid: "terminal" })]).toEqual([
279+
["valid", "terminal"],
280+
]);
230281
});
231282
});

‎apps/server/src/usage/usageAggregation.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,26 @@ export interface AggregateOptions {
7878
export function resolveModelAliases(
7979
aliases: Readonly<Record<string, string>>,
8080
): ReadonlyMap<string, string> {
81-
const resolved = new Map<string, string>();
82-
for (const model of Object.keys(aliases)) {
83-
const seen = new Set([model]);
84-
let target = aliases[model]!;
85-
while (Object.hasOwn(aliases, target) && !seen.has(target)) {
86-
seen.add(target);
81+
const models = Object.keys(aliases);
82+
const memo = new Map<string, string | null>();
83+
for (const model of models) {
84+
if (memo.has(model)) continue;
85+
const path: string[] = [];
86+
let target = model;
87+
while (Object.hasOwn(aliases, target) && !memo.has(target)) {
88+
path.push(target);
89+
// Null marks both the active walk and paths that end in a cycle.
90+
memo.set(target, null);
8791
target = aliases[target]!;
8892
}
89-
if (!Object.hasOwn(aliases, target)) resolved.set(model, target);
93+
const terminal = Object.hasOwn(aliases, target) ? (memo.get(target) ?? null) : target;
94+
for (const alias of path) memo.set(alias, terminal);
95+
}
96+
97+
const resolved = new Map<string, string>();
98+
for (const model of models) {
99+
const target = memo.get(model);
100+
if (target !== null && target !== undefined) resolved.set(model, target);
90101
}
91102
return resolved;
92103
}

0 commit comments

Comments
 (0)