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

compiler/wasm: memoize in function (not callsite) #3169

Conversation

srenatus
Copy link
Contributor

@srenatus srenatus commented Feb 19, 2021

As soon as a function is called twice, there should be a little bit
to gain from having the memoization happen in the function body.

Run on a big bundle, this results in the following change in opcode
counts:

    Total opcodes: 184881 -> 183852

    Opcode counts:
    local.get: 46920 -> 46803 -
    i32.const: 24152 -> 23708 -
    local.set: 14097 -> 14097 =
    call:      12547 -> 12103 -
    end:       10911 -> 11016 +
    br_if:     11062 -> 10840 -
    block:      8987 ->  9092 +

So, the change isn't dramatic at all. But less is more, so I guess
we could still do this.

@srenatus srenatus force-pushed the sr/wasm/memoize-in-function-not-callsite branch from 893bba2 to d85df7f Compare February 19, 2021 16:26
@srenatus srenatus marked this pull request as draft February 19, 2021 16:27
@srenatus
Copy link
Contributor Author

😬 forgot about something: the args length check.

@srenatus srenatus force-pushed the sr/wasm/memoize-in-function-not-callsite branch 3 times, most recently from 1fee90a to 8da631c Compare February 19, 2021 16:36
@srenatus srenatus marked this pull request as ready for review February 19, 2021 16:36
@srenatus
Copy link
Contributor Author

Also forgot that we can drop the memo around call_indirect, and thereby the elem_to_func mechanism. ✨

@srenatus srenatus force-pushed the sr/wasm/memoize-in-function-not-callsite branch 2 times, most recently from b1ed3f6 to d0191f1 Compare February 22, 2021 13:54
@srenatus
Copy link
Contributor Author

✔️ The call_indirect path should be taken care of now. As an implication, we'll drop the extra data section for mapping elem idx -> func idx, together with the lookup function. So, the overall module gets a little lighter.

As soon as a function is called twice, there should be a little bit
to gain from having the memoization happen in the function body.

Run on a big bundle, this results in the following change in opcode
counts:

    Total opcodes: 184881 -> 183719

    Opcode counts:
    local.get: 46920 -> 46760 -
    i32.const: 24152 -> 23706 -
    local.set: 14097 -> 14097 =
    call:      12547 -> 12061 -
    end:       10911 -> 11015 +
    br_if:     11062 -> 10826 -
    block:      8987 ->  9092 +

So, the change isn't dramatic at all. But less is more, so I guess
we could still do this.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
@srenatus srenatus force-pushed the sr/wasm/memoize-in-function-not-callsite branch from d0191f1 to ac39e2f Compare February 22, 2021 14:05
c.appendInstr(instruction.GetLocal{Index: c.local(fn.Return)})
c.appendInstr(instruction.Call{Index: c.function(opaMemoizeInsert)})
}
c.appendInstr(instr)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 Currently, if this instruction is Return, we could just ignore it. The last value on the stack is the function's return value, with our without that statement.

Copy link
Member

@tsandall tsandall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. It's nice how this cuts down the amount of code in the backend because we don't have to special case CallStmt vs CallDynamicStmt.

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

Successfully merging this pull request may close these issues.

2 participants