rascal icon indicating copy to clipboard operation
rascal copied to clipboard

Performance of visit with type pattern order of magnitude slower than concrete syntax pattern

Open toinehartman opened this issue 8 months ago • 6 comments

Describe the bug

A visit with the pattern TypeVar _ is at least an order of magnitude slower than a visit with an exhaustive list of constructor patterns for this non-terminal. The following program illustrates this.

To Reproduce

module ProfileVisit

import IO;
import DateTime;
import ParseTree;
import lang::rascal::\syntax::Rascal;

int NUM_RUNS = 100;

int TypeMatch(Tree tr) {
    int i = 0;
    datetime s = now();
    for (_ <- [0..NUM_RUNS]) {
        top-down-break visit (tr) {
            case TypeVar _: i = i + 1;
        }
    }
    println("Elapsed: <now() - s>");
    return i;
}

int ProdMatch(Tree tr) {
    int i = 0;
    datetime s = now();
    for (_ <- [0..NUM_RUNS]) {
        top-down-break visit (tr) {
            case (TypeVar) `&<Name _>`: i = i + 1;
            case (TypeVar) `&<Name _> \<: <Type _>`: i = i + 1;
        }
    }
    println("Elapsed: <now() - s>");
    return i;
}

Now, this can be ran as follows (profile below):

rascal>import util::Reflective;
ok
rascal>m = parseModuleWithSpaces(|home:///swat/projects/Rascal/rascal/src/org/rascalmpl/library/List.rsc|);
...
rascal>ProdMatch(m);
Elapsed: duration(0,0,0,0,0,0,319)
int: 18700
rascal>TypeMatch(m);
Elapsed: duration(0,0,0,0,0,37,117)
int: 18700

Expected behavior I would expect the performance to be a lot closer to each other.

Screenshots Type-based run profile (slow): Image

Constructor-based run profile (fast): Image

Desktop (please complete the following information):

  • Context: interpreter
  • Rascal Version: 0.41.0-RC10

Investigated together with @DavyLandman

toinehartman avatar Mar 27 '25 09:03 toinehartman