Refactor VT parser for more speed

No longer copy bytes into a separate buffer, instead parse them in place
in the read buffer
This commit is contained in:
Kovid Goyal
2023-11-05 10:24:00 +05:30
parent 23bb2e1b67
commit 6205fb32fd
19 changed files with 1037 additions and 1015 deletions

View File

@@ -6,6 +6,7 @@
*/
#include "data-types.h"
#include "charsets.h"
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
@@ -19,17 +20,37 @@ static bool use_os_log = false;
void
log_error(const char *fmt, ...) {
va_list ar;
struct timeval tv;
#ifdef __APPLE__
// Apple does not provide a varargs style os_logv
char logbuf[16 * 1024] = {0};
#else
char logbuf[4];
#endif
#define bufprint(fmt, ...) { \
if ((size_t)(p - logbuf) < sizeof(logbuf) - 2) { \
p += vsnprintf(p, sizeof(logbuf) - (p - logbuf), fmt, __VA_ARGS__); \
} }
char logbuf[16 * 1024];
char *p = logbuf;
#define bufprint(func, ...) { if ((size_t)(p - logbuf) < sizeof(logbuf) - 2) { p += func(p, sizeof(logbuf) - (p - logbuf), __VA_ARGS__); } }
va_list ar;
va_start(ar, fmt);
bufprint(fmt, ar);
va_end(ar);
RAII_ALLOC(char, sanbuf, calloc(1, 3*(p - logbuf) + 1));
char utf8buf[4];
START_ALLOW_CASE_RANGE
size_t j = 0;
for (char *x = logbuf; x < p; x++) {
switch(*x) {
case C0_EXCEPT_NL_SPACE_TAB: {
const uint32_t ch = 0x2400 + *x;
const unsigned sz = encode_utf8(ch, utf8buf);
for (unsigned c = 0; c < sz; c++, j++) sanbuf[j] = utf8buf[c];
} break;
default:
sanbuf[j++] = *x;
break;
}
}
sanbuf[j] = 0;
END_ALLOW_CASE_RANGE
if (!use_os_log) { // Apple's os_log already records timestamps
struct timeval tv;
gettimeofday(&tv, NULL);
struct tm stack_tmp;
struct tm *tmp = localtime_r(&tv.tv_sec, &stack_tmp);
@@ -41,14 +62,11 @@ log_error(const char *fmt, ...) {
}
}
}
va_start(ar, fmt);
if (use_os_log) { bufprint(vsnprintf, fmt, ar); }
else vfprintf(stderr, fmt, ar);
va_end(ar);
#ifdef __APPLE__
if (use_os_log) os_log(OS_LOG_DEFAULT, "%{public}s", logbuf);
if (use_os_log) os_log(OS_LOG_DEFAULT, "%{public}s", sanbuf);
#endif
if (!use_os_log) fprintf(stderr, "\n");
if (!use_os_log) fprintf(stderr, "%s\n", sanbuf);
#undef bufprint
}
static PyObject*