Skip to content

Commit 992b26f

Browse files
committed
minimum std condition added
1 parent 589572f commit 992b26f

File tree

1 file changed

+80
-57
lines changed

1 file changed

+80
-57
lines changed

Objects/listobject.c

Lines changed: 80 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,72 +1903,92 @@ abinarysort(MergeState *ms, const sortslice *ss, Py_ssize_t n, Py_ssize_t ok, in
19031903
Py_ssize_t M, L, R;
19041904
Py_ssize_t nsorted = ok;
19051905
Py_ssize_t last = ok >> 1;
1906-
Py_ssize_t std = ok >> 2;
1906+
Py_ssize_t std = last;
19071907
Py_ssize_t mu = last;
1908-
Py_ssize_t nbad = 0; // badness of fit
1908+
Py_ssize_t nbad = 0; // badness of fit
19091909

19101910
if (adapt) {
19111911
for (; ok < n; ++ok) {
19121912
pivot = a[ok];
19131913

1914-
IFLT(pivot, a[mu]) {
1915-
L = 0;
1916-
R = mu;
1917-
if (L < R) {
1918-
// To not affect diff for measure counting
1919-
std += (std == 0);
1920-
M = R - std;
1921-
if (M < L)
1922-
M = L;
1923-
IFLT(pivot, a[M]) {
1924-
R = M;
1925-
if (L < R) {
1926-
M = R - std;
1927-
if (M < L)
1928-
M = L;
1929-
IFLT(pivot, a[M])
1930-
R = M;
1931-
else
1932-
L = M + 1;
1914+
if (std < ok / 4) {
1915+
M = mu;
1916+
IFLT(pivot, a[M]) {
1917+
L = 0;
1918+
R = M;
1919+
if (L < R) {
1920+
std += !std;
1921+
M = R - std;
1922+
if (M < L)
1923+
M = L;
1924+
IFLT(pivot, a[M]) {
1925+
R = M;
1926+
if (L < R) {
1927+
M = R - std;
1928+
if (M < L)
1929+
M = L;
1930+
IFLT(pivot, a[M])
1931+
R = M;
1932+
else
1933+
L = M + 1;
1934+
}
1935+
}
1936+
else {
1937+
L = M + 1;
19331938
}
19341939
}
1935-
else {
1936-
L = M + 1;
1940+
}
1941+
else {
1942+
L = M + 1;
1943+
R = ok;
1944+
if (L < R) {
1945+
M = L + std;
1946+
if (M >= R)
1947+
M = R - 1;
1948+
IFLT(pivot, a[M]) {
1949+
R = M;
1950+
}
1951+
else {
1952+
L = M + 1;
1953+
if (L < R) {
1954+
M = L + std;
1955+
if (M >= R)
1956+
M = R - 1;
1957+
IFLT(pivot, a[M])
1958+
R = M;
1959+
else
1960+
L = M + 1;
1961+
}
1962+
}
19371963
}
19381964
}
1939-
}
1940-
else {
1941-
L = mu + 1;
1942-
R = ok;
1943-
if (L < R) {
1944-
M = L + std;
1945-
if (M >= R)
1946-
M = R - 1;
1947-
IFLT(pivot, a[M]) {
1965+
// Binary Insertion
1966+
while (L < R) {
1967+
M = (L + R) >> 1;
1968+
IFLT(pivot, a[M])
19481969
R = M;
1949-
}
1950-
else {
1970+
else
19511971
L = M + 1;
1952-
if (L < R) {
1953-
M = L + std;
1954-
if (M >= R)
1955-
M = R - 1;
1956-
IFLT(pivot, a[M])
1957-
R = M;
1958-
else
1959-
L = M + 1;
1960-
}
1961-
}
19621972
}
19631973
}
1964-
1965-
// Binary Insertion
1966-
while (L < R) {
1967-
M = (L + R) >> 1;
1968-
IFLT(pivot, a[M])
1974+
else {
1975+
// Binary Insertion
1976+
M = ok >> 1;
1977+
IFLT(pivot, a[M]) {
1978+
L = 0;
19691979
R = M;
1970-
else
1980+
}
1981+
else {
19711982
L = M + 1;
1983+
R = ok;
1984+
}
1985+
while (L < R) {
1986+
M = (L + R) >> 1;
1987+
IFLT(pivot, a[M])
1988+
R = M;
1989+
else
1990+
L = M + 1;
1991+
}
19721992
}
19731993

19741994
for (M = ok; M > L; --M)
@@ -1992,10 +2012,17 @@ abinarysort(MergeState *ms, const sortslice *ss, Py_ssize_t n, Py_ssize_t ok, in
19922012
else {
19932013
for (; ok < n; ++ok) {
19942014
pivot = a[ok];
1995-
L = 0;
1996-
R = ok;
19972015

19982016
// Binary Insertion
2017+
M = ok >> 1;
2018+
IFLT(pivot, a[M]) {
2019+
L = 0;
2020+
R = M;
2021+
}
2022+
else {
2023+
L = M + 1;
2024+
R = ok;
2025+
}
19992026
while (L < R) {
20002027
M = (L + R) >> 1;
20012028
IFLT(pivot, a[M])
@@ -3253,12 +3280,8 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
32533280
} while (nremaining);
32543281
}
32553282
else {
3256-
// NOTE:WIP: Only 1% out of 6% worst case is due to
3283+
// NOTE:WIP: Only 1% difference is due to
32573284
// extra calculations in simple binary sort
3258-
// removing big branch in `abinarysort` also has not effect
3259-
// this has something to do with higher level branch prediction
3260-
// doing if (0) removes only 1% extra == 2%
3261-
// and commenting out code still 2% slower...???
32623285
int adapt = 0; // do not run binarysort adaptivity on 1st run
32633286
int cs = 0; // but do check goodness of adaptive fit
32643287
int cd = 1;

0 commit comments

Comments
 (0)