Optimize memcpy and other standard memory manipulating functions
If my program uses memcpy, in java (byte)code I see this implementation:
private int memcpy(int var1, int var2, int var3) {
if (var3 != 0) {
int var4 = var1;
do {
this.memory.put(var4, (byte)Byte.toUnsignedInt(this.memory.get(var2)));
++var2;
++var4;
} while((var3 += -1) != 0);
}
return var1;
}
But it can be implemented in much more efficient way by using ByteBuffer API:
private int memcpy(int src, int dst, int len) {
if (len != 0) {
ByteBuffer srcBuf = memory.duplicate();
srcBuf.position(src);
srcBuf.limit(src + len);
ByteBuffer dstBuf = memory.duplicate();
dstBuf.position(dst);
dstBuf.put(srcBuf);
}
return src;
}
With this implementation the real native memcpy will be used (if memory is DirectByteBuffer).
We can detect functions like memcpy by their names, signatures and bodies, and then replace them with more efficient implementation.
I am not wanting to go down the path of optimizing libc calls. Some things don't even have libc (e.g. Go) and functions like memcpy are not guaranteed to come over the same every time. There are plenty of other WASM situations where bulk memory operations can be improved, not just memcpy. At this point, of that's really important, I'd recommend a post processor use ASM and change the source. Or if there is a good spot for me to make the current code more extensible so a caller can do this with some kind of separate "contrib" project, I will be happy to add those extension hooks.
I really want to only support WASM without library-specific pattern catching/optimization. I'm afraid this will instead have to wait for https://github.com/WebAssembly/bulk-memory-operations and then clang to call it.
EDIT: After more thought, I might be open to pattern matching a bulk get/put set of ops, not memcpy specific or at the whole function level, but if it is generic enough we might be able to do it. But I would rather wait for bulk mem ops.