Files
scriptc/tests/corpus/closed-virtual-dispatch-modules/main.ts
T
Malte Ubl fc89fec9b9 Dispatch closed virtual calls to their implementations directly
A virtual call whose receivers can only be a few known classes (every
class that may be instantiated below the static class resolves the method
to one of at most four declarations) now records that closed set on the
virtualCall node. The backend compares the loaded vtable slot against
each implementation but the last and calls the match directly, so LLVM
can inline the bodies and keep loop-carried values in registers. Open
subtrees (decorators, dynamic bases, exported library classes, runtime
classes) and larger sets keep the indirect call.
2026-10-16 06:54:44 -07:00

23 lines
660 B
TypeScript

// Closed class hierarchies declared in another module dispatch like local
// ones, and an exported counter stays visible to the importer.
import { Animal, Bird, Dog, calls, chorus } from "./shapes.ts";
class Cat extends Animal {
sound(): string {
return "meow";
}
}
const zoo: Animal[] = [];
for (let i = 0; i < 9; i++) {
if (i % 3 === 0) zoo.push(new Dog(i));
else if (i % 3 === 1) zoo.push(new Bird(i));
else zoo.push(new Cat(i));
}
let legs = 0;
for (let round = 0; round < 4; round++) for (const a of zoo) legs += a.legs();
console.log(chorus(zoo));
console.log("legs", legs, "calls", calls);
console.log(chorus(zoo.slice(3, 6)), calls);