Git Product home page Git Product logo

Comments (8)

willglynn avatar willglynn commented on May 16, 2024

I got this working with a crude encoding scheme. Each isa::Instruction is identified with a single u8, followed by any operands (usually u32). As of now, I'm seeing a 10-15% performance improvement in cargo bench results.

The savings aren't as large as I had hoped. A napkin-math analysis over the test suite gives me a thought:

Bytes per instruction

Most of the u32 operands are slots or offsets or similar small integers. Maybe a variable length encoding scheme is worth a go?

from wasmi.

pepyakin avatar pepyakin commented on May 16, 2024

10-15% is a very good figure actually!

This is basically a trade-off and we can easily get more overhead than we save by compacting the bytecode. What we might want to explore is introduction of a special narrow versions of opcodes that takes, say, u16 instead of u32 immediates.

from wasmi.

willglynn avatar willglynn commented on May 16, 2024

Well… then I started thinking about how WebAssembly is already a compact bytecode format with variable-length encoding of operands. FunctionReader::read_instruction() swizzles around flow control, but many more instructions are validated then emitted 1:1. Maybe it's better not to produce a second bytecode, but to instead produce a mix of new/enriched flow control instructions along with references to the original bytecode.

from wasmi.

pepyakin avatar pepyakin commented on May 16, 2024

Maybe it's better not to produce a second bytecode, but to instead produce a mix of new/enriched flow control instructions along with references to the original bytecode.

Can you expand a little on this? I'm not sure I follow.

from wasmi.

willglynn avatar willglynn commented on May 16, 2024

This issue proposes decoding instructions for validation, re-encoding them to a new bytecode, and decoding that bytecode in the interpreter.

I'm now thinking about decoding instructions for validation, building &[u8]s of which instructions do not require special handling, and then decoding those instructions from the original bytecode again in the interpreter. Something like:

mod isa {
  pub enum Operation {   // formerly Instruction
    GetLocal(u32),
    SetLocal(u32),
    TeeLocal(u32),

    Br(Target),
    BrIfEqz(Target),
    BrIfNez(Target),
    BrTable(Box<[Target]>),
    Return(DropKeep),

    /// WebAssembly bytecode, guaranteed not to contain:
    ///
    ///  * `Nop`
    ///  * `Block`
    ///  * `Loop`
    ///  * `If`, `Else`, `End`
    ///  * `Br`, `BrIf`, `BrTable`
    ///  * `Return`
    ///  * `GetLocal`, `SetLocal`, `TeeLocal`
    ///
    /// All other instructions are permissible and have valid operands.
    ValidatedWasmBytecode(&[u8]),
  }
}

The existing bytecode format is equivalent for most instructions, and wasmi already knows how to decode it. It's denser than my u32 immediate format and thus ought to make better use of memory bandwidth. I intend to experiment in this direction :-)

from wasmi.

pepyakin avatar pepyakin commented on May 16, 2024

Related:

from wasmi.

Robbepop avatar Robbepop commented on May 16, 2024

I am going to implement a more compact encoding of wasmi bytecode for the v1 implementation introduced in #287.

from wasmi.

Robbepop avatar Robbepop commented on May 16, 2024

The mentioned PR (#287) shrank the size of the enum bytecode from 24 bytes to 16 bytes by disentangling BrTable variant. The BrCode operand now only hosts the number of targets excluding the default target and is followed by Branch and Return operands by construction of the wasmi bytecode.

Closing this issue now since we probably won't get way lower than that given that we embed constant values into the bytecode since indirectly accessing constants (that must be 64-bits) would create too much overhead.

from wasmi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.