Changeset 76502 in webkit for trunk/Source/JavaScriptCore/yarr


Ignore:
Timestamp:
Jan 24, 2011, 3:52:43 AM (14 years ago)
Author:
[email protected]
Message:

2011-01-24 Peter Varga <[email protected]>

Reviewed by Oliver Hunt.

Optimize regex patterns which contain empty alternatives
https://bugs.webkit.org/show_bug.cgi?id=51395

Eliminate the empty alternatives from the regex pattern and convert it to do
the matching in an easier way.

  • fast/regex/script-tests/slow.js:
  • fast/regex/slow-expected.txt:

2011-01-24 Peter Varga <[email protected]>

Reviewed by Oliver Hunt.

Optimize regex patterns which contain empty alternatives
https://bugs.webkit.org/show_bug.cgi?id=51395

Eliminate the empty alternatives from the regex pattern and convert it to do
the matching in an easier way.

  • yarr/YarrPattern.cpp: (JSC::Yarr::YarrPatternConstructor::atomParenthesesEnd):
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/yarr/YarrPattern.cpp

    r75602 r76502  
    489489
    490490        PatternTerm& lastTerm = m_alternative->lastTerm();
    491        
     491
    492492        unsigned numParenAlternatives = parenthesesDisjunction->m_alternatives.size();
    493493        unsigned numBOLAnchoredAlts = 0;
    494         // Bubble up BOL flags
     494        bool containsEmptyAlternative = false;
     495
    495496        for (unsigned i = 0; i < numParenAlternatives; i++) {
     497            if (!parenthesesDisjunction->m_alternatives[i]->m_terms.size() && numParenAlternatives > 1) {
     498                parenthesesDisjunction->m_alternatives.remove(i);
     499                --numParenAlternatives;
     500
     501                containsEmptyAlternative = true;
     502                continue;
     503            }
     504
     505            // Bubble up BOL flags
    496506            if (parenthesesDisjunction->m_alternatives[i]->m_startsWithBOL)
    497507                numBOLAnchoredAlts++;
    498508        }
    499        
     509
    500510        if (numBOLAnchoredAlts) {
    501511            m_alternative->m_containsBOL = true;
     
    504514                m_alternative->m_startsWithBOL = true;
    505515        }
    506        
     516
    507517        lastTerm.parentheses.lastSubpatternId = m_pattern.m_numSubpatterns;
    508518        m_invertParentheticalAssertion = false;
     519
     520        if (containsEmptyAlternative) {
     521            // Backup and remove the current disjunction's alternatives.
     522            Vector<PatternAlternative*> alternatives;
     523            alternatives.append(parenthesesDisjunction->m_alternatives);
     524            parenthesesDisjunction->m_alternatives.clear();
     525            PatternAlternative* alternative = parenthesesDisjunction->addNewAlternative();
     526
     527            // Insert a new non-capturing parentheses.
     528            unsigned subpatternId = m_pattern.m_numSubpatterns + 1;
     529            PatternDisjunction* newDisjunction = new PatternDisjunction(alternative);
     530            m_pattern.m_disjunctions.append(newDisjunction);
     531            alternative->m_terms.append(PatternTerm(PatternTerm::TypeParenthesesSubpattern, subpatternId, newDisjunction, false, false));
     532            newDisjunction->m_alternatives.append(alternatives);
     533
     534            // Set the quantifier of the new parentheses to '?' and set the inherited properties.
     535            PatternTerm& disjunctionTerm = alternative->lastTerm();
     536            disjunctionTerm.quantify(1, QuantifierGreedy);
     537            disjunctionTerm.parentheses.lastSubpatternId = m_pattern.m_numSubpatterns;
     538            alternative->m_containsBOL = m_alternative->m_containsBOL;
     539            alternative->m_startsWithBOL = m_alternative->m_startsWithBOL;
     540        }
    509541    }
    510542
Note: See TracChangeset for help on using the changeset viewer.