https://bugs.webkit.org/show_bug.cgi?id=150828
Reviewed by Geoffrey Garen.
Source/JavaScriptCore:
Undo the 2% compile time regression caused by http://trac.webkit.org/changeset/191913.
* b3/B3InsertionSet.cpp:
(JSC::B3::InsertionSet::execute): Switch to bubble sort.
* b3/air/AirInsertionSet.cpp:
(JSC::B3::Air::InsertionSet::execute): Switch to bubble sort.
* dfg/DFGBlockInsertionSet.cpp:
(JSC::DFG::BlockInsertionSet::execute): Switch back to quicksort.
Source/WTF:
Add a pretty good bubble sort implementation to WTF. This implementation has three
common tricks:
- Forward and backward scans. This reduces the severity of certain kinds of bubble sort
pathologies.
- Return if a scan finds the list to be sorted. This gives the algorithm one of its most
attractive properties: it's super fast when the list is already sorted.
- Each scan eliminates one element from future scans. This makes the algorithm no worse
than insertion sort.
Why do we want this? Because bubble sort is a really great stable sort for small lists,
or large lists in which only a handful of elements are out of order. Compiler insertion
sets tend to be one of those or somewhere in between: usually they are very small, and
usually they are sorted. It's rare that an element will be out of order, and when it is,
it's usually very close to where it's supposed to be.
This is a significant speed-up for B3 compile times.
* WTF.xcodeproj/project.pbxproj:
* wtf/BubbleSort.h: Added.
(WTF::bubbleSort):
* wtf/CMakeLists.txt:
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@191960
268f45cc-cd09-0410-ab3c-
d52691b4dbfc
+2015-11-02 Filip Pizlo <fpizlo@apple.com>
+
+ B3/Air should use bubble sort for their insertion sets, because it's faster than std::stable_sort
+ https://bugs.webkit.org/show_bug.cgi?id=150828
+
+ Reviewed by Geoffrey Garen.
+
+ Undo the 2% compile time regression caused by http://trac.webkit.org/changeset/191913.
+
+ * b3/B3InsertionSet.cpp:
+ (JSC::B3::InsertionSet::execute): Switch to bubble sort.
+ * b3/air/AirInsertionSet.cpp:
+ (JSC::B3::Air::InsertionSet::execute): Switch to bubble sort.
+ * dfg/DFGBlockInsertionSet.cpp:
+ (JSC::DFG::BlockInsertionSet::execute): Switch back to quicksort.
+
2015-11-03 Csaba Osztrogonác <ossy@webkit.org>
Unreviewed, partially revert r191952.
#if ENABLE(B3_JIT)
#include "B3BasicBlock.h"
+#include <wtf/BubbleSort.h>
namespace JSC { namespace B3 {
void InsertionSet::execute(BasicBlock* block)
{
- std::stable_sort(m_insertions.begin(), m_insertions.end());
+ bubbleSort(m_insertions.begin(), m_insertions.end());
executeInsertions(block->m_values, m_insertions);
}
#if ENABLE(B3_JIT)
#include "AirBasicBlock.h"
-#include <algorithm>
+#include <wtf/BubbleSort.h>
namespace JSC { namespace B3 { namespace Air {
void InsertionSet::execute(BasicBlock* block)
{
- std::stable_sort(m_insertions.begin(), m_insertions.end());
+ bubbleSort(m_insertions.begin(), m_insertions.end());
executeInsertions(block->m_insts, m_insertions);
}
if (m_insertions.isEmpty())
return false;
- // We allow insertions to be given to us in any order. So, we need to
- // sort them before running WTF::executeInsertions.
- std::stable_sort(m_insertions.begin(), m_insertions.end());
+ // We allow insertions to be given to us in any order. So, we need to sort them before
+ // running WTF::executeInsertions. Also, we don't really care if the sort is stable since
+ // basic block order doesn't have semantics - it's just to make code easier to read.
+ std::sort(m_insertions.begin(), m_insertions.end());
executeInsertions(m_graph.m_blocks, m_insertions);
+2015-11-02 Filip Pizlo <fpizlo@apple.com>
+
+ B3/Air should use bubble sort for their insertion sets, because it's faster than std::stable_sort
+ https://bugs.webkit.org/show_bug.cgi?id=150828
+
+ Reviewed by Geoffrey Garen.
+
+ Add a pretty good bubble sort implementation to WTF. This implementation has three
+ common tricks:
+
+ - Forward and backward scans. This reduces the severity of certain kinds of bubble sort
+ pathologies.
+
+ - Return if a scan finds the list to be sorted. This gives the algorithm one of its most
+ attractive properties: it's super fast when the list is already sorted.
+
+ - Each scan eliminates one element from future scans. This makes the algorithm no worse
+ than insertion sort.
+
+ Why do we want this? Because bubble sort is a really great stable sort for small lists,
+ or large lists in which only a handful of elements are out of order. Compiler insertion
+ sets tend to be one of those or somewhere in between: usually they are very small, and
+ usually they are sorted. It's rare that an element will be out of order, and when it is,
+ it's usually very close to where it's supposed to be.
+
+ This is a significant speed-up for B3 compile times.
+
+ * WTF.xcodeproj/project.pbxproj:
+ * wtf/BubbleSort.h: Added.
+ (WTF::bubbleSort):
+ * wtf/CMakeLists.txt:
+
2015-11-02 Andy Estes <aestes@apple.com>
[Cocoa] Add tvOS and watchOS to SUPPORTED_PLATFORMS
0F2B66A717B6B4FD00A7AE3F /* FlipBytes.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F2B66A517B6B4F700A7AE3F /* FlipBytes.h */; };
0F3501641BB258D500F0A2A3 /* WeakRandom.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F3501631BB258C800F0A2A3 /* WeakRandom.h */; };
0F4570431BE5B58F0062A629 /* Dominators.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F4570421BE5B58F0062A629 /* Dominators.h */; };
+ 0F4570451BE834410062A629 /* BubbleSort.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F4570441BE834410062A629 /* BubbleSort.h */; };
0F824A681B7443A0002E345D /* ParkingLot.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0F824A641B7443A0002E345D /* ParkingLot.cpp */; };
0F824A691B7443A0002E345D /* ParkingLot.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F824A651B7443A0002E345D /* ParkingLot.h */; };
0F87105A16643F190090B0AD /* RawPointer.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F87105916643F190090B0AD /* RawPointer.h */; };
0F300B7D18AB48B400A6D72E /* HashMethod.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HashMethod.h; sourceTree = "<group>"; };
0F3501631BB258C800F0A2A3 /* WeakRandom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WeakRandom.h; sourceTree = "<group>"; };
0F4570421BE5B58F0062A629 /* Dominators.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Dominators.h; sourceTree = "<group>"; };
+ 0F4570441BE834410062A629 /* BubbleSort.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BubbleSort.h; sourceTree = "<group>"; };
0F824A641B7443A0002E345D /* ParkingLot.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParkingLot.cpp; sourceTree = "<group>"; };
0F824A651B7443A0002E345D /* ParkingLot.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParkingLot.h; sourceTree = "<group>"; };
0F87105916643F190090B0AD /* RawPointer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RawPointer.h; sourceTree = "<group>"; };
A8A47261151A825A004123FF /* BitVector.h */,
A8A47264151A825A004123FF /* BlockStack.h */,
A8A47265151A825A004123FF /* BloomFilter.h */,
+ 0F4570441BE834410062A629 /* BubbleSort.h */,
A8A47267151A825A004123FF /* BumpPointerAllocator.h */,
EB95E1EF161A72410089A2F5 /* ByteOrder.h */,
A8A4726A151A825A004123FF /* CheckedArithmetic.h */,
FEDACD3E1630F83F00C69634 /* StackStats.h in Headers */,
A8A47429151A825B004123FF /* StaticConstructors.h in Headers */,
A8A4742A151A825B004123FF /* StdLibExtras.h in Headers */,
+ 0F4570451BE834410062A629 /* BubbleSort.h in Headers */,
C4F8A93719C65EB400B2B15D /* Stopwatch.h in Headers */,
1A6BB769162F300500DD16DB /* StreamBuffer.h in Headers */,
A8A4743B151A825B004123FF /* StringBuffer.h in Headers */,
--- /dev/null
+/*
+ * Copyright (C) 2015 Apple Inc. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef BubbleSort_h
+#define BubbleSort_h
+
+namespace WTF {
+
+// Why would you want to use bubble sort? When you know that your input is already mostly
+// sorted! This sort is guaranteed stable (it won't reorder elements that were equal), it
+// doesn't require any scratch memory, and is the fastest available sorting algorithm if your
+// input already happens to be sorted. This sort is also likely to have competetive performance
+// for small inputs, even if they are very unsorted.
+
+// We use this sorting algorithm for compiler insertion sets. An insertion set is usually very
+// nearly sorted. It shouldn't take more than a few bubbles to make it fully sorted. We made
+// this decision deliberately. Here's the performance of the testb3 Complex(64, 384) benchmark
+// with the Air::InsertionSet doing no sorting, std::stable_sorting, and bubbleSorting:
+//
+// no sort: 8.8222 +- 0.1911 ms.
+// std::stable_sort: 9.0135 +- 0.1418 ms.
+// bubbleSort: 8.8457 +- 0.1511 ms.
+//
+// Clearly, bubble sort is superior.
+//
+// Note that the critical piece here is that insertion sets tend to be small, they must be
+// sorted, the sort must be stable, they are usually already sorted to begin with, and when they
+// are unsorted it's usually because of a few out-of-place elements.
+
+template<typename IteratorType, typename LessThan>
+void bubbleSort(IteratorType begin, IteratorType end, const LessThan& lessThan)
+{
+ for (;;) {
+ bool changed = false;
+ ASSERT(end >= begin);
+ size_t limit = end - begin;
+ for (size_t i = limit; i-- > 1;) {
+ if (lessThan(begin[i], begin[i - 1])) {
+ std::swap(begin[i], begin[i - 1]);
+ changed = true;
+ }
+ }
+ if (!changed)
+ return;
+ // After one run, the first element in the list is guaranteed to be the smallest.
+ begin++;
+
+ // Now go in the other direction. This eliminates most sorting pathologies.
+ changed = false;
+ ASSERT(end >= begin);
+ limit = end - begin;
+ for (size_t i = 1; i < limit; ++i) {
+ if (lessThan(begin[i], begin[i - 1])) {
+ std::swap(begin[i], begin[i - 1]);
+ changed = true;
+ }
+ }
+ if (!changed)
+ return;
+ // Now the last element is guaranteed to be the largest.
+ end--;
+ }
+}
+
+template<typename IteratorType>
+void bubbleSort(IteratorType begin, IteratorType end)
+{
+ bubbleSort(
+ begin, end,
+ [] (typename std::iterator_traits<IteratorType>::value_type left,
+ typename std::iterator_traits<IteratorType>::value_type right) -> bool {
+ return left < right;
+ });
+}
+
+} // namespace WTF
+
+using WTF::bubbleSort;
+
+#endif // BubbleSort_h
+
BagToHashMap.h
BitVector.h
Bitmap.h
+ BubbleSort.h
BumpPointerAllocator.h
ByteOrder.h
CompilationThread.h