Skip to content

optimize fastfloat_strncasecmp - #356

Merged
lemire merged 2 commits into
fastfloat:mainfrom
sleepingeight:surya/opt-cmp
Jan 18, 2026
Merged

lemire merged 2 commits into
fastfloat:mainfrom
sleepingeight:surya/opt-cmp

Conversation

@sleepingeight

Copy link
Copy Markdown
Contributor

Related to #60.

Optimized fastfloat_strncasecmp function using SWAR and few little workarounds to reduce instruction count.

Benchmarks on Apple M1 Pro (arm) -

(Below results contain time taken per iteration)

$ ./bench --iterations=10000 --size=4000000
Old - 7518891ns
New - 3753442ns

$ ./bench --iterations=4000000 --size=8
Old - 23ns
New - 11ns

$ ./bench --iterations=4000000 --size=5
Old - 18ns
New - 14ns

$ ./bench --iterations=4000000 --size=3
Old - 13ns
New - 13ns

@sleepingeight

Copy link
Copy Markdown
Contributor Author

This is a generic implementation for all sized strings which is resulting in a 2x performance for large sized strings, but the codebase is mostly comparing with small strings of lengths 3, 8. Small string cases have more scope of optmisation with loop unrolling.

@lemire requesting a review.

Comment thread include/fast_float/float_common.h Outdated
for (size_t i = 0; i < length; i += sz) {
val1 = val2 = 0;
sz = std::min(sz, length - i);
::memcpy(&val1, actual_mixedcase + i, sz * sizeof(UC));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this use of memcpy likely breaks the constexpr qualifier. (Easy to fix.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've modified the function to use memcpy only during run time. PTAL.

@lemire

lemire commented Dec 29, 2025

Copy link
Copy Markdown
Member

@sleepingeight This is likely fine, but does it help our performance ?

@sleepingeight

Copy link
Copy Markdown
Contributor Author

@lemire I've added few more optmizations - specialised functions for lengths 3 and 5 with loop unrolling. Results -

Old:

> ./build/benchmarks/realbenchmark
# reading /Users/s/ff/fast_float/build/benchmarks/data/canada.txt
#### 
# read 111126 lines 
ASCII volume = 1.93374 MB 
fastfloat (64)                          :  1036.68 MB/s (+/- 1.0 %)    59.57 Mfloat/s  
fastfloat (32)                          :  1067.48 MB/s (+/- 1.5 %)    61.34 Mfloat/s  
UTF-16 volume = 3.86749 MB 
fastfloat (64)                          :  1886.08 MB/s (+/- 3.1 %)    54.19 Mfloat/s  
fastfloat (32)                          :  1918.99 MB/s (+/- 2.7 %)    55.14 Mfloat/s  
#### 
# reading /Users/s/ff/fast_float/build/benchmarks/data/mesh.txt
#### 
# read 73019 lines 
ASCII volume = 0.536009 MB 
fastfloat (64)                          :   827.87 MB/s (+/- 1.9 %)   112.78 Mfloat/s  
fastfloat (32)                          :   720.52 MB/s (+/- 1.0 %)    98.15 Mfloat/s  
UTF-16 volume = 1.07202 MB 
fastfloat (64)                          :  1572.07 MB/s (+/- 0.5 %)   107.08 Mfloat/s  
fastfloat (32)                          :  1371.82 MB/s (+/- 1.9 %)    93.44 Mfloat/s  

New:

> ./build/benchmarks/realbenchmark 
# reading /Users/s/fast_float/build/benchmarks/data/canada.txt
#### 
# read 111126 lines 
ASCII volume = 1.93374 MB 
fastfloat (64)                          :  1032.96 MB/s (+/- 1.6 %)    59.36 Mfloat/s  
fastfloat (32)                          :  1068.47 MB/s (+/- 0.9 %)    61.40 Mfloat/s  
UTF-16 volume = 3.86749 MB 
fastfloat (64)                          :  1898.19 MB/s (+/- 0.8 %)    54.54 Mfloat/s  
fastfloat (32)                          :  1919.27 MB/s (+/- 1.0 %)    55.15 Mfloat/s  
#### 
# reading /Users/s/fast_float/build/benchmarks/data/mesh.txt
#### 
# read 73019 lines 
ASCII volume = 0.536009 MB 
fastfloat (64)                          :   820.95 MB/s (+/- 1.1 %)   111.84 Mfloat/s  
fastfloat (32)                          :   714.48 MB/s (+/- 1.3 %)    97.33 Mfloat/s  
UTF-16 volume = 1.07202 MB 
fastfloat (64)                          :  1566.32 MB/s (+/- 0.6 %)   106.69 Mfloat/s  
fastfloat (32)                          :  1361.51 MB/s (+/- 0.7 %)    92.74 Mfloat/s  

Although benchmarking only the fastfloat_strncasecmp function is providing good speedups, it is not being reflected in the benchmarks. I'll look into this in more detail. But when tested on data containing only nan, infinity, inf, the results are slightly better and are as follows -

Old:

> ./build/benchmarks/realbenchmark nanif.txt 
# reading nanif.txt
#### 
# read 111100 lines 
ASCII volume = 0.493298 MB 
fastfloat (64)                          :   486.79 MB/s (+/- 2.9 %)   109.63 Mfloat/s  
fastfloat (32)                          :   486.63 MB/s (+/- 1.7 %)   109.60 Mfloat/s  
UTF-16 volume = 0.986595 MB 
fastfloat (64)                          :   950.55 MB/s (+/- 3.2 %)   107.04 Mfloat/s  
fastfloat (32)                          :   965.52 MB/s (+/- 2.2 %)   108.73 Mfloat/s  

New:

> ./build/benchmarks/realbenchmark nanif.txt 
# reading nanif.txt
#### 
# read 111100 lines 
ASCII volume = 0.493298 MB 
fastfloat (64)                          :   513.58 MB/s (+/- 2.3 %)   115.67 Mfloat/s  
fastfloat (32)                          :   515.69 MB/s (+/- 1.1 %)   116.14 Mfloat/s  
UTF-16 volume = 0.986595 MB 
fastfloat (64)                          :  1045.17 MB/s (+/- 1.2 %)   117.70 Mfloat/s  
fastfloat (32)                          :  1046.88 MB/s (+/- 1.0 %)   117.89 Mfloat/s  

@lemire

lemire commented Dec 30, 2025

Copy link
Copy Markdown
Member

@sleepingeight If you can, I recommend running the benchmarks in privileged mode (sudo) under macOS or Linux.

@sleepingeight

sleepingeight commented Dec 30, 2025 •

Copy link
Copy Markdown
Contributor Author

Sure @lemire, here are the results on Apple Mac M1 Pro (10Cores)

Old:

> sudo ./build/benchmarks/realbenchmark
# Using hardware counters
#### 
# reading /Users/devrev/ff/fast_float/build/benchmarks/data/canada.txt
#### 
# read 111126 lines 
ASCII volume = 1.93374 MB 
fastfloat (64)                          :  1036.37 MB/s (+/- 2.1 %)    59.56 Mfloat/s      17.99 i/B   328.30 i/f (+/- 0.0 %)      2.97 c/B    54.23 c/f (+/- 1.2 %)      6.05 i/c     59.81 b/f    107.01 bm/f      3.23 GHz 
fastfloat (32)                          :  1066.62 MB/s (+/- 5.0 %)    61.30 Mfloat/s      17.61 i/B   321.31 i/f (+/- 0.1 %)      2.89 c/B    52.70 c/f (+/- 2.6 %)      6.10 i/c     58.34 b/f    100.38 bm/f      3.23 GHz 
UTF-16 volume = 3.86749 MB 
fastfloat (64)                          :  1884.36 MB/s (+/- 2.9 %)    54.14 Mfloat/s       9.74 i/B   355.52 i/f (+/- 0.0 %)      1.64 c/B    59.67 c/f (+/- 1.8 %)      5.96 i/c     58.82 b/f    106.68 bm/f      3.23 GHz 
fastfloat (32)                          :  1917.33 MB/s (+/- 2.1 %)    55.09 Mfloat/s       9.55 i/B   348.54 i/f (+/- 0.0 %)      1.61 c/B    58.64 c/f (+/- 1.4 %)      5.94 i/c     57.33 b/f     98.62 bm/f      3.23 GHz 
#### 
# reading /Users/devrev/ff/fast_float/build/benchmarks/data/mesh.txt
#### 
# read 73019 lines 
ASCII volume = 0.536009 MB 
fastfloat (64)                          :   828.29 MB/s (+/- 1.9 %)   112.84 Mfloat/s      25.83 i/B   198.83 i/f (+/- 0.0 %)      3.72 c/B    28.67 c/f (+/- 1.3 %)      6.93 i/c     41.03 b/f     84.50 bm/f      3.24 GHz 
fastfloat (32)                          :   719.84 MB/s (+/- 1.6 %)    98.06 Mfloat/s      28.45 i/B   218.96 i/f (+/- 0.0 %)      4.28 c/B    32.97 c/f (+/- 1.4 %)      6.64 i/c     42.78 b/f     88.04 bm/f      3.23 GHz 
UTF-16 volume = 1.07202 MB 
fastfloat (64)                          :  1575.24 MB/s (+/- 0.7 %)   107.30 Mfloat/s      13.59 i/B   209.27 i/f (+/- 0.0 %)      1.96 c/B    30.14 c/f (+/- 0.6 %)      6.94 i/c     40.65 b/f     86.44 bm/f      3.23 GHz 
fastfloat (32)                          :  1369.70 MB/s (+/- 2.0 %)    93.30 Mfloat/s      14.90 i/B   229.40 i/f (+/- 0.0 %)      2.25 c/B    34.65 c/f (+/- 1.2 %)      6.62 i/c     42.41 b/f     86.99 bm/f      3.23 GHz 

New:

> sudo ./build/benchmarks/realbenchmark
# Using hardware counters
#### 
# reading /Users/devrev/fast_float/build/benchmarks/data/canada.txt
#### 
# read 111126 lines 
ASCII volume = 1.93374 MB 
fastfloat (64)                          :  1032.71 MB/s (+/- 4.5 %)    59.35 Mfloat/s      18.05 i/B   329.30 i/f (+/- 0.1 %)      2.98 c/B    54.43 c/f (+/- 1.8 %)      6.05 i/c     60.82 b/f    108.00 bm/f      3.23 GHz 
fastfloat (32)                          :  1066.53 MB/s (+/- 3.5 %)    61.29 Mfloat/s      17.66 i/B   322.31 i/f (+/- 0.0 %)      2.89 c/B    52.69 c/f (+/- 1.9 %)      6.12 i/c     59.32 b/f     98.92 bm/f      3.23 GHz 
UTF-16 volume = 3.86749 MB 
fastfloat (64)                          :  1901.19 MB/s (+/- 4.5 %)    54.63 Mfloat/s       9.77 i/B   356.52 i/f (+/- 0.0 %)      1.62 c/B    59.15 c/f (+/- 2.3 %)      6.03 i/c     59.83 b/f    107.47 bm/f      3.23 GHz 
fastfloat (32)                          :  1924.20 MB/s (+/- 3.0 %)    55.29 Mfloat/s       9.58 i/B   349.54 i/f (+/- 0.0 %)      1.60 c/B    58.44 c/f (+/- 1.6 %)      5.98 i/c     58.33 b/f     98.15 bm/f      3.23 GHz 
#### 
# reading /Users/devrev/fast_float/build/benchmarks/data/mesh.txt
#### 
# read 73019 lines 
ASCII volume = 0.536009 MB 
fastfloat (64)                          :   821.42 MB/s (+/- 1.3 %)   111.90 Mfloat/s      25.96 i/B   199.83 i/f (+/- 0.0 %)      3.76 c/B    28.91 c/f (+/- 1.1 %)      6.91 i/c     42.03 b/f     83.52 bm/f      3.23 GHz 
fastfloat (32)                          :   714.44 MB/s (+/- 2.1 %)    97.33 Mfloat/s      28.58 i/B   219.96 i/f (+/- 0.0 %)      4.32 c/B    33.22 c/f (+/- 1.5 %)      6.62 i/c     43.78 b/f     87.93 bm/f      3.23 GHz 
UTF-16 volume = 1.07202 MB 
fastfloat (64)                          :  1566.80 MB/s (+/- 1.8 %)   106.72 Mfloat/s      13.66 i/B   210.27 i/f (+/- 0.0 %)      1.97 c/B    30.30 c/f (+/- 1.5 %)      6.94 i/c     41.66 b/f     83.92 bm/f      3.23 GHz 
fastfloat (32)                          :  1361.29 MB/s (+/- 2.1 %)    92.72 Mfloat/s      14.97 i/B   230.40 i/f (+/- 0.0 %)      2.27 c/B    34.88 c/f (+/- 1.6 %)      6.61 i/c     43.41 b/f     89.04 bm/f      3.23 GHz 

Results on new dataset (containing only nan, infinity, inf) -
Old:

> sudo ./build/benchmarks/realbenchmark nanif.txt 
# Using hardware counters
#### 
# reading nanif.txt
#### 
# read 111100 lines 
ASCII volume = 0.493298 MB 
fastfloat (64)                          :   487.67 MB/s (+/- 3.0 %)   109.83 Mfloat/s      26.72 i/B   124.42 i/f (+/- 0.0 %)      6.32 c/B    29.43 c/f (+/- 1.9 %)      4.23 i/c     31.36 b/f    691.50 bm/f      3.23 GHz 
fastfloat (32)                          :   488.19 MB/s (+/- 2.7 %)   109.95 Mfloat/s      26.72 i/B   124.42 i/f (+/- 0.0 %)      6.31 c/B    29.40 c/f (+/- 2.2 %)      4.23 i/c     31.36 b/f    690.56 bm/f      3.23 GHz 
UTF-16 volume = 0.986595 MB 
fastfloat (64)                          :   949.75 MB/s (+/- 3.3 %)   106.95 Mfloat/s      14.90 i/B   138.73 i/f (+/- 0.0 %)      3.25 c/B    30.22 c/f (+/- 3.0 %)      4.59 i/c     30.36 b/f    690.71 bm/f      3.23 GHz 
fastfloat (32)                          :   964.14 MB/s (+/- 1.5 %)   108.57 Mfloat/s      14.90 i/B   138.73 i/f (+/- 0.0 %)      3.20 c/B    29.77 c/f (+/- 1.3 %)      4.66 i/c     30.35 b/f    693.79 bm/f      3.23 GHz 

New:

> sudo ./build/benchmarks/realbenchmark nanif.txt 
# Using hardware counters
#### 
# reading nanif.txt
#### 
# read 111100 lines 
ASCII volume = 0.493298 MB 
fastfloat (64)                          :   512.85 MB/s (+/- 1.4 %)   115.50 Mfloat/s      24.22 i/B   112.78 i/f (+/- 0.0 %)      6.01 c/B    27.99 c/f (+/- 1.0 %)      4.03 i/c     26.03 b/f    692.99 bm/f      3.23 GHz 
fastfloat (32)                          :   514.63 MB/s (+/- 1.4 %)   115.91 Mfloat/s      24.22 i/B   112.79 i/f (+/- 0.0 %)      5.99 c/B    27.89 c/f (+/- 1.1 %)      4.04 i/c     26.03 b/f    689.28 bm/f      3.23 GHz 
UTF-16 volume = 0.986595 MB 
fastfloat (64)                          :  1044.89 MB/s (+/- 1.9 %)   117.66 Mfloat/s      13.11 i/B   122.12 i/f (+/- 0.0 %)      2.95 c/B    27.47 c/f (+/- 1.4 %)      4.45 i/c     25.36 b/f    688.18 bm/f      3.23 GHz 
fastfloat (32)                          :  1045.68 MB/s (+/- 2.5 %)   117.75 Mfloat/s      13.11 i/B   122.12 i/f (+/- 0.0 %)      2.95 c/B    27.44 c/f (+/- 2.0 %)      4.45 i/c     25.37 b/f    688.19 bm/f      3.23 GHz 

@lemire

lemire commented Dec 30, 2025

Copy link
Copy Markdown
Member

@sleepingeight Right, so with the new numbers (instructions and so forth), the results are clearer.

@sleepingeight

Copy link
Copy Markdown
Contributor Author

@lemire Is this PR in the right direction or does it require any improvements?

@lemire

lemire commented Jan 2, 2026

Copy link
Copy Markdown
Member

@sleepingeight We'll merge this, but I like to let PR sit to give people a chance to comment.

@lemire
lemire merged commit babb1f3 into fastfloat:main Jan 18, 2026
if ((actual_mixedcase[i] | 32) != expected_lowercase[i]) {
return false;
}
return true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exit after 1 cycle.

if ((actual_mixedcase[i] | 32) != expected_lowercase[i]) {
return false;
}
return true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also exit after 1 cycle.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for the overlook, will raise a fix PR shortly.

@IRainman

Copy link
Copy Markdown

Needs to be fixed.

@IRainman

Copy link
Copy Markdown

Sorry for delay. I''ll not tested it before it's merged to the upstream.

mikedld pushed a commit to transmission/fast_float that referenced this pull request Sep 22, 2026
PR fastfloat#356 replaced the 3/5-character case-insensitive compare used by
parse_infnan with SWAR helpers (fastfloat_strncasecmp3/5 plus a generic
memcpy-based fastfloat_strncasecmp). That code only runs for "nan",
"inf" and "infinity" inputs, so it cannot help ordinary parsing, but it
grew the inlined inf/nan tail of the parser: on x86-64 (GCC 14 and
clang 21) the per-commit benchmarks dropped by 1.5-2.5% because the
extra dead code changed inlining decisions and register allocation on
the hot path (+7 instructions/float on canada.txt).

Go back to the plain loop, which the compiler unrolls for the constant
lengths 3 and 5, and drop the now-unused generic SWAR function. The
`actual < 256` guard of the original is unnecessary: OR-ing 0x20 into
a value can only equal an ASCII lowercase letter when the value is that
letter or its uppercase form, so the plain OR is exact for every
character type.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants