assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

optimization: Dead Code issue

Open JesseCodeBones opened this issue 3 years ago • 3 comments

During my testing, I found we have some dead code in the project and can we have a discuss how can we handle it properly? I will give an example here: File: src/parser.ts Function: parseNamespace DeadCode Block:

if (tn.skipIdentifier()) {
///...
} else {
  this.error(
    DiagnosticCode.Identifier_expected,
    tn.range()
  );
}

actually, the program already have the identifier check before: located at: parser.parseTopLevelStatement(), with this block

case Token.NAMESPACE: {
  let state = tn.mark();
  tn.next();
  if (tn.peek(false, IdentifierHandling.PREFER) == Token.IDENTIFIER) {
    tn.discard(state);
    statement = this.parseNamespace(tn, flags, decorators, startPos);
    decorators = null;
  } else {
    tn.reset(state);
    statement = this.parseStatement(tn, true);
  }
  break;
}

so, what should be the suggestion for this kind of situation? should we remove the duplicated check from function parseNamespace?

JesseCodeBones avatar May 31 '22 07:05 JesseCodeBones

Looks like, if parseNamespace is only used within parseTopLevelStatement, the code could be changed to obtain the identifier already in parseTopLevelStatement, passing it down to parseNamespace as an additional argument, in turn removing the unnecessary branch, any duplicate work (checking then reading) and perhaps even the need for a resettable state.

dcodeIO avatar May 31 '22 09:05 dcodeIO

@dcodeIO I created a draft PR, please kindly review, thank you.

JesseCodeBones avatar Jun 01 '22 09:06 JesseCodeBones

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!

github-actions[bot] avatar Jul 01 '22 23:07 github-actions[bot]