Skip to content

Commit 219e668

Browse files
authored
[Parser][NFC] Do less work when parsing function types (#6516)
After the initial parsing pass to find the locations of all the module elements and after the type definitions have been parsed, the next phase of parsing is to visit all of the module elements and parse their types. This phase does not require parsing function bodies, but it previously parsed entire functions anyway for simplicity. To improve performance, skip that useless work.
1 parent c60fe15 commit 219e668

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/parser/contexts.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ struct NullTypeParserCtx {
173173
BlockTypeT getBlockTypeFromResult(size_t results) { return Ok{}; }
174174

175175
Result<> getBlockTypeFromTypeUse(Index, TypeUseT) { return Ok{}; }
176+
177+
bool skipFunctionBody() { return false; }
176178
};
177179

178180
template<typename Ctx> struct TypeParserCtx {
@@ -310,6 +312,8 @@ template<typename Ctx> struct TypeParserCtx {
310312
assert(results.size() == 1);
311313
return HeapType(Signature(Type::none, results[0]));
312314
}
315+
316+
bool skipFunctionBody() { return false; }
313317
};
314318

315319
struct NullInstrParserCtx {
@@ -1198,6 +1202,8 @@ struct ParseModuleTypesCtx : TypeParserCtx<ParseModuleTypesCtx>,
11981202
types(types), implicitTypes(implicitTypes),
11991203
implicitElemIndices(implicitElemIndices) {}
12001204

1205+
bool skipFunctionBody() { return true; }
1206+
12011207
Result<HeapTypeT> getHeapTypeFromIdx(Index idx) {
12021208
if (idx >= types.size()) {
12031209
return in.err("type index out of bounds");

src/parser/parsers.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,11 +3028,13 @@ template<typename Ctx> MaybeResult<> func(Ctx& ctx) {
30283028
CHECK_ERR(l);
30293029
localVars = *l;
30303030
}
3031-
CHECK_ERR(instrs(ctx));
3032-
ctx.setSrcLoc(ctx.in.takeAnnotations());
3031+
if (!ctx.skipFunctionBody()) {
3032+
CHECK_ERR(instrs(ctx));
3033+
ctx.setSrcLoc(ctx.in.takeAnnotations());
3034+
}
30333035
}
30343036

3035-
if (!ctx.in.takeRParen()) {
3037+
if (!ctx.skipFunctionBody() && !ctx.in.takeRParen()) {
30363038
return ctx.in.err("expected end of function");
30373039
}
30383040

0 commit comments

Comments
 (0)