Improve performance of printf functions

This commit is contained in:
Justine Tunney 2021-04-24 13:58:34 -07:00
parent b107d2709f
commit dc6d11a031
39 changed files with 577 additions and 650 deletions

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/iovec.h"
#include "libc/errno.h"
#include "libc/fmt/conv.h"
@ -28,30 +29,6 @@
#include "libc/str/str.h"
#include "libc/sysv/consts/o.h"
static ssize_t WritevAll(int fd, struct iovec *iov, int iovlen) {
ssize_t rc;
size_t wrote;
do {
if ((rc = writev(fd, iov, iovlen)) != -1) {
wrote = rc;
do {
if (wrote >= iov->iov_len) {
wrote -= iov->iov_len;
++iov;
--iovlen;
} else {
iov->iov_base = (char *)iov->iov_base + wrote;
iov->iov_len -= wrote;
wrote = 0;
}
} while (wrote);
} else if (errno != EINTR) {
return -1;
}
} while (iovlen);
return 0;
}
/**
* Writes data to stream.
*
@ -102,7 +79,7 @@ size_t fwrite(const void *data, size_t stride, size_t count, FILE *f) {
iov[1].iov_base = data;
iov[1].iov_len = n;
n += f->beg;
if (WritevAll(f->fd, iov, 2) == -1) {
if (WritevUninterruptible(f->fd, iov, 2) == -1) {
f->state = errno;
return 0;
}

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/fmt/fmt.h"
#include "libc/limits.h"
#include "libc/stdio/stdio.h"
@ -26,9 +27,17 @@ struct state {
int n;
};
static noinstrument int vfprintfputchar(int c, struct state *st) {
st->n++;
return fputc(c, st->f);
static int vfprintfputchar(const char *s, struct state *t, size_t n) {
if (n) {
if (n == 1 && *s != '\n' && t->f->beg < t->f->size &&
t->f->bufmode != _IONBF) {
t->f->buf[t->f->beg++] = *s;
} else if (!fwrite(s, 1, n, t->f)) {
return -1;
}
t->n += n;
}
return 0;
}
int(vfprintf)(FILE *f, const char *fmt, va_list va) {