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

@ -26,11 +26,11 @@
*/
int CategorizeIp(uint32_t x) {
int a;
if (IsAnonymousIp(x)) return kIpAnonymous;
if (IsMulticastIp(x)) return kIpMulticast;
if (IsLoopbackIp(x)) return kIpLoopback;
if (IsPrivateIp(x)) return kIpPrivate;
if (IsTestnetIp(x)) return kIpTestnet;
if (IsMulticastIp(x)) return kIpMulticast;
if (IsAnonymousIp(x)) return kIpAnonymous; /* order matters */
if (IsTestnetIp(x)) return kIpTestnet; /* order matters */
if (IsAfrinicIp(x)) return kIpAfrinic;
if (IsLacnicIp(x)) return kIpLacnic;
if (IsApnicIp(x)) return kIpApnic;

View file

@ -1,6 +1,7 @@
/* ANSI-C code produced by gperf version 3.1 */
/* Command-line: gperf gethttpheader.gperf */
/* Computed positions: -k'3-4,10' */
/* clang-format off */
#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
&& ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
@ -71,7 +72,7 @@ static unsigned char gperf_downcase[256] =
#ifndef GPERF_CASE_STRNCMP
#define GPERF_CASE_STRNCMP 1
static int
static inline int
gperf_case_strncmp (register const char *s1, register const char *s2, register size_t n)
{
for (; n > 0;)
@ -152,7 +153,7 @@ hash (register const char *str, register size_t len)
return hval;
}
const struct thatispacked HttpHeaderSlot *
static inline const struct thatispacked HttpHeaderSlot *
LookupHttpHeader (register const char *str, register size_t len)
{
static const struct thatispacked HttpHeaderSlot wordlist[] =

View file

@ -1,6 +1,7 @@
/* ANSI-C code produced by gperf version 3.1 */
/* Command-line: gperf gethttpmethod.gperf */
/* Computed positions: -k'1-2' */
/* clang-format off */
#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
&& ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
@ -71,7 +72,7 @@ static unsigned char gperf_downcase[256] =
#ifndef GPERF_CASE_STRNCMP
#define GPERF_CASE_STRNCMP 1
static int
static inline int
gperf_case_strncmp (register const char *s1, register const char *s2, register size_t n)
{
for (; n > 0;)
@ -131,7 +132,7 @@ hash (register const char *str, register size_t len)
return len + asso_values[(unsigned char)str[1]] + asso_values[(unsigned char)str[0]];
}
const struct HttpMethodSlot *
static inline const struct HttpMethodSlot *
LookupHttpMethod (register const char *str, register size_t len)
{
static const struct HttpMethodSlot wordlist[] =
@ -153,7 +154,7 @@ LookupHttpMethod (register const char *str, register size_t len)
{""},
#line 27 "gethttpmethod.gperf"
{"NOTIFY", kHttpNotify},
#line 20 "gethttpmethod.gperf"
#line 19 "gethttpmethod.gperf"
{"OPTIONS", kHttpOptions},
{""},
#line 22 "gethttpmethod.gperf"
@ -162,7 +163,7 @@ LookupHttpMethod (register const char *str, register size_t len)
{"MERGE", kHttpMerge},
#line 29 "gethttpmethod.gperf"
{"REPORT", kHttpReport},
#line 19 "gethttpmethod.gperf"
#line 20 "gethttpmethod.gperf"
{"CONNECT", kHttpConnect},
{""},
#line 26 "gethttpmethod.gperf"

View file

@ -19,6 +19,26 @@
#include "libc/str/str.h"
#include "net/http/http.h"
// -_0-9A-Za-z
static const char kHostChars[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, // 0x20
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 0x30
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 0x50
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, // 0x70
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x80
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x90
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xa0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xb0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xc0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xd0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xe0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xf0
};
/**
* Returns true if host seems legit.
*
@ -49,33 +69,35 @@
*/
bool IsAcceptableHost(const char *s, size_t n) {
size_t i;
bool isip;
int c, b, j;
if (n == -1) n = s ? strlen(s) : 0;
if (!n) return true;
for (isip = true, b = j = i = 0; i < n; ++i) {
for (b = j = i = 0; i < n; ++i) {
c = s[i] & 255;
if (c == '.' && (!i || s[i - 1] == '.')) {
return false;
} else if (!(isalnum(c) || c == '-' || c == '_' || c == '.')) {
return false;
}
if (isip) {
if (isdigit(c)) {
b *= 10;
b += c - '0';
if (b > 255) {
if (isdigit(c)) {
b *= 10;
b += c - '0';
if (b > 255) {
return false;
}
} else if (c == '.') {
if (!i || s[i - 1] == '.') return false;
b = 0;
++j;
} else {
for (;;) {
if (!kHostChars[c] && (c != '.' || (!i || s[i - 1] == '.'))) {
return false;
}
} else if (c == '.') {
b = 0;
++j;
} else {
isip = false;
if (++i < n) {
c = s[i] & 255;
} else {
return true;
}
}
}
}
if (isip && j != 3) return false;
if (j != 3) return false;
if (i && s[i - 1] == '.') return false;
return true;
}

View file

@ -20,7 +20,10 @@
/**
* Returns true if IPv4 address can come from the Internet.
*
* We intentionally omit TEST-NET here which can be used to simulate
* public Internet traffic using non-Internet IPs.
*/
bool IsPublicIp(uint32_t x) {
return !IsLoopbackIp(x) && !IsPrivateIp(x) && !IsTestnetIp(x);
return !IsLoopbackIp(x) && !IsPrivateIp(x);
}

View file

@ -75,7 +75,7 @@ void DestroyHttpRequest(struct HttpRequest *r) {
* fragmented. If a message is valid but incomplete, this function will
* return zero so that it can be resumed as soon as more data arrives.
*
* This parser takes about 500 nanoseconds to parse a 403 byte Chrome
* This parser takes about 400 nanoseconds to parse a 403 byte Chrome
* HTTP request under MODE=rel on a Core i9 which is about three cycles
* per byte or a gigabyte per second of throughput per core.
*