@@ -82,33 +82,34 @@ static int charTailAt(StringPair *P, size_t Pos) {
82
82
83
83
// Three-way radix quicksort. This is much faster than std::sort with strcmp
84
84
// because it does not compare characters that we already know the same.
85
- static void multikey_qsort (std::vector<StringPair *> &Vec, size_t Begin,
86
- size_t End, int Pos) {
85
+ static void multikeySort (MutableArrayRef<StringPair *> Vec, int Pos) {
87
86
tailcall:
88
- if (End - Begin <= 1 )
87
+ if (Vec. size () <= 1 )
89
88
return ;
90
89
91
- // Partition items so that items in [Begin, P) are greater than the pivot,
92
- // [P, Q) are the same as the pivot, and [Q, End) are less than the pivot.
93
- int Pivot = charTailAt (Vec[Begin], Pos);
94
- size_t P = Begin;
95
- size_t Q = End;
96
- for (size_t R = Begin + 1 ; R < Q;) {
97
- int C = charTailAt (Vec[R], Pos);
90
+ // Partition items so that items in [0, I) are greater than the pivot,
91
+ // [I, J) are the same as the pivot, and [J, Vec.size()) are less than
92
+ // the pivot.
93
+ int Pivot = charTailAt (Vec[0 ], Pos);
94
+ size_t I = 0 ;
95
+ size_t J = Vec.size ();
96
+ for (size_t K = 1 ; K < J;) {
97
+ int C = charTailAt (Vec[K], Pos);
98
98
if (C > Pivot)
99
- std::swap (Vec[P ++], Vec[R ++]);
99
+ std::swap (Vec[I ++], Vec[K ++]);
100
100
else if (C < Pivot)
101
- std::swap (Vec[--Q ], Vec[R ]);
101
+ std::swap (Vec[--J ], Vec[K ]);
102
102
else
103
- R ++;
103
+ K ++;
104
104
}
105
105
106
- multikey_qsort (Vec, Begin, P, Pos);
107
- multikey_qsort (Vec, Q, End, Pos);
106
+ multikeySort (Vec.slice (0 , I), Pos);
107
+ multikeySort (Vec.slice (J), Pos);
108
+
109
+ // multikeySort(Vec.slice(I, J - I), Pos + 1), but with
110
+ // tail call optimization.
108
111
if (Pivot != -1 ) {
109
- // qsort(P, Q, Pos + 1), but with tail call optimization.
110
- Begin = P;
111
- End = Q;
112
+ Vec = Vec.slice (I, J - I);
112
113
++Pos;
113
114
goto tailcall;
114
115
}
@@ -131,12 +132,7 @@ void StringTableBuilder::finalizeStringTable(bool Optimize) {
131
132
for (StringPair &P : StringIndexMap)
132
133
Strings.push_back (&P);
133
134
134
- if (!Strings.empty ()) {
135
- // If we're optimizing, sort by name. If not, sort by previously assigned
136
- // offset.
137
- multikey_qsort (Strings, 0 , Strings.size (), 0 );
138
- }
139
-
135
+ multikeySort (Strings, 0 );
140
136
initSize ();
141
137
142
138
StringRef Previous;
0 commit comments