Backstory

I wanted to build a sed-like regex replacement bot for my telegram chat. Using regex to detect the s/…/…/ pattern wasn’t very nice and I felt that it was overkill. So I created a mini finite state machine of sorts which I tried to make faster and faster. My friend was wondering how much faster my code was compared to regex. I created a ton of benchmarks and many different test strings. I benchmarked them all on all different variations to see what version would be fastest.

Once I had thought I created my fastest version, I tested it and to my surprise, one particular benchmark was much slower than the previous iterations, even though all the other tests were faster.

(best_5 in this was was the abnormally slower one)

I decided to try and find out what the issue was, so I started removing benchmarks to clean up the clutter, and it suddenly caused the function to become faster.

best_5 had miraculously become faster. I tried to create a minimal working (or breaking for this matter) code that experienced the same anomaly, but almost any small change I had caused the code to become fast. But the original code would always be slow.

Even altering unrelated code caused it to become fast.

Inlining

I had #[inline] on my function, and decided to see what would happen if I altered it.

no inline = fast

#[inline] = slow

= #[inline(always)] = fast

= fast #[inline(never)] = fast

This made no sense to me. But then again almost any other change to any function made it go fast. (except the removal of regex_0 benchmark)

I have no idea whats going on and I would love to find out

The code in question in its (slow) form: https://github.com/JuanPotato/what-the-heck-is-going-on

Video for the skeptical: https://youtu.be/T4CAL93vO_4

I’ve only gotten one friend that the code behaved the same for them. So I would like if you do test, please say what you experienced and what computer specs you have.

EDIT: I’m not sure what the best way would be to get asm output for the different builds and compare them, but I think that would be beneficial.