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

@ -16,10 +16,10 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/safemacros.internal.h"
#include "libc/dce.h"
#include "libc/fmt/fmt.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/runtime/runtime.h"
struct SprintfStr {
@ -28,10 +28,13 @@ struct SprintfStr {
size_t n;
};
static noinstrument int vsnprintfputchar(unsigned char c,
struct SprintfStr *str) {
if (str->i < str->n) str->p[str->i] = c;
str->i++;
static int vsnprintfputchar(const char *s, struct SprintfStr *t, size_t n) {
if (t->i + n <= t->n) {
memcpy(t->p + t->i, s, n);
} else if (t->i < t->n) {
memcpy(t->p + t->i, s, t->n - t->i);
}
t->i += n;
return 0;
}
@ -51,6 +54,6 @@ static noinstrument int vsnprintfputchar(unsigned char c,
int(vsnprintf)(char *buf, size_t size, const char *fmt, va_list va) {
struct SprintfStr str = {buf, 0, size};
__fmt(vsnprintfputchar, &str, fmt, va);
if (str.n) str.p[min(str.i, str.n - 1)] = '\0';
if (str.n) str.p[MIN(str.i, str.n - 1)] = '\0';
return str.i;
}