Changeset 20974 in webkit for trunk/JavaScriptCore/kjs/array_object.cpp
- Timestamp:
- Apr 20, 2007, 3:20:15 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/array_object.cpp
r20949 r20974 303 303 void ArrayInstance::sort(ExecState* exec) 304 304 { 305 int lengthNotIncludingUndefined = pushUndefinedObjectsToEnd(exec);306 305 size_t lengthNotIncludingUndefined = pushUndefinedObjectsToEnd(exec); 306 307 307 ExecState* oldExec = execForCompareByStringForQSort; 308 308 execForCompareByStringForQSort = exec; 309 #if HAVE(MERGESORT) 310 // mergesort usually does fewer compares, so it is actually faster than qsort for JS sorts. 311 // however, becuase it requires extra copies of the storage buffer, don't use it for very 312 // large arrays 313 // FIXME: for sorting by string value, the fastest thing would actually be to convert all the 314 // values to string once up front, and then use a radix sort. That would be O(N) rather than 315 // O(N log N). 316 if (lengthNotIncludingUndefined < sparseArrayCutoff) { 317 JSValue** storageCopy = static_cast<JSValue**>(fastMalloc(capacity * sizeof(JSValue*))); 318 memcpy(storageCopy, storage, capacity * sizeof(JSValue*)); 319 mergesort(storageCopy, lengthNotIncludingUndefined, sizeof(JSValue *), compareByStringForQSort); 320 storage = storageCopy; 321 fastFree(storage); 322 execForCompareByStringForQSort = oldExec; 323 return; 324 } 325 #endif 326 309 327 qsort(storage, lengthNotIncludingUndefined, sizeof(JSValue*), compareByStringForQSort); 310 328 execForCompareByStringForQSort = oldExec; … … 352 370 void ArrayInstance::sort(ExecState* exec, JSObject* compareFunction) 353 371 { 354 int lengthNotIncludingUndefined = pushUndefinedObjectsToEnd(exec);372 size_t lengthNotIncludingUndefined = pushUndefinedObjectsToEnd(exec); 355 373 356 374 CompareWithCompareFunctionArguments* oldArgs = compareWithCompareFunctionArguments; 357 375 CompareWithCompareFunctionArguments args(exec, compareFunction); 358 376 compareWithCompareFunctionArguments = &args; 377 #if HAVE(MERGESORT) 378 // mergesort usually does fewer compares, so it is actually faster than qsort for JS sorts. 379 // however, becuase it requires extra copies of the storage buffer, don't use it for very 380 // large arrays 381 // FIXME: a tree sort using a perfectly balanced tree (e.g. an AVL tree) could do an even 382 // better job of minimizing compares 383 if (lengthNotIncludingUndefined < sparseArrayCutoff) { 384 JSValue** storageCopy = static_cast<JSValue**>(fastMalloc(capacity * sizeof(JSValue*))); 385 memcpy(storageCopy, storage, capacity * sizeof(JSValue*)); 386 mergesort(storageCopy, lengthNotIncludingUndefined, sizeof(JSValue *), compareWithCompareFunctionForQSort); 387 storage = storageCopy; 388 compareWithCompareFunctionArguments = oldArgs; 389 return; 390 } 391 #endif 359 392 qsort(storage, lengthNotIncludingUndefined, sizeof(JSValue*), compareWithCompareFunctionForQSort); 360 393 compareWithCompareFunctionArguments = oldArgs;
Note:
See TracChangeset
for help on using the changeset viewer.