Skip to content

[Parser][NFC] Solve performance issue by adding maybeLabelidx #6514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/parser/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ template<typename Ctx> Result<typename Ctx::ElemIdxT> elemidx(Ctx&);
template<typename Ctx> Result<typename Ctx::DataIdxT> dataidx(Ctx&);
template<typename Ctx> Result<typename Ctx::LocalIdxT> localidx(Ctx&);
template<typename Ctx>
MaybeResult<typename Ctx::LabelIdxT> maybeLabelidx(Ctx&,
bool inDelegate = false);
template<typename Ctx>
Result<typename Ctx::LabelIdxT> labelidx(Ctx&, bool inDelegate = false);
template<typename Ctx> Result<typename Ctx::TagIdxT> tagidx(Ctx&);
template<typename Ctx> Result<typename Ctx::TypeUseT> typeuse(Ctx&);
Expand Down Expand Up @@ -1969,16 +1972,18 @@ Result<> makeBreakTable(Ctx& ctx,
Index pos,
const std::vector<Annotation>& annotations) {
std::vector<typename Ctx::LabelIdxT> labels;
// Parse at least one label; return an error only if we parse none.
while (true) {
// Parse at least one label; return an error only if we parse none.
auto label = labelidx(ctx);
if (labels.empty()) {
CHECK_ERR(label);
} else if (label.getErr()) {
auto label = maybeLabelidx(ctx);
if (!label) {
break;
}
CHECK_ERR(label);
labels.push_back(*label);
}
if (labels.empty()) {
return ctx.in.err("expected label");
}
auto defaultLabel = labels.back();
labels.pop_back();
return ctx.makeSwitch(pos, annotations, labels, defaultLabel);
Expand Down Expand Up @@ -2701,17 +2706,26 @@ template<typename Ctx> Result<typename Ctx::LocalIdxT> localidx(Ctx& ctx) {
return ctx.in.err("expected local index or identifier");
}

template<typename Ctx>
Result<typename Ctx::LabelIdxT> labelidx(Ctx& ctx, bool inDelegate) {
if (auto idx = maybeLabelidx(ctx, inDelegate)) {
CHECK_ERR(idx);
return *idx;
}
return ctx.in.err("expected label index or identifier");
}

// labelidx ::= x:u32 => x
// | v:id => x (if labels[x] = v)
template<typename Ctx>
Result<typename Ctx::LabelIdxT> labelidx(Ctx& ctx, bool inDelegate) {
MaybeResult<typename Ctx::LabelIdxT> maybeLabelidx(Ctx& ctx, bool inDelegate) {
if (auto x = ctx.in.takeU32()) {
return ctx.getLabelFromIdx(*x, inDelegate);
}
if (auto id = ctx.in.takeID()) {
return ctx.getLabelFromName(*id, inDelegate);
}
return ctx.in.err("expected label index or identifier");
return {};
}

// tagidx ::= x:u32 => x
Expand Down
Loading