Anyone who's been writing rust for a little bit will notice the String clone like a sore thumb. The "go-to-perf-guide" also very explicitly states than clones on String/Vec/Box etc will cause an allocation and slow down your code.
https://gist.github.com/jFransham/369a86eff00e5f280ed2512145...
In every one of these micro benchmarks, the comments will then show that by removing the alloc, the Rust version ends up much much faster.
String cloning is one of those things you learn to avoid early on, and any time you type "clone" it should be obvious that there's a better way to write your algorithm.
* There is no use in first pop()ing a candidate from the "reacts" list and then potentially re-pushing them later. Getting a pointer to the last element, and removing it if it is not needed, is more efficient.
* Reordering the character equality condition could help a tiny bit since the inequality check should be faster than the ignore case check.
I think the given ordering is better: in a conjuction, you typically put the less likely condition first, not the cheaper one.
Out of curiosity, I just did a small benchmark (criterion) using the input.txt provided, and reordering results in a 10% difference:
Old Order time:[307.28 us 309.12 us 311.17 us]
Reversed time:[270.82 us 271.18 us 271.54 us]
This feels weird since the input.txt contains few pairs of equal characters, so there is likely something else going on. The inequality is very inexpensive but the ignore case compare should not be that expensive either.Micro benchmarking remains a fickle beast :)
With a couple changes you could probably eliminate all allocations after the initial one.