Skip to content

Multi-line comment indentation + compact output makes code generation very slow #2008

@jbt

Description

@jbt

If I have a really big file like:

var foo = class {
    doesntReallyMatter(){}
}
... few thousand lines of things ...

/*
 @license
 */
... maybe some other stuff

and I compile it with compact:true (or the auto-compact-erizer kicks in because it's >100k, with or without comments:false because @license is always preserved), code generation takes ages (many minutes, versus seconds if the license comment isn't there)

It looks like it's coming from the trim-right in Buffer#get (replacing that line with return this.buf makes everything fast again).

By the look of it, trim-right uses a regex that's quadratically bad if you have massive expanses of whitespace that aren't at the end of the string, which is what happens in this case because the output looks something like this (with potentially enormous amounts of whitespace to align the comment):

foo something [lots and lots of stuff, many hundreds of thousands of characters] /*
                                                                                  @license
                                                                                  */ rest of the things

I'm not sure what you'd consider to be the most elegant solution here - changing Buffer#get to something like:

get(){
    var tail = this.buf.length;
    while(/[\s\uFEFF\xA0]/.test(this.buf.charAt(tail - 1))){
        tail--;
    }
    return this.buf.slice(0, tail);
}

... seems to work for me (although it may or may not have an off-by-one error because I've written it fairly quickly). Alternatively getting rid of the indentation of comments in compact mode would probably achieve the same effect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions