These date back all the way to the 8086/8088. They were the fastest way to do string operations on those early CPUs, but I don't think this the case on modern CPUs.
https://www.google.com/search?q=intel+rep+prefix
http://wiki.osdev.org/X86-64_Instruction_Encoding#REPNE.2FRE...
https://courses.engr.illinois.edu/ece390/archive/spr2002/boo...
Good point about the code size. I imagine there are likely to be cases where that would let some algorithm run faster overall because it fits in the instruction cache, even if the string operation considered on its own is slower.
Modern CPUs still have a fast path for string operations, and I recall hearing that they even had some improvements not too long ago (in Sandy Bridge or other recent arch).
CPUs may be smart enough to detect a memcpy done with a loop, but REPxx is the preferred way - even on modern CPUs.
It's a microcode loop. Here is the appropriate pseudocode (this is extracted from the Intel manuals):
http://qcd.phys.cmu.edu/QCDcluster/intel/vtune/reference/vc2...
[1]: https://github.com/kelseyhightower/echo/blob/53d84ea4e79db3d...