Skip to content

Commit f3f4e64

Browse files
committed
Merge remote-tracking branch 'origin/master' into p516/warm-restore-depth
# Conflicts: # docs/browser.md # docs/node.md # src/jvm.ts # src/snapshot.ts # test/warm-jit.test.ts
2 parents 901ccc9 + 359f44d commit f3f4e64

9 files changed

Lines changed: 127 additions & 100 deletions

File tree

‎docs/browser.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,11 @@ With `warmSnapshot: true`, `cheerpjRunLibrary` also saves its VM after a call
160160
returns once its calls have loaded at least 200 classes since boot (one javac
161161
compile loads over a thousand) and no library handles are live. Later library
162162
starts with the same class path restore that image, after checking that the
163-
files, missing paths and directory listings those calls used are unchanged; the
164-
first javac compile after it took 2.7 s instead of 5.3 s in Chromium on a
165-
Raspberry Pi 5. An image is about 21 MB; it holds the JS the JIT generated for the methods compiled so far, and a restored method compiles that code on its first call. IndexedDB keeps the 2 most recent warm images separately
163+
files, missing paths and directory listings those calls used are unchanged. In
164+
Chromium, the first javac compile after it takes about half the time of one
165+
after a boot snapshot. An image is about 21 MB; it holds the JS the JIT
166+
generated for the methods compiled so far, and a restored method compiles that
167+
code on its first call. IndexedDB keeps the 2 most recent warm images separately
166168
from boot snapshots. With `debug: true`, the console says whether a VM was
167169
saved, skipped as busy (with the reason) or restored, and why a restore missed.
168170

‎docs/node.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ returns once its calls have loaded at least 200 classes since boot (one javac
7474
compile loads over a thousand) and no library handles are live. A later library
7575
start with the same class path and working directory restores it, after
7676
checking that the files, missing paths and directory listings those calls used
77-
are unchanged; a javac compile from it takes about 1 s instead of 6 s on a
78-
Raspberry Pi 5. An image is about 21 MB, as it also holds the JS the JIT generated for the
79-
methods compiled so far; a restored method compiles that code on its first
80-
call instead of generating it again. Warm snapshots share the snapshot
77+
are unchanged. A first javac compile from it takes about a sixth of the time
78+
of one after a boot snapshot. An image is about 21 MB, as it also holds the JS
79+
the JIT generated for the methods compiled so far; a restored method compiles
80+
that code on its first call instead of generating it again. Warm snapshots share the snapshot
8181
directory and keep the 2 most recent. They build on boot snapshots, so with
8282
`snapshotDir: null` there are none.
8383

‎src/classfile.ts‎

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ export interface CpEntry {
1616
b: number;
1717
}
1818

19+
// Utf8 and numeric values live in values. Other entries keep a and b as two
20+
// u16 in one word of ab.
21+
export interface ConstantPool {
22+
length: number;
23+
values: (string | number | bigint | undefined)[];
24+
tags: Uint8Array;
25+
ab: Uint32Array;
26+
}
27+
1928
export interface ExceptionEntry {
2029
start: number;
2130
end: number;
@@ -52,7 +61,7 @@ export interface ClassFile {
5261
renamedFrom?: string;
5362
minor: number;
5463
major: number;
55-
cp: (CpEntry | string | undefined)[];
64+
cp: ConstantPool;
5665
access: number;
5766
name: string;
5867
superName: string | null;
@@ -181,22 +190,27 @@ function readCode(raw: Uint8Array, cp: ClassFile["cp"]): Code {
181190
return { maxStack, maxLocals, code, handlers, lines, localVars };
182191
}
183192

184-
// UTF-8 entries are stored as strings to avoid an object per constant.
185193
export function cpEntry(cp: ClassFile["cp"], i: number): CpEntry | undefined {
186-
const e = cp[i];
187-
return typeof e === "string" ? { tag: Tag.Utf8, value: e, a: 0, b: 0 } : e;
194+
const tag = cp.tags[i];
195+
if (!tag) return undefined;
196+
const value = cp.values[i];
197+
return { tag, value, a: cp.ab[i] >>> 16, b: cp.ab[i] & 0xffff };
188198
}
189199

200+
export function cpTag(cp: ClassFile["cp"], i: number): number { return cp.tags[i]; }
201+
export function cpA(cp: ClassFile["cp"], i: number): number { return cp.ab[i] >>> 16; }
202+
export function cpB(cp: ClassFile["cp"], i: number): number { return cp.ab[i] & 0xffff; }
203+
export function cpValue(cp: ClassFile["cp"], i: number): number | bigint | undefined { return cp.values[i] as number | bigint | undefined; }
204+
190205
export function utf8(cp: ClassFile["cp"], i: number): string {
191-
const e = cp[i];
192-
if (typeof e !== "string") throw new ClassFormatError(`bad utf8 index ${i}`);
193-
return e;
206+
const s = cp.values[i];
207+
if (typeof s !== "string") throw new ClassFormatError(`bad utf8 index ${i}`);
208+
return s;
194209
}
195210

196211
export function className(cp: ClassFile["cp"], i: number): string {
197-
const e = cpEntry(cp, i);
198-
if (!e || e.tag !== Tag.Class) throw new ClassFormatError(`bad class index ${i}`);
199-
return utf8(cp, e.a);
212+
if (cp.tags[i] !== Tag.Class) throw new ClassFormatError(`bad class index ${i}`);
213+
return utf8(cp, cp.ab[i] >>> 16);
200214
}
201215

202216
export function parseClass(bytes: Uint8Array, preview = false): ClassFile {
@@ -221,7 +235,12 @@ function parseBody(r: Reader, preview: boolean): ClassFile {
221235
if (major < 45 || major > MAX_MAJOR) throw new UnsupportedVersion(major, minor);
222236
if (major >= 56 && minor !== 0 && !(minor === 0xffff && major === MAX_MAJOR && preview)) throw new UnsupportedVersion(major, minor);
223237
const count = r.u2();
224-
const cp: ClassFile["cp"] = new Array(count);
238+
const cp: ClassFile["cp"] = {
239+
length: count,
240+
values: new Array(count),
241+
tags: new Uint8Array(count),
242+
ab: new Uint32Array(count),
243+
};
225244
for (let i = 1; i < count; i++) {
226245
const tag = r.u1();
227246
let value: CpEntry["value"], a = 0, b = 0;
@@ -239,7 +258,9 @@ function parseBody(r: Reader, preview: boolean): ClassFile {
239258
a = r.u2(); b = r.u2(); break;
240259
default: throw new ClassFormatError(`bad constant pool tag ${tag} at ${i}`);
241260
}
242-
cp[i] = tag === Tag.Utf8 ? value as string : { tag, value, a, b };
261+
cp.tags[i] = tag;
262+
if (value !== undefined) cp.values[i] = value;
263+
cp.ab[i] = a << 16 | b;
243264
if (tag === Tag.Long || tag === Tag.Double) i++;
244265
}
245266
const access = r.u2();

‎src/indy.ts‎

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// the same thing the JDK bootstraps do with the ClassFile API, minus the
66
// cost of running that machinery in the interpreter.
77

8-
import { cpEntry, Tag, className, utf8 } from "./classfile.ts";
8+
import { cpA, cpB, cpTag, cpValue, Tag, className, utf8 } from "./classfile.ts";
99
import { ClassWriter } from "./classwriter.ts";
1010
import type { Code } from "./classwriter.ts";
1111
import {
@@ -42,22 +42,22 @@ export type BsmArg =
4242

4343
export function cpArg(cls: JClass, idx: number): BsmArg {
4444
const cp = cls.cf!.cp;
45-
const e = cpEntry(cp, idx)!;
46-
switch (e.tag) {
47-
case Tag.Integer: case Tag.Float: case Tag.Long: case Tag.Double: return { kind: "value", tag: e.tag, value: e.value };
48-
case Tag.String: return { kind: "value", tag: e.tag, value: utf8(cp, e.a) };
45+
const tag = cpTag(cp, idx), a = cpA(cp, idx), b = cpB(cp, idx);
46+
switch (tag) {
47+
case Tag.Integer: case Tag.Float: case Tag.Long: case Tag.Double: return { kind: "value", tag, value: cpValue(cp, idx) };
48+
case Tag.String: return { kind: "value", tag, value: utf8(cp, a) };
4949
case Tag.Class: return { kind: "class", name: className(cp, idx) };
50-
case Tag.MethodType: return { kind: "mt", desc: utf8(cp, e.a) };
50+
case Tag.MethodType: return { kind: "mt", desc: utf8(cp, a) };
5151
case Tag.MethodHandle: {
52-
const ref = cpEntry(cp, e.b)!, nat = cpEntry(cp, ref.b)!;
52+
const ref = b, nat = cpB(cp, ref);
5353
return {
54-
kind: "mh", refKind: e.a, owner: className(cp, ref.a), name: utf8(cp, nat.a), desc: utf8(cp, nat.b),
55-
iface: ref.tag === Tag.InterfaceMethodref,
54+
kind: "mh", refKind: a, owner: className(cp, cpA(cp, ref)), name: utf8(cp, cpA(cp, nat)), desc: utf8(cp, cpB(cp, nat)),
55+
iface: cpTag(cp, ref) === Tag.InterfaceMethodref,
5656
};
5757
}
5858
case Tag.Dynamic: return { kind: "condy", index: idx };
5959
}
60-
throw new PendingThrow("java/lang/InternalError", `bad bootstrap argument tag ${e.tag}`);
60+
throw new PendingThrow("java/lang/InternalError", `bad bootstrap argument tag ${tag}`);
6161
}
6262

6363
function wordSize(descs: string[]) {
@@ -434,15 +434,14 @@ function* methodHandleConstant(vm: JVM, t: JThread, cls: JClass, mh: MethodHandl
434434

435435
function* dynamicConstant(vm: JVM, t: JThread, cls: JClass, idx: number): NativeGen {
436436
const cp = cls.cf!.cp;
437-
const e = cpEntry(cp, idx)!;
438-
const bsm = cls.cf!.bootstrap[e.a];
439-
const nat = cpEntry(cp, e.b)!;
437+
const bsm = cls.cf!.bootstrap[cpA(cp, idx)];
438+
const nat = cpB(cp, idx);
440439
const bsmHandle = yield* constantObject(vm, t, cls, bsm.ref);
441-
const type: JClass = yield* typeOf(t, cls, utf8(cp, nat.b));
440+
const type: JClass = yield* typeOf(t, cls, utf8(cp, cpB(cp, nat)));
442441
const args = yield* staticArgs(vm, t, cls, bsm.args);
443442
return yield* upcall(vm, t,
444443
"linkDynamicConstant(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
445-
vm.mirror(cls), bsmHandle, vm.intern(utf8(cp, nat.a)), vm.mirror(type), args);
444+
vm.mirror(cls), bsmHandle, vm.intern(utf8(cp, cpA(cp, nat))), vm.mirror(type), args);
446445
}
447446

448447
function* staticArgs(vm: JVM, t: JThread, cls: JClass, idxs: number[]): NativeGen {
@@ -455,70 +454,68 @@ function* staticArgs(vm: JVM, t: JThread, cls: JClass, idxs: number[]): NativeGe
455454
// The Java object for a loadable constant, boxing primitives.
456455
export function* constantObject(vm: JVM, t: JThread, cls: JClass, idx: number): NativeGen {
457456
const cp = cls.cf!.cp;
458-
const e = cpEntry(cp, idx)!;
459-
switch (e.tag) {
460-
case Tag.Integer: return yield* box(t, "I", e.value);
461-
case Tag.Float: return yield* box(t, "F", e.value);
462-
case Tag.Long: return yield* box(t, "J", e.value);
463-
case Tag.Double: return yield* box(t, "D", e.value);
464-
case Tag.String: return vm.intern(utf8(cp, e.a));
457+
const tag = cpTag(cp, idx), a = cpA(cp, idx), b = cpB(cp, idx), value = cpValue(cp, idx);
458+
switch (tag) {
459+
case Tag.Integer: return yield* box(t, "I", value);
460+
case Tag.Float: return yield* box(t, "F", value);
461+
case Tag.Long: return yield* box(t, "J", value);
462+
case Tag.Double: return yield* box(t, "D", value);
463+
case Tag.String: return vm.intern(utf8(cp, a));
465464
case Tag.Class: return vm.mirror(yield* vm.loadClassGen(t, cls.loader, className(cp, idx)));
466465
}
467466
const cached = cls.ldc[idx];
468467
if (cached !== undefined) {
469-
if (e.tag !== Tag.Dynamic) return cached;
470-
const desc = utf8(cp, cpEntry(cp, e.b)!.b);
468+
if (tag !== Tag.Dynamic) return cached;
469+
const desc = utf8(cp, cpB(cp, b));
471470
return isPrim(desc) ? yield* box(t, desc, cached) : cached;
472471
}
473472
let v: any;
474-
if (e.tag === Tag.MethodType) v = yield* methodType(vm, t, cls.loader, utf8(cp, e.a));
475-
else if (e.tag === Tag.MethodHandle) v = yield* methodHandleConstant(vm, t, cls, cpArg(cls, idx) as MethodHandleRef);
476-
else if (e.tag === Tag.Dynamic) {
473+
if (tag === Tag.MethodType) v = yield* methodType(vm, t, cls.loader, utf8(cp, a));
474+
else if (tag === Tag.MethodHandle) v = yield* methodHandleConstant(vm, t, cls, cpArg(cls, idx) as MethodHandleRef);
475+
else if (tag === Tag.Dynamic) {
477476
v = yield* dynamicConstant(vm, t, cls, idx);
478-
const desc = utf8(cp, cpEntry(cp, e.b)!.b);
477+
const desc = utf8(cp, cpB(cp, b));
479478
cls.ldc[idx] = isPrim(desc) && v !== null ? vm.getField(v, v.cls.name, "value") : v;
480479
return v;
481480
}
482-
else throw new PendingThrow("java/lang/InternalError", `bad constant tag ${e.tag}`);
481+
else throw new PendingThrow("java/lang/InternalError", `bad constant tag ${tag}`);
483482
cls.ldc[idx] = v;
484483
return v;
485484
}
486485

487486
// ldc of a MethodType, MethodHandle or dynamic constant.
488487
export function* resolveConstant(vm: JVM, t: JThread, cls: JClass, idx: number): NativeGen {
489488
const cp = cls.cf!.cp;
490-
const e = cpEntry(cp, idx)!;
489+
const tag = cpTag(cp, idx), b = cpB(cp, idx);
491490
const v = yield* constantObject(vm, t, cls, idx);
492-
if (e.tag === Tag.Dynamic) {
493-
const desc = utf8(cp, cpEntry(cp, e.b)!.b);
491+
if (tag === Tag.Dynamic) {
492+
const desc = utf8(cp, cpB(cp, b));
494493
if (isPrim(desc)) return v === null ? null : vm.getField(v, v.cls.name, "value");
495494
}
496495
return v;
497496
}
498497

499498
function* linkGeneral(vm: JVM, t: JThread, cls: JClass, idx: number): NativeGen {
500499
const cp = cls.cf!.cp;
501-
const e = cpEntry(cp, idx)!;
502-
const bsm = cls.cf!.bootstrap[e.a];
503-
const nat = cpEntry(cp, e.b)!;
500+
const bsm = cls.cf!.bootstrap[cpA(cp, idx)];
501+
const nat = cpB(cp, idx);
504502
const bsmHandle = yield* constantObject(vm, t, cls, bsm.ref);
505-
const type = yield* methodType(vm, t, cls.loader, utf8(cp, nat.b));
503+
const type = yield* methodType(vm, t, cls.loader, utf8(cp, cpB(cp, nat)));
506504
const args = yield* staticArgs(vm, t, cls, bsm.args);
507505
const appendix = vm.newObjectArray("java/lang/Object", [null]);
508506
const mn: JObject = yield* upcall(vm, t,
509507
"linkCallSite(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/invoke/MemberName;",
510-
vm.mirror(cls), bsmHandle, vm.intern(utf8(cp, nat.a)), type, args, appendix);
508+
vm.mirror(cls), bsmHandle, vm.intern(utf8(cp, cpA(cp, nat))), type, args, appendix);
511509
return { m: memberTarget(t, mn), appendix: appendix.a[0] as JObject | null };
512510
}
513511

514512
// ---- linking ---------------------------------------------------------------
515513

516514
export function* linkCallSite(vm: JVM, t: JThread, cls: JClass, idx: number): Generator<any, CallSite, any> {
517515
const cp = cls.cf!.cp;
518-
const e = cpEntry(cp, idx)!;
519-
const bsm = cls.cf!.bootstrap[e.a];
520-
const nat = cpEntry(cp, e.b)!;
521-
const name = utf8(cp, nat.a), desc = utf8(cp, nat.b);
516+
const bsm = cls.cf!.bootstrap[cpA(cp, idx)];
517+
const nat = cpB(cp, idx);
518+
const name = utf8(cp, cpA(cp, nat)), desc = utf8(cp, cpB(cp, nat));
522519
const ref = cpArg(cls, bsm.ref) as MethodHandleRef;
523520
const args = bsm.args.map((a) => cpArg(cls, a));
524521
let m: JMethod | null = null;

‎src/jit.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// caller spill too, and later delivers its result through the frame like an
1515
// interpreted call.
1616

17-
import { cpEntry, Tag, utf8 } from "./classfile.ts";
17+
import { cpB, cpTag, cpValue, Tag, utf8 } from "./classfile.ts";
1818
import {
1919
ACC_FINAL, INITIALIZED, JArray, JClass, JField, JMethod, JObject, isWide, longToFloat, newArrayData, parseMethodDesc,
2020
} from "./runtime.ts";
@@ -725,9 +725,9 @@ function insn(g: Gen, b: Body, pc: number) {
725725
case 0x11: emit(`${S(d)}=${s16(c, pc + 1)};`); break;
726726
case 0x12: case 0x13: case 0x14: {
727727
const i = op === 0x12 ? c[pc + 1] : idx2;
728-
const e = cpEntry(cp, i)!;
729-
if (e.tag === Tag.Integer || e.tag === Tag.Float || e.tag === Tag.Double) emit(`${S(d)}=${numLit(e.value as number)};`);
730-
else if (e.tag === Tag.Long) emit(`${S(d)}=${e.value}n;`);
728+
const tag = cpTag(cp, i), value = cpValue(cp, i);
729+
if (tag === Tag.Integer || tag === Tag.Float || tag === Tag.Double) emit(`${S(d)}=${numLit(value as number)};`);
730+
else if (tag === Tag.Long) emit(`${S(d)}=${value}n;`);
731731
else {
732732
const v = cls.ldc[i];
733733
if (v === undefined) { emit(slow(pc, d)); break; }
@@ -983,7 +983,7 @@ function fuseConcat(g: Gen, b: Body, pc: number): { pieces: string[]; end: numbe
983983
else if (op === 0x12 || op === 0x13) {
984984
const i = op === 0x12 ? c[p + 1] : u16(c, p + 1);
985985
const v = cls.ldc[i];
986-
if (cpEntry(cls.cf!.cp, i)!.tag !== Tag.String || v === undefined) return null;
986+
if (cpTag(cls.cf!.cp, i) !== Tag.String || v === undefined) return null;
987987
pieces.push(() => g.K(v));
988988
p += op === 0x12 ? 2 : 3;
989989
} else return null;
@@ -1266,13 +1266,12 @@ function stackDelta(cls: JClass, c: Uint8Array, pc: number): number {
12661266
const op = c[pc];
12671267
const cp = cls.cf!.cp;
12681268
const fieldSize = (idx: number) => {
1269-
const nat = cpEntry(cp, cpEntry(cp, idx)!.b)!;
1270-
return isWide(utf8(cp, nat.b)) ? 2 : 1;
1269+
const nat = cpB(cp, idx);
1270+
return isWide(utf8(cp, cpB(cp, nat))) ? 2 : 1;
12711271
};
12721272
const methodSlots = (idx: number, withReceiver: boolean) => {
1273-
const e = cpEntry(cp, idx)!;
1274-
const nat = cpEntry(cp, e.b)!;
1275-
const { params, ret } = parseMethodDesc(utf8(cp, nat.b));
1273+
const nat = cpB(cp, idx);
1274+
const { params, ret } = parseMethodDesc(utf8(cp, cpB(cp, nat)));
12761275
let n = withReceiver ? 1 : 0;
12771276
for (const p of params) n += isWide(p) ? 2 : 1;
12781277
return (ret === "V" ? 0 : isWide(ret) ? 2 : 1) - n;

0 commit comments

Comments
 (0)