Changeset 32220 in webkit
- Timestamp:
- Apr 18, 2008, 12:02:50 PM (17 years ago)
- Location:
- trunk/JavaScriptCore
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/ChangeLog
r32198 r32220 1 2008-04-18 Alexey Proskuryakov <ap@webkit.org> 2 3 Reviewed by Darin. 4 5 Get rid of static compareWithCompareFunctionArguments in array_instance.cpp. 6 7 No change on SunSpider, CelticKane or iBench JavaScript. It is probable that in some cases, 8 merge sort is still faster, but more investigation is needed to determine a new cutoff. 9 Or possibly, it would be better to do what FIXME says (change to tree sort). 10 11 Also, made arguments a local variable - not sure why it was a member of 12 CompareWithCompareFunctionArguments. 13 14 * kjs/array_instance.cpp: 15 (KJS::CompareWithCompareFunctionArguments::CompareWithCompareFunctionArguments): 16 (KJS::CompareWithCompareFunctionArguments::operator()): 17 (KJS::ArrayInstance::sort): 18 1 19 2008-04-18 Simon Hausmann <hausmann@webkit.org> 2 20 -
trunk/JavaScriptCore/kjs/array_instance.cpp
r31946 r32220 485 485 486 486 struct CompareWithCompareFunctionArguments { 487 CompareWithCompareFunctionArguments(ExecState *e, JSObject *cf)488 : exec(e )487 CompareWithCompareFunctionArguments(ExecState* exec, JSObject* cf) 488 : exec(exec) 489 489 , compareFunction(cf) 490 , globalThisValue(e ->globalThisValue())490 , globalThisValue(exec->globalThisValue()) 491 491 { 492 492 } 493 493 494 ExecState *exec; 495 JSObject *compareFunction; 496 List arguments; 494 bool operator()(JSValue* va, JSValue* vb) 495 { 496 ASSERT(!va->isUndefined()); 497 ASSERT(!vb->isUndefined()); 498 499 List arguments; 500 arguments.append(va); 501 arguments.append(vb); 502 double compareResult = compareFunction->call(exec, globalThisValue, arguments)->toNumber(exec); 503 return compareResult < 0; 504 } 505 506 ExecState* exec; 507 JSObject* compareFunction; 497 508 JSObject* globalThisValue; 498 509 }; 499 510 500 static CompareWithCompareFunctionArguments* compareWithCompareFunctionArguments = 0;501 502 static int compareWithCompareFunctionForQSort(const void* a, const void* b)503 {504 CompareWithCompareFunctionArguments *args = compareWithCompareFunctionArguments;505 506 JSValue* va = *static_cast<JSValue* const*>(a);507 JSValue* vb = *static_cast<JSValue* const*>(b);508 ASSERT(!va->isUndefined());509 ASSERT(!vb->isUndefined());510 511 args->arguments.clear();512 args->arguments.append(va);513 args->arguments.append(vb);514 double compareResult = args->compareFunction->call(args->exec, args->globalThisValue, args->arguments)->toNumber(args->exec);515 return compareResult < 0 ? -1 : compareResult > 0 ? 1 : 0;516 }517 518 511 void ArrayInstance::sort(ExecState* exec, JSObject* compareFunction) 519 512 { 520 513 size_t lengthNotIncludingUndefined = compactForSorting(); 521 522 CompareWithCompareFunctionArguments* oldArgs = compareWithCompareFunctionArguments;523 CompareWithCompareFunctionArguments args(exec, compareFunction);524 compareWithCompareFunctionArguments = &args;525 526 #if HAVE(MERGESORT)527 // Because mergesort usually does fewer compares, it is faster than qsort here.528 // However, because it requires extra copies of the storage buffer, don't use it for very529 // large arrays.530 514 531 515 // FIXME: A tree sort using a perfectly balanced tree (e.g. an AVL tree) could do an even 532 516 // better job of minimizing compares. 533 517 534 if (lengthNotIncludingUndefined < copyingSortCutoff) { 535 // During the sort, we could do a garbage collect, and it's important to still 536 // have references to every object in the array for ArrayInstance::mark. 537 // The mergesort algorithm does not guarantee this, so we sort a copy rather 538 // than the original. 539 size_t size = storageSize(m_vectorLength); 540 ArrayStorage* copy = static_cast<ArrayStorage*>(fastMalloc(size)); 541 memcpy(copy, m_storage, size); 542 mergesort(copy->m_vector, lengthNotIncludingUndefined, sizeof(JSValue*), compareWithCompareFunctionForQSort); 543 fastFree(m_storage); 544 m_storage = copy; 545 compareWithCompareFunctionArguments = oldArgs; 546 return; 547 } 548 #endif 549 550 qsort(m_storage->m_vector, lengthNotIncludingUndefined, sizeof(JSValue*), compareWithCompareFunctionForQSort); 551 compareWithCompareFunctionArguments = oldArgs; 518 std::sort(m_storage->m_vector, m_storage->m_vector + lengthNotIncludingUndefined, CompareWithCompareFunctionArguments(exec, compareFunction)); 552 519 } 553 520
Note:
See TracChangeset
for help on using the changeset viewer.