Skip to content

Commit 23cf614

Browse files
committed
fix: avoid to use visit wrapper for __visit_globals
If a class is annotated as final, it is impossible to have dynamic dispatch for __visit. So we can call the class's __visit function directly. For string, we can even save the call
1 parent ef01f2b commit 23cf614

File tree

171 files changed

+1232
-2646
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

171 files changed

+1232
-2646
lines changed

src/builtins.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ import {
8585
DecoratorFlags,
8686
Class,
8787
PropertyPrototype,
88-
VariableLikeElement
88+
VariableLikeElement,
89+
Function,
8990
} from "./program";
9091

9192
import {
@@ -10673,6 +10674,17 @@ builtinFunctions.set(BuiltinNames.i32x4_relaxed_dot_i8x16_i7x16_add_s, builtin_i
1067310674

1067410675
// === Internal helpers =======================================================================
1067510676

10677+
function getVisitInstanceName(visitInstance: Function, classReference: Class): string | null {
10678+
if (classReference.hasDecorator(DecoratorFlags.Final)) {
10679+
if (classReference.visitRef != 0) {
10680+
return classReference.visitorFunctionName;
10681+
} else if (classReference.isPointerfree) {
10682+
return null; // no visitor for pointerfree classes
10683+
}
10684+
}
10685+
return visitInstance.internalName;
10686+
}
10687+
1067610688
/** Compiles the `visit_globals` function. */
1067710689
export function compileVisitGlobals(compiler: Compiler): void {
1067810690
let module = compiler.module;
@@ -10695,11 +10707,13 @@ export function compileVisitGlobals(compiler: Compiler): void {
1069510707
!classReference.hasDecorator(DecoratorFlags.Unmanaged) &&
1069610708
global.is(CommonFlags.Compiled)
1069710709
) {
10710+
let visitFunctionName = getVisitInstanceName(visitInstance, classReference);
10711+
if (visitFunctionName == null) continue;
1069810712
if (global.is(CommonFlags.Inlined)) {
1069910713
let value = global.constantIntegerValue;
1070010714
if (i64_low(value) || i64_high(value)) {
1070110715
exprs.push(
10702-
module.call(visitInstance.internalName, [
10716+
module.call(visitFunctionName, [
1070310717
compiler.options.isWasm64
1070410718
? module.i64(i64_low(value), i64_high(value))
1070510719
: module.i32(i64_low(value)),
@@ -10714,7 +10728,7 @@ export function compileVisitGlobals(compiler: Compiler): void {
1071410728
module.global_get(global.internalName, sizeTypeRef),
1071510729
false // internal
1071610730
),
10717-
module.call(visitInstance.internalName, [
10731+
module.call(visitFunctionName, [
1071810732
module.local_get(1, sizeTypeRef), // tempRef != null
1071910733
module.local_get(0, TypeRef.I32) // cookie
1072010734
], TypeRef.None)
@@ -10750,7 +10764,7 @@ function ensureVisitMembersOf(compiler: Compiler, instance: Class): void {
1075010764
let base = instance.base;
1075110765
if (base) {
1075210766
body.push(
10753-
module.call(`${base.internalName}~visit`, [
10767+
module.call(base.visitorFunctionName, [
1075410768
module.local_get(0, sizeTypeRef), // this
1075510769
module.local_get(1, TypeRef.I32) // cookie
1075610770
], TypeRef.None)
@@ -10860,7 +10874,7 @@ export function compileVisitMembers(compiler: Compiler): void {
1086010874
cases[i] = module.return();
1086110875
} else {
1086210876
cases[i] = module.block(null, [
10863-
module.call(`${instance.internalName}~visit`, [
10877+
module.call(instance.visitorFunctionName, [
1086410878
module.local_get(0, sizeTypeRef), // this
1086510879
module.local_get(1, TypeRef.I32) // cookie
1086610880
], TypeRef.None),

src/compiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,8 @@ export class Compiler extends DiagnosticEmitter {
647647
// finalize runtime features
648648
module.removeGlobal(BuiltinNames.rtti_base);
649649
if (this.runtimeFeatures & RuntimeFeatures.Rtti) compileRTTI(this);
650-
if (this.runtimeFeatures & RuntimeFeatures.visitGlobals) compileVisitGlobals(this);
651650
if (this.runtimeFeatures & RuntimeFeatures.visitMembers) compileVisitMembers(this);
651+
if (this.runtimeFeatures & RuntimeFeatures.visitGlobals) compileVisitGlobals(this);
652652

653653
let memoryOffset = i64_align(this.memoryOffset, options.usizeType.byteSize);
654654

src/program.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4910,6 +4910,10 @@ export class Class extends TypedElement {
49104910
}
49114911
return false;
49124912
}
4913+
4914+
get visitorFunctionName(): string {
4915+
return `${this.internalName}~visit`;
4916+
}
49134917
}
49144918

49154919
/** A yet unresolved interface. */

tests/compiler/assignment-chain.debug.wat

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,15 +2333,6 @@
23332333
call $assignment-chain/setter_assignment_chain
23342334
call $assignment-chain/static_setter_assignment_chain
23352335
)
2336-
(func $~lib/rt/__visit_globals (param $0 i32)
2337-
(local $1 i32)
2338-
i32.const 224
2339-
local.get $0
2340-
call $~lib/rt/itcms/__visit
2341-
i32.const 32
2342-
local.get $0
2343-
call $~lib/rt/itcms/__visit
2344-
)
23452336
(func $~lib/arraybuffer/ArrayBufferView~visit (param $0 i32) (param $1 i32)
23462337
(local $2 i32)
23472338
local.get $0
@@ -2385,6 +2376,9 @@
23852376
end
23862377
unreachable
23872378
)
2379+
(func $~lib/rt/__visit_globals (param $0 i32)
2380+
(local $1 i32)
2381+
)
23882382
(func $~start
23892383
call $start:assignment-chain
23902384
)

tests/compiler/assignment-chain.release.wat

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@
4242
(func $~lib/rt/itcms/visitRoots
4343
(local $0 i32)
4444
(local $1 i32)
45-
i32.const 1248
46-
call $~lib/rt/itcms/__visit
47-
i32.const 1056
48-
call $~lib/rt/itcms/__visit
4945
global.get $~lib/rt/itcms/pinSpace
5046
local.tee $1
5147
i32.load offset=4

tests/compiler/bindings/esm.debug.wat

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,41 +2832,6 @@
28322832
i32.const 0
28332833
drop
28342834
)
2835-
(func $~lib/rt/__visit_globals (param $0 i32)
2836-
(local $1 i32)
2837-
global.get $bindings/esm/stringGlobal
2838-
local.tee $1
2839-
if
2840-
local.get $1
2841-
local.get $0
2842-
call $~lib/rt/itcms/__visit
2843-
end
2844-
global.get $bindings/esm/mutableStringGlobal
2845-
local.tee $1
2846-
if
2847-
local.get $1
2848-
local.get $0
2849-
call $~lib/rt/itcms/__visit
2850-
end
2851-
i32.const 528
2852-
local.get $0
2853-
call $~lib/rt/itcms/__visit
2854-
i32.const 224
2855-
local.get $0
2856-
call $~lib/rt/itcms/__visit
2857-
i32.const 944
2858-
local.get $0
2859-
call $~lib/rt/itcms/__visit
2860-
i32.const 336
2861-
local.get $0
2862-
call $~lib/rt/itcms/__visit
2863-
i32.const 1072
2864-
local.get $0
2865-
call $~lib/rt/itcms/__visit
2866-
i32.const 1136
2867-
local.get $0
2868-
call $~lib/rt/itcms/__visit
2869-
)
28702835
(func $~lib/arraybuffer/ArrayBufferView~visit (param $0 i32) (param $1 i32)
28712836
(local $2 i32)
28722837
local.get $0
@@ -3038,6 +3003,9 @@
30383003
end
30393004
unreachable
30403005
)
3006+
(func $~lib/rt/__visit_globals (param $0 i32)
3007+
(local $1 i32)
3008+
)
30413009
(func $~setArgumentsLength (param $0 i32)
30423010
local.get $0
30433011
global.set $~argumentsLength

tests/compiler/bindings/esm.release.wat

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,26 +139,6 @@
139139
(func $~lib/rt/itcms/visitRoots
140140
(local $0 i32)
141141
(local $1 i32)
142-
i32.const 1056
143-
call $~lib/rt/itcms/__visit
144-
global.get $bindings/esm/mutableStringGlobal
145-
local.tee $0
146-
if
147-
local.get $0
148-
call $~lib/rt/itcms/__visit
149-
end
150-
i32.const 1552
151-
call $~lib/rt/itcms/__visit
152-
i32.const 1248
153-
call $~lib/rt/itcms/__visit
154-
i32.const 1968
155-
call $~lib/rt/itcms/__visit
156-
i32.const 1360
157-
call $~lib/rt/itcms/__visit
158-
i32.const 2096
159-
call $~lib/rt/itcms/__visit
160-
i32.const 2160
161-
call $~lib/rt/itcms/__visit
162142
global.get $~lib/rt/itcms/pinSpace
163143
local.tee $1
164144
i32.load offset=4

tests/compiler/bindings/noExportRuntime.debug.wat

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,53 +2415,6 @@
24152415
)
24162416
(func $bindings/noExportRuntime/takesFunction (param $fn i32)
24172417
)
2418-
(func $~lib/rt/__visit_globals (param $0 i32)
2419-
(local $1 i32)
2420-
global.get $bindings/noExportRuntime/isString
2421-
local.tee $1
2422-
if
2423-
local.get $1
2424-
local.get $0
2425-
call $~lib/rt/itcms/__visit
2426-
end
2427-
global.get $bindings/noExportRuntime/isBuffer
2428-
local.tee $1
2429-
if
2430-
local.get $1
2431-
local.get $0
2432-
call $~lib/rt/itcms/__visit
2433-
end
2434-
global.get $bindings/noExportRuntime/isTypedArray
2435-
local.tee $1
2436-
if
2437-
local.get $1
2438-
local.get $0
2439-
call $~lib/rt/itcms/__visit
2440-
end
2441-
global.get $bindings/noExportRuntime/isArrayOfBasic
2442-
local.tee $1
2443-
if
2444-
local.get $1
2445-
local.get $0
2446-
call $~lib/rt/itcms/__visit
2447-
end
2448-
global.get $bindings/noExportRuntime/isArrayOfArray
2449-
local.tee $1
2450-
if
2451-
local.get $1
2452-
local.get $0
2453-
call $~lib/rt/itcms/__visit
2454-
end
2455-
i32.const 368
2456-
local.get $0
2457-
call $~lib/rt/itcms/__visit
2458-
i32.const 64
2459-
local.get $0
2460-
call $~lib/rt/itcms/__visit
2461-
i32.const 176
2462-
local.get $0
2463-
call $~lib/rt/itcms/__visit
2464-
)
24652418
(func $~lib/arraybuffer/ArrayBufferView~visit (param $0 i32) (param $1 i32)
24662419
(local $2 i32)
24672420
local.get $0
@@ -2557,6 +2510,30 @@
25572510
end
25582511
unreachable
25592512
)
2513+
(func $~lib/rt/__visit_globals (param $0 i32)
2514+
(local $1 i32)
2515+
global.get $bindings/noExportRuntime/isTypedArray
2516+
local.tee $1
2517+
if
2518+
local.get $1
2519+
local.get $0
2520+
call $~lib/rt/itcms/__visit
2521+
end
2522+
global.get $bindings/noExportRuntime/isArrayOfBasic
2523+
local.tee $1
2524+
if
2525+
local.get $1
2526+
local.get $0
2527+
call $~lib/rt/itcms/__visit
2528+
end
2529+
global.get $bindings/noExportRuntime/isArrayOfArray
2530+
local.tee $1
2531+
if
2532+
local.get $1
2533+
local.get $0
2534+
call $~lib/rt/itcms/__visit
2535+
end
2536+
)
25602537
(func $~start
25612538
global.get $~started
25622539
if

tests/compiler/bindings/noExportRuntime.release.wat

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,6 @@
7171
(func $~lib/rt/itcms/visitRoots
7272
(local $0 i32)
7373
(local $1 i32)
74-
i32.const 1056
75-
call $~lib/rt/itcms/__visit
76-
global.get $bindings/noExportRuntime/isBuffer
77-
local.tee $0
78-
if
79-
local.get $0
80-
call $~lib/rt/itcms/__visit
81-
end
8274
global.get $bindings/noExportRuntime/isTypedArray
8375
local.tee $0
8476
if
@@ -89,12 +81,6 @@
8981
call $~lib/rt/itcms/__visit
9082
i32.const 1712
9183
call $~lib/rt/itcms/__visit
92-
i32.const 1392
93-
call $~lib/rt/itcms/__visit
94-
i32.const 1088
95-
call $~lib/rt/itcms/__visit
96-
i32.const 1200
97-
call $~lib/rt/itcms/__visit
9884
global.get $~lib/rt/itcms/pinSpace
9985
local.tee $1
10086
i32.load offset=4

tests/compiler/bindings/raw.debug.wat

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,41 +2835,6 @@
28352835
i32.const 0
28362836
drop
28372837
)
2838-
(func $~lib/rt/__visit_globals (param $0 i32)
2839-
(local $1 i32)
2840-
i32.const 528
2841-
local.get $0
2842-
call $~lib/rt/itcms/__visit
2843-
i32.const 224
2844-
local.get $0
2845-
call $~lib/rt/itcms/__visit
2846-
i32.const 944
2847-
local.get $0
2848-
call $~lib/rt/itcms/__visit
2849-
i32.const 336
2850-
local.get $0
2851-
call $~lib/rt/itcms/__visit
2852-
i32.const 1072
2853-
local.get $0
2854-
call $~lib/rt/itcms/__visit
2855-
i32.const 1136
2856-
local.get $0
2857-
call $~lib/rt/itcms/__visit
2858-
global.get $bindings/esm/stringGlobal
2859-
local.tee $1
2860-
if
2861-
local.get $1
2862-
local.get $0
2863-
call $~lib/rt/itcms/__visit
2864-
end
2865-
global.get $bindings/esm/mutableStringGlobal
2866-
local.tee $1
2867-
if
2868-
local.get $1
2869-
local.get $0
2870-
call $~lib/rt/itcms/__visit
2871-
end
2872-
)
28732838
(func $~lib/arraybuffer/ArrayBufferView~visit (param $0 i32) (param $1 i32)
28742839
(local $2 i32)
28752840
local.get $0
@@ -3041,6 +3006,9 @@
30413006
end
30423007
unreachable
30433008
)
3009+
(func $~lib/rt/__visit_globals (param $0 i32)
3010+
(local $1 i32)
3011+
)
30443012
(func $~setArgumentsLength (param $0 i32)
30453013
local.get $0
30463014
global.set $~argumentsLength

0 commit comments

Comments
 (0)