Skip to content

JAVA: TokenStreamRewriter#reduceToSingleOperationPerIndex consumes lots of memory in case of lots of RewriteOperations #4886

@snuyanzin

Description

@snuyanzin

In Apache Flink (https://flink.apache.org/) we use antlr4 to split and rewrite generated code in order to make it fit 64Kb limitation from janino (https://janino-compiler.github.io/janino/)

The generated code might be huge enough and depending on rewrite rule there might be lots of RewriteOperations.

It looks like the more rewrite operations we have the way slower it works.

After looking into it seems the main issue is TokenStreamRewriter#getKindOfOps

protected <T extends RewriteOperation> List<? extends T> getKindOfOps(List<? extends RewriteOperation> rewrites, Class<T> kind, int before) {
List<T> ops = new ArrayList<T>();
for (int i=0; i<before && i<rewrites.size(); i++) {
RewriteOperation op = rewrites.get(i);
if ( op==null ) continue; // ignore deleted
if ( kind.isInstance(op) ) {
ops.add(kind.cast(op));
}
}
return ops;

which tries to iterate multiple times through rewrites and to create a new collection.

Since we already have all the data in rewrite may be don't need to create new collections then

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions