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

Cost tracking in wasm #344

Open
krl opened this issue Feb 19, 2024 · 4 comments
Open

Cost tracking in wasm #344

krl opened this issue Feb 19, 2024 · 4 comments
Assignees
Labels

Comments

@krl
Copy link
Collaborator

krl commented Feb 19, 2024

wasm cost differences

All the type checking happens at compile time, which makes runtime costs based on type checking redundant.

wasm compiled contracts

Compiling the cost contract into webassembly, and dynamically linking it seems like a possibility, however calling into the cost contract at each call, each arithmetic operation seems expensive, and might completely negate any gains from compilation in the first place.

I see a few alternatives.

Instead of calling into the cost contract at each point, global counters are incremented as we execute, and then all called into the cost contract at the end of the execution, or more often than that (for example, as functions return)

This would be more performant in the good path, but could lead to larger rollbacks in the bad cases.

Keep globals of the running amount for each operation, this would neccesitate around 22 globals, and we could use the current cost contract as is. If we assume we can use 32 bit values for these, that might be acceptabe.

Another alternative is to use globals directly for runtime/read/write, tallying up the costs as we run, which would make it neccesary to hardcode the values in the compiled contract itself, which would neccesitate re-complilation of contracts at cost function upgrades. This might be acceptable, since the cached compiled contracts could just be invalidated and re-compiled as they are called.

creating a new cost tracking logic from scratch

Also what we've discussed, is to create a new cost tracking with specifically wasm in mind. This would also make unnecesary any 1-to-1 matching with the old cost calculations (that are unfortunately not very well covered by tests).

In this case we would need to have a meeting to figure out exactly what problem it is we want to solve, and how it differs from the interpreted case.

@smcclellan smcclellan added this to the WASM Phase 1 milestone Mar 12, 2024
@krl
Copy link
Collaborator Author

krl commented Mar 20, 2024

After taking discussions and future-compatability into account, I now believe the best way forward is to use the built-in metering fuel of wasmtime for the runtime cost calculations.

This has several advantages,

  1. It simplifies the implementation and optimization of the code, since this is an approach based on compiler middleware that has already been through optimization.
  2. It removes the restriction of using only clarity as the input language, and allows running wasm from other sources, such as compatability layers, without having to change the code.
  3. It is more accurate to measure on the wasm instruction level rather than the clarity level, since for example shortcuts in multiplication with smaller numbers will consume less fuel, whereas in clarity a large and a small multiplication costs the same.

Note that this is for the runtime cost only, for the read/write count of bytes it makes more sense to do it at the linker level, i.e when the code calls out to the host, which is also compatible with point 2 above.

@krl
Copy link
Collaborator Author

krl commented Mar 26, 2024

i know you're busy now, but could you have a look @obycode ?

@krl krl removed their assignment Mar 26, 2024
@smcclellan
Copy link
Collaborator

@krl Is this in progress? I'll assign it to you until it's complete. When it's in In Review stage, you'll keep the assignment as you own it getting reviewed.

@smcclellan
Copy link
Collaborator

Blocked on #409 (removing circular dependencies in code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: 🚫 Blocked
Development

No branches or pull requests

4 participants