Skip to content
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

Fix slow token rescanning during preprocessing. #442

Merged
merged 2 commits into from
Apr 1, 2015

Conversation

abbeyj
Copy link

@abbeyj abbeyj commented Mar 30, 2015

The loop to rescan tokens during preprocessing can become very slow when expanding a macro that consists of many tokens. For instance, preprocessing the following can take several minutes:

void foo() {}

#define A foo();
#define FOO() \
    A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A \
    A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A \
    A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A \
    A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A \
    A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A \
    A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A \
    A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A \
    A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A \
    A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A \
    A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A

int main()
{
    FOO()
    return 0;
}

You can add more lines of As to make it take an arbitrarily long amount of time.

The problem is the calls like replTokens = replTokens.subList(...) on each iteration of the loop. subList doesn't return a new list but a SubList that's a "view" of the existing list. And calling subList on a SubList doesn't create a view of the underlying list but instead creates a view of the SubList or a 'view of a view". After running through the loop a few times you now have a "view of a view of a view of a view of ..." which gets very slow.

Modifying the existing list in-place solves this problem.

I'd like to have a test for this but I'm not sure how to write one when the only observable difference in behavior is how long it takes to run.

@guwirth
Copy link
Collaborator

guwirth commented Mar 31, 2015

There are many unit test for the preprocessor. If everything is still green it should work.

@guwirth guwirth added this to the M 0.9.3 milestone Mar 31, 2015
wenns added a commit that referenced this pull request Apr 1, 2015
Fix slow token rescanning during preprocessing.
@wenns wenns merged commit a7e77bb into SonarOpenCommunity:master Apr 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants