Skip to content

Commit

Permalink
[Parser][NFC] Do less work when parsing function types
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tlively committed Apr 19, 2024
1 parent c60fe15 commit b90b104
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ struct NullTypeParserCtx {
BlockTypeT getBlockTypeFromResult(size_t results) { return Ok{}; }

Result<> getBlockTypeFromTypeUse(Index, TypeUseT) { return Ok{}; }

bool skipFunctionBody() { return false; }
};

template<typename Ctx> struct TypeParserCtx {
Expand Down Expand Up @@ -310,6 +312,8 @@ template<typename Ctx> struct TypeParserCtx {
assert(results.size() == 1);
return HeapType(Signature(Type::none, results[0]));
}

bool skipFunctionBody() { return false; }
};

struct NullInstrParserCtx {
Expand Down Expand Up @@ -1198,6 +1202,8 @@ struct ParseModuleTypesCtx : TypeParserCtx<ParseModuleTypesCtx>,
types(types), implicitTypes(implicitTypes),
implicitElemIndices(implicitElemIndices) {}

bool skipFunctionBody() { return true; }

Result<HeapTypeT> getHeapTypeFromIdx(Index idx) {
if (idx >= types.size()) {
return in.err("type index out of bounds");
Expand Down
8 changes: 5 additions & 3 deletions src/parser/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3028,11 +3028,13 @@ template<typename Ctx> MaybeResult<> func(Ctx& ctx) {
CHECK_ERR(l);
localVars = *l;
}
CHECK_ERR(instrs(ctx));
ctx.setSrcLoc(ctx.in.takeAnnotations());
if (!ctx.skipFunctionBody()) {
CHECK_ERR(instrs(ctx));
ctx.setSrcLoc(ctx.in.takeAnnotations());
}
}

if (!ctx.in.takeRParen()) {
if (!ctx.skipFunctionBody() && !ctx.in.takeRParen()) {
return ctx.in.err("expected end of function");
}

Expand Down

0 comments on commit b90b104

Please sign in to comment.