Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize memcpy and other standard memory manipulating functions #22

Open
vlad20012 opened this issue Oct 5, 2018 · 1 comment
Open

Comments

@vlad20012
Copy link
Contributor

vlad20012 commented Oct 5, 2018

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.

@cretz
Copy link
Owner

cretz commented Oct 5, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants