Improve Python tree-shaking

This commit is contained in:
Justine Tunney 2021-09-06 19:24:10 -07:00
parent 5bb2275788
commit 4f41f2184d
169 changed files with 4182 additions and 2411 deletions

View file

@ -631,7 +631,7 @@ static void *__asan_memalign(size_t align, size_t size) {
}
static void *__asan_malloc(size_t size) {
return __asan_memalign(16, size);
return __asan_memalign(__BIGGEST_ALIGNMENT__, size);
}
static void *__asan_calloc(size_t n, size_t m) {
@ -641,44 +641,64 @@ static void *__asan_calloc(size_t n, size_t m) {
return p;
}
static void *__asan_realloc(void *p, size_t n) {
char *q, *f;
size_t c, m;
if (p) {
if (n) {
if ((c = weaken(dlmalloc_usable_size)(p)) >= 8) {
f = (char *)p + c - 8;
if ((m = READ64BE(f)) <= c) {
if (n <= m) { /* shrink */
__asan_poison((uintptr_t)p + n, m - n, kAsanHeapOverrun);
WRITE64BE(f, n);
q = p;
} else if (n <= c - 8) { /* small growth */
__asan_unpoison((uintptr_t)p + m, n - m);
WRITE64BE(f, n);
q = p;
} else if ((q = __asan_malloc(n))) { /* exponential growth */
__asan_memcpy(q, p, m);
__asan_deallocate(p, kAsanRelocated);
}
} else {
__asan_report_heap_fault(p, m);
}
} else {
__asan_report_heap_fault(p, 0);
}
} else {
__asan_free(p);
q = NULL;
}
} else {
q = __asan_malloc(n);
static void *__asan_realloc_nogrow(void *p, size_t n, size_t m) {
return 0;
}
static void *__asan_realloc_grow(void *p, size_t n, size_t m) {
char *q;
if ((q = __asan_malloc(n))) {
__asan_memcpy(q, p, m);
__asan_deallocate(p, kAsanRelocated);
}
return q;
}
static void *__asan_realloc_impl(void *p, size_t n,
void *grow(void *, size_t, size_t)) {
char *f;
size_t c, m;
if ((c = weaken(dlmalloc_usable_size)(p)) >= 8) {
f = (char *)p + c - 8;
if ((m = READ64BE(f)) <= c) {
if (n <= m) { /* shrink */
__asan_poison((uintptr_t)p + n, m - n, kAsanHeapOverrun);
WRITE64BE(f, n);
return p;
} else if (n <= c - 8) { /* small growth */
__asan_unpoison((uintptr_t)p + m, n - m);
WRITE64BE(f, n);
return p;
} else { /* exponential growth */
return grow(p, n, m);
}
} else {
__asan_report_heap_fault(p, m);
}
} else {
__asan_report_heap_fault(p, 0);
}
}
static void *__asan_realloc(void *p, size_t n) {
if (p) {
if (n) {
return __asan_realloc_impl(p, n, __asan_realloc_grow);
} else {
__asan_free(p);
return 0;
}
} else {
return __asan_malloc(n);
}
}
static void *__asan_realloc_in_place(void *p, size_t n) {
return 0;
if (p) {
return __asan_realloc_impl(p, n, __asan_realloc_nogrow);
} else {
return 0;
}
}
static void *__asan_valloc(size_t n) {

View file

@ -16,6 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/bits.h"
#include "libc/dce.h"
#include "libc/intrin/pcmpeqb.h"
#include "libc/intrin/pmovmskb.h"
#include "libc/nexgen32e/bsf.h"
@ -29,25 +31,116 @@
*/
int memcmp(const void *a, const void *b, size_t n) {
int c;
size_t i;
uint64_t w;
unsigned m;
uint8_t *p1, *p2;
uint8_t v1[16], v2[16];
if (n) {
for (p1 = a, p2 = b, i = 0; i + 16 <= n; i += 16) {
memcpy(v1, p1 + i, 16);
memcpy(v2, p2 + i, 16);
pcmpeqb(v1, v1, v2);
if ((m = pmovmskb(v1) - 0xffff)) {
i += bsf(m);
return p1[i] - p2[i];
uint8_t A[16], B[16];
const uint8_t *p = a, *q = b;
if (p == q) return 0;
if (IsTiny()) {
for (; n >= 8; p += 8, q += 8, n -= 8) {
w = READ64LE(p) ^ READ64LE(q);
if (w) {
m = bsfl(w) >> 3;
return p[m] - q[m];
}
}
for (; i < n; ++i) {
if ((c = p1[i] - p2[i])) {
for (; n; ++p, ++q, --n) {
if ((c = *p - *q)) {
return c;
}
}
return 0;
}
return 0;
StartOver:
switch (n) {
case 0:
return 0;
case 1:
return *p - *q;
case 2:
w = (p[0] << 000 | p[1] << 010) ^ (q[0] << 000 | q[1] << 010);
break;
case 3:
w = (p[0] << 000 | p[1] << 010 | p[2] << 020) ^
(q[0] << 000 | q[1] << 010 | q[2] << 020);
break;
case 4:
w = ((uint32_t)p[0] << 000 | (uint32_t)p[1] << 010 |
(uint32_t)p[2] << 020 | (uint32_t)p[3] << 030) ^
((uint32_t)q[0] << 000 | (uint32_t)q[1] << 010 |
(uint32_t)q[2] << 020 | (uint32_t)q[3] << 030);
break;
case 5:
w = ((uint64_t)p[0] << 000 | (uint64_t)p[1] << 010 |
(uint64_t)p[2] << 020 | (uint64_t)p[3] << 030 |
(uint64_t)p[4] << 040) ^
((uint64_t)q[0] << 000 | (uint64_t)q[1] << 010 |
(uint64_t)q[2] << 020 | (uint64_t)q[3] << 030 |
(uint64_t)q[4] << 040);
break;
case 6:
w = ((uint64_t)p[0] << 000 | (uint64_t)p[1] << 010 |
(uint64_t)p[2] << 020 | (uint64_t)p[3] << 030 |
(uint64_t)p[4] << 040 | (uint64_t)p[5] << 050) ^
((uint64_t)q[0] << 000 | (uint64_t)q[1] << 010 |
(uint64_t)q[2] << 020 | (uint64_t)q[3] << 030 |
(uint64_t)q[4] << 040 | (uint64_t)q[5] << 050);
break;
case 7:
w = ((uint64_t)p[0] << 000 | (uint64_t)p[1] << 010 |
(uint64_t)p[2] << 020 | (uint64_t)p[3] << 030 |
(uint64_t)p[4] << 040 | (uint64_t)p[5] << 050 |
(uint64_t)p[6] << 060) ^
((uint64_t)q[0] << 000 | (uint64_t)q[1] << 010 |
(uint64_t)q[2] << 020 | (uint64_t)q[3] << 030 |
(uint64_t)q[4] << 040 | (uint64_t)q[5] << 050 |
(uint64_t)q[6] << 060);
break;
case 8:
w = ((uint64_t)p[0] << 000 | (uint64_t)p[1] << 010 |
(uint64_t)p[2] << 020 | (uint64_t)p[3] << 030 |
(uint64_t)p[4] << 040 | (uint64_t)p[5] << 050 |
(uint64_t)p[6] << 060 | (uint64_t)p[7] << 070) ^
((uint64_t)q[0] << 000 | (uint64_t)q[1] << 010 |
(uint64_t)q[2] << 020 | (uint64_t)q[3] << 030 |
(uint64_t)q[4] << 040 | (uint64_t)q[5] << 050 |
(uint64_t)q[6] << 060 | (uint64_t)q[7] << 070);
break;
default:
for (; n >= 16; p += 16, q += 16, n -= 16) {
memcpy(A, p, 16);
memcpy(B, q, 16);
pcmpeqb(A, A, B);
if ((m = pmovmskb(A) - 0xffff)) {
m = bsf(m);
return p[m] - q[m];
}
}
if (n > 8) {
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
w = ((uint64_t)p[0] << 000 | (uint64_t)p[1] << 010 |
(uint64_t)p[2] << 020 | (uint64_t)p[3] << 030 |
(uint64_t)p[4] << 040 | (uint64_t)p[5] << 050 |
(uint64_t)p[6] << 060 | (uint64_t)p[7] << 070) ^
((uint64_t)q[0] << 000 | (uint64_t)q[1] << 010 |
(uint64_t)q[2] << 020 | (uint64_t)q[3] << 030 |
(uint64_t)q[4] << 040 | (uint64_t)q[5] << 050 |
(uint64_t)q[6] << 060 | (uint64_t)q[7] << 070);
if (w) goto ItsDifferent;
p += 8;
q += 8;
n -= 8;
}
goto StartOver;
}
if (!w) return 0;
ItsDifferent:
m = bsfl(w) >> 3;
return p[m] - q[m];
}

View file

@ -56,6 +56,7 @@ o/$(MODE)/libc/str/dosdatetimetounix.o: \
OVERRIDE_CFLAGS += \
-O3
o//libc/str/memcmp.o \
o/$(MODE)/libc/str/getzipcdir.o \
o/$(MODE)/libc/str/getzipcdircomment.o \
o/$(MODE)/libc/str/getzipcdircommentsize.o \

View file

@ -0,0 +1,16 @@
#ifndef COSMOPOLITAN_LIBC_TESTLIB_FASTRANDOMSTRING_H_
#define COSMOPOLITAN_LIBC_TESTLIB_FASTRANDOMSTRING_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
static inline const char *FastRandomString(void) {
static unsigned long t;
static union {
unsigned long x;
char b[sizeof(unsigned long)];
} u;
u.x = (t = (t * 0xdeaadead) + 0xdeaadead) & 0x7e7e7e7e7e7e7e;
return u.b;
}
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_TESTLIB_FASTRANDOMSTRING_H_ */

View file

@ -21,6 +21,7 @@ LIBC_TESTLIB_A_HDRS = \
libc/testlib/bench.h \
libc/testlib/blocktronics.h \
libc/testlib/ezbench.h \
libc/testlib/fastrandomstring.h \
libc/testlib/hyperion.h \
libc/testlib/moby.h \
libc/testlib/testlib.h

View file

@ -36,21 +36,29 @@
*/
char *(xstrcat)(const char *s, ...) {
va_list va;
size_t n, m;
intptr_t q;
char *p, b[2];
p = NULL;
size_t n, m, c;
n = 0;
c = 32;
p = xmalloc(c);
va_start(va, s);
do {
if ((intptr_t)s > 0 && (intptr_t)s <= 255) {
b[0] = (unsigned char)(intptr_t)s;
q = (intptr_t)s;
if (q > 0 && q <= 255) {
b[0] = q;
b[1] = '\0';
s = b;
m = 1;
} else {
m = strlen(s);
}
p = xrealloc(p, n + m + 1);
if (n + m + 1 > c) {
do {
c += c >> 1;
} while (n + m + 1 > c);
p = xrealloc(p, c);
}
memcpy(p + n, s, m + 1);
n += m;
} while ((s = va_arg(va, const char *)));