From 219e668e87b012c0634043ed702534b8be31231f Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Fri, 19 Apr 2024 17:07:43 -0700 Subject: [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. --- src/parser/contexts.h | 6 ++++++ src/parser/parsers.h | 8 +++++--- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/parser/contexts.h b/src/parser/contexts.h index d68d475a0..81537abaf 100644 --- a/src/parser/contexts.h +++ b/src/parser/contexts.h @@ -173,6 +173,8 @@ struct NullTypeParserCtx { BlockTypeT getBlockTypeFromResult(size_t results) { return Ok{}; } Result<> getBlockTypeFromTypeUse(Index, TypeUseT) { return Ok{}; } + + bool skipFunctionBody() { return false; } }; template struct TypeParserCtx { @@ -310,6 +312,8 @@ template struct TypeParserCtx { assert(results.size() == 1); return HeapType(Signature(Type::none, results[0])); } + + bool skipFunctionBody() { return false; } }; struct NullInstrParserCtx { @@ -1198,6 +1202,8 @@ struct ParseModuleTypesCtx : TypeParserCtx, types(types), implicitTypes(implicitTypes), implicitElemIndices(implicitElemIndices) {} + bool skipFunctionBody() { return true; } + Result getHeapTypeFromIdx(Index idx) { if (idx >= types.size()) { return in.err("type index out of bounds"); diff --git a/src/parser/parsers.h b/src/parser/parsers.h index bbb50b664..25078f354 100644 --- a/src/parser/parsers.h +++ b/src/parser/parsers.h @@ -3028,11 +3028,13 @@ template 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"); } -- cgit v1.2.3