Reverse match of binary?

I recently opened a very small PR to phoenix that would limit the number of extra bytes generated when using mix phx.gen.secret. Ultimately it was not merged, but José commented that:

i usually do the reverse match as well on high performant code, but for this script, I think going with the most readable approach is good enough

Here is a link for the full context:

I’ve tried looking up “reverse match” with regards to binary pattern matching, but can’t find anything. Can anyone explain what it means or offer a resource that does? Thank you!

1 Like

Just a guess, but pattern matching on the end of a binary can be done by reversing the binary first and matching on the result.

Hmm, could be. I did briefly consider that.
In this case, this particular match is always done from the front.
Plus, it seems like reversing the binary would be expensive (and, naively, the match would need to be reversed again), while using binary_part/3 just requires computing the offsets and means the VM could just copy the relevant parts of the binary out, but I don’t really have sufficient knowledge of the VM to know that’s the case.

I usually did this by simply accumulating the head part I don’t need and start matching and doing appropriate actions when I find the start of what I look for.

Give an example and I’m willing to post code showing how I usually approach this. is the entirety of the context around reverse match that I have. :man_shrugging: