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

@ -205,6 +205,114 @@ UNITTEST_START
}
}
{
struct t {
const char *str;
curl_off_t max;
};
static struct t nums[] = {
{ "00", 8},
{ "1", 8},
{ "1", 1},
{ "2", 1},
{ "2", 2},
{ "5", 6},
{ "000000000000000000000006", 6},
{ "7", 6},
{ "8", 6},
{ "9", 8},
{ "10", 10},
{ "11", 10},
{ "12", 10},
{NULL, 0}
};
printf("Curl_str_number varying max\n");
for(i = 0; nums[i].str; i++) {
curl_off_t num;
const char *line = nums[i].str;
const char *orgline = line;
int rc = Curl_str_number(&line, &num, nums[i].max);
printf("%u: (\"%s\") max %" CURL_FORMAT_CURL_OFF_T
" == %d, [%" CURL_FORMAT_CURL_OFF_T "]\n",
i, orgline, nums[i].max, rc, num);
}
}
{
struct t {
const char *str;
curl_off_t max;
};
static struct t nums[] = {
{ "00", 8},
{ "1", 8},
{ "1", 1},
{ "2", 1},
{ "2", 2},
{ "5", 6},
{ "000000000000000000000006", 6},
{ "7", 6},
{ "8", 6},
{ "9", 8},
{ "a", 14},
{ "b", 14},
{ "c", 14},
{ "d", 14},
{ "e", 14},
{ "f", 14},
{ "f", 15},
{ "10", 16},
{ "11", 16},
{ "12", 16},
{NULL, 0}
};
printf("Curl_str_hex varying max\n");
for(i = 0; nums[i].str; i++) {
curl_off_t num;
const char *line = nums[i].str;
const char *orgline = line;
int rc = Curl_str_hex(&line, &num, nums[i].max);
printf("%u: (\"%s\") max %" CURL_FORMAT_CURL_OFF_T
" == %d, [%" CURL_FORMAT_CURL_OFF_T "]\n",
i, orgline, nums[i].max, rc, num);
}
}
{
struct t {
const char *str;
curl_off_t max;
};
static struct t nums[] = {
{ "00", 4},
{ "1", 4},
{ "1", 4},
{ "2", 4},
{ "3", 4},
{ "4", 4},
{ "5", 4},
{ "000000000000000000000006", 6},
{ "7", 7},
{ "10", 8},
{ "11", 8},
{ "11", 9},
{ "12", 9},
{ "13", 9},
{ "8", 10},
{NULL, 0}
};
printf("Curl_str_octal varying max\n");
for(i = 0; nums[i].str; i++) {
curl_off_t num;
const char *line = nums[i].str;
const char *orgline = line;
int rc = Curl_str_octal(&line, &num, nums[i].max);
printf("%u: (\"%s\") max %" CURL_FORMAT_CURL_OFF_T
" == %d, [%" CURL_FORMAT_CURL_OFF_T "]\n",
i, orgline, nums[i].max, rc, num);
}
}
{
/* CURL_OFF_T is typically 9223372036854775807 */
static const char *nums[] = {
@ -213,6 +321,20 @@ UNITTEST_START
"18446744073709551615", /* 2^64 - 1 */
"18446744073709551616", /* 2^64 */
"18446744073709551617", /* 2^64 + 1 */
"0123456799a",
"0123456789",
"123498760b",
"1234987607611298232",
"1111111111111111111",
"2222222222222222222",
"00000000000000000000000000000009223372036854775807",
"3333333333333333333",
"4444444444444444444",
"5555555555555555555",
"6666666666666666666",
"7777777777777777777",
"8888888888888888888",
"999999999999999999",
NULL
};
printf("Curl_str_number / max\n");
@ -237,6 +359,7 @@ UNITTEST_START
"\n",
"\r",
"\r\n",
"\x0c",
"",
NULL
};
@ -245,8 +368,8 @@ UNITTEST_START
const char *line = newl[i];
const char *orgline = line;
int rc = Curl_str_newline(&line);
printf("%u: (\"%s\") %d, line %d\n",
i, orgline, rc, (int)(line - orgline));
printf("%u: (%%%02x) %d, line %d\n",
i, *orgline, rc, (int)(line - orgline));
}
}
@ -309,6 +432,12 @@ UNITTEST_START
static const char *nums[] = {
"777777777777777777777", /* 2^63 -1 */
"1000000000000000000000", /* 2^63 */
"111111111111111111111",
"222222222222222222222",
"333333333333333333333",
"444444444444444444444",
"555555555555555555555",
"666666666666666666666",
NULL
};
printf("Curl_str_octal / max\n");
@ -327,6 +456,24 @@ UNITTEST_START
static const char *nums[] = {
"7FFFFFFFFFFFFFFF", /* 2^63 -1 */
"8000000000000000", /* 2^63 */
"1111111111111111",
"2222222222222222",
"3333333333333333",
"4444444444444444",
"5555555555555555",
"6666666666666666",
"7777777777777777",
"888888888888888",
"999999999999999",
"aaaaaaaaAAAAAAA",
"bbbbbbbbBBBBBBB",
"BBBBBBBBbbbbbbb",
"ccccccccCCCCCCC",
"ddddddddDDDDDDD",
"eeeeeeeeEEEEEEE",
"ffffffffFFFFFFF",
"abcdef",
"ABCDEF",
NULL
};
printf("Curl_str_hex / max\n");