This problem was solved under terms of
Field_string::cmp() seems to do unnecessary work trimming trailing spaces:
In absolute majority cases, the difference between strings is found in the very beginning of the compared strings. So doing charpos() on the two arguments, before passing them to the actual comparison function, looks like an inefficient waste of CPU.
A better approach would be to implement a new comparison function with this tentative API:
Internally, the exact virtial implementations of strnncollsp_nchars() would do the same with what strnncollsp() do in the same collation, but with an extra limit on "nchars".
This new function should also help to fix a bug in the similar code in InnoDB: see
MDEV-25440 for details.