Ignore:
Timestamp:
Jul 12, 2011, 3:14:30 AM (14 years ago)
Author:
[email protected]
Message:

Patch by Oliver Varga <[email protected]> on 2011-07-12
Reviewed by Nikolas Zimmermann.

Speed up SVGSMILElement::findInstanceTime.
https://bugs.webkit.org/show_bug.cgi?id=61025

Source/JavaScriptCore:

Add a new parameter to StdlibExtras.h::binarySerarch function
to also handle cases when the array does not contain the key value.
This is needed for an svg function.

  • wtf/StdLibExtras.h:

(WTF::binarySearch):

Source/WebCore:

Replace the linear search to binary search on ordered list because
the previous searches from the beginning was not efficient.
Out of index error fixed by Renata Hodovan.

No new tests this is only a performance tweak.

  • svg/animation/SVGSMILElement.cpp:

(WebCore::extractTimeFromVector):
(WebCore::SVGSMILElement::findInstanceTime):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/wtf/StdLibExtras.h

    r90117 r90811  
    124124}
    125125
     126enum BinarySearchMode {
     127    KeyMustBePresentInArray,
     128    KeyMustNotBePresentInArray
     129};
     130
    126131// Binary search algorithm, calls extractKey on pre-sorted elements in array,
    127132// compares result with key (KeyTypes should be comparable with '--', '<', '>').
    128 // Optimized for cases where the array contains the key, checked by assertions.
    129133template<typename ArrayType, typename KeyType, KeyType(*extractKey)(ArrayType*)>
    130 inline ArrayType* binarySearch(ArrayType* array, size_t size, KeyType key)
     134inline ArrayType* binarySearch(ArrayType* array, size_t size, KeyType key, BinarySearchMode mode = KeyMustBePresentInArray)
    131135{
    132     // The array must contain at least one element (pre-condition, array does conatin key).
    133     // If the array only contains one element, no need to do the comparison.
     136    // The array must contain at least one element (pre-condition, array does contain key).
     137    // If the array contains only one element, no need to do the comparison.
    134138    while (size > 1) {
    135139        // Pick an element to check, half way through the array, and read the value.
    136140        int pos = (size - 1) >> 1;
    137141        KeyType val = extractKey(&array[pos]);
    138        
     142
    139143        // If the key matches, success!
    140144        if (val == key)
     
    150154        }
    151155
    152         // 'size' should never reach zero.
    153         ASSERT(size);
     156        // In case of BinarySearchMode = KeyMustBePresentInArray 'size' should never reach zero.
     157        if (mode == KeyMustBePresentInArray)
     158            ASSERT(size);
    154159    }
    155    
    156     // If we reach this point we've chopped down to one element, no need to check it matches
    157     ASSERT(size == 1);
    158     ASSERT(key == extractKey(&array[0]));
     160
     161    // In case of BinarySearchMode = KeyMustBePresentInArray if we reach this point
     162    // we've chopped down to one element, no need to check it matches
     163    if (mode == KeyMustBePresentInArray) {
     164        ASSERT(size == 1);
     165        ASSERT(key == extractKey(&array[0]));
     166    }
     167
    159168    return &array[0];
    160169}
Note: See TracChangeset for help on using the changeset viewer.