strparse: speed up the hex parser somewhat

Around 2.3x speed-up parsing many large hexadecimal numbers. The decimal and
octal parser get marginally faster.

Still very readable, compact and easy to follow code.

Tweaks

- combine the max and the overflow check, gains 3ns/num (use a separate
  check outside of the loop instead for max < base)
- one less indirection in the pointer, gains 3ns/num
- using the table lookup for hex nums, gains 5ns/num
- unfold the num_digit() macro, gains 3s/num
- use the hexasciitable unconditionally, gains 2ns/num
- use post-increment pointer in the table lookup, gains 1ns/num
- improved valid_digit() using the table for the hex case,
  gains 26 ns/num
- use "max char" in valid_digit(), gains 3ns/num

Behavior changes:

- no longer returns STRE_TOO_BIG - only STRE_OVERFLOW
- does not move the char ** on error, which is probably better

Updated and extended test 1664 (significantly).

Closes #16374
This commit is contained in:
Daniel Stenberg 2025-02-17 22:34:21 +01:00
parent 3fd1dfc829
commit ad700a0917
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
6 changed files with 310 additions and 56 deletions

View file

@ -995,7 +995,7 @@ static CURLcode cf_socket_ctx_init(struct cf_socket_ctx *ctx,
p = getenv("CURL_DBG_SOCK_RMAX");
if(p) {
curl_off_t l;
if(!Curl_str_number(&p, &l, SIZE_T_MAX))
if(!Curl_str_number(&p, &l, CURL_OFF_T_MAX))
ctx->recv_max = (size_t)l;
}
}

View file

@ -882,14 +882,14 @@ CURLcode Curl_conn_send(struct Curl_easy *data, int sockindex,
DEBUGASSERT(data->conn);
conn = data->conn;
#ifdef DEBUGBUILD
{
if(write_len) {
/* Allow debug builds to override this logic to force short sends
*/
const char *p = getenv("CURL_SMALLSENDS");
if(p) {
curl_off_t altsize;
if(!Curl_str_number(&p, &altsize, SIZE_T_MAX))
write_len = CURLMIN(write_len, (size_t)altsize);
if(!Curl_str_number(&p, &altsize, write_len))
write_len = (size_t)altsize;
}
}
#endif

View file

@ -195,11 +195,13 @@ static CURLcode xfer_send(struct Curl_easy *data,
/* Allow debug builds to override this logic to force short initial
sends */
size_t body_len = blen - hds_len;
const char *p = getenv("CURL_SMALLREQSEND");
if(p) {
curl_off_t body_small;
if(!Curl_str_number(&p, &body_small, body_len))
blen = hds_len + (size_t)body_small;
if(body_len) {
const char *p = getenv("CURL_SMALLREQSEND");
if(p) {
curl_off_t body_small;
if(!Curl_str_number(&p, &body_small, body_len))
blen = hds_len + (size_t)body_small;
}
}
}
#endif

View file

@ -104,40 +104,57 @@ int Curl_str_singlespace(const char **linep)
return Curl_str_single(linep, ' ');
}
/* given an ASCII hexadecimal character, return the value */
#define HEXDIGIT2NUM(x) \
(((x) > '9') ? Curl_raw_tolower(x) - 'a' + 10 : x - '0')
/* given an ASCII character and a given base, return TRUE if valid */
#define valid_digit(digit, base) \
(((base == 10) && ISDIGIT(digit)) || \
((base == 16) && ISXDIGIT(digit)) || \
((base == 8) && ISODIGIT(digit)))
/* given an ASCII character and a given base, return the value */
#define num_digit(digit, base) \
((base != 16) ? digit - '0' : HEXDIGIT2NUM(digit))
/* given an ASCII character and max ascii, return TRUE if valid */
#define valid_digit(x,m) \
(((x) >= '0') && ((x) <= m) && hexasciitable[(x)-'0'])
/* no support for 0x prefix nor leading spaces */
static int str_num_base(const char **linep, curl_off_t *nump, curl_off_t max,
int base) /* 8, 10 or 16, nothing else */
{
/* We use 16 for the zero index (and the necessary bitwise AND in the loop)
to be able to have a non-zero value there to make valid_digit() able to
use the info */
static const unsigned char hexasciitable[] = {
16, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 0x30: 0 - 9 */
0, 0, 0, 0, 0, 0, 0,
10, 11, 12, 13, 14, 15, /* 0x41: A - F */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
10, 11, 12, 13, 14, 15 /* 0x61: a - f */
};
curl_off_t num = 0;
const char *p;
int m = (base == 10) ? '9' : /* the largest digit possible */
(base == 16) ? 'f' : '7';
DEBUGASSERT(linep && *linep && nump);
DEBUGASSERT((base == 8) || (base == 10) || (base == 16));
DEBUGASSERT(max >= 0); /* mostly to catch SIZE_T_MAX, which is too large */
*nump = 0;
if(!valid_digit(**linep, base))
p = *linep;
if(!valid_digit(*p, m))
return STRE_NO_NUM;
do {
int n = num_digit(**linep, base);
if(num > ((CURL_OFF_T_MAX - n) / base))
return STRE_OVERFLOW;
num = num * base + n;
if(num > max)
return STRE_BIG; /** too big */
(*linep)++;
} while(valid_digit(**linep, base));
if(max < base) {
/* special-case low max scenario because check needs to be different */
do {
int n = hexasciitable[*p++ - '0'] & 0x0f;
num = num * base + n;
if(num > max)
return STRE_OVERFLOW;
} while(valid_digit(*p, m));
}
else {
do {
int n = hexasciitable[*p++ - '0'] & 0x0f;
if(num > ((max - n) / base))
return STRE_OVERFLOW;
num = num * base + n;
} while(valid_digit(*p, m));
}
*nump = num;
*linep = p;
return STRE_OK;
}