Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Remove null value from Opcode #18

Closed
stoklund opened this issue Oct 28, 2016 · 4 comments
Closed

Remove null value from Opcode #18

stoklund opened this issue Oct 28, 2016 · 4 comments
Labels
E-easy Issues suitable for newcomers to investigate, including Rust newcomers!

Comments

@stoklund
Copy link
Contributor

After the Rust language designers carefully removed nulls from the language, I went and added my own. It turns out that Option<T> is often larger than T, and tables with 32-bit or smaller entries can double in size when using Option<T>.

C-like Opcodeenum

The Opcode enum in the generated opcodes.rs file contains an explicit null value:

pub enum Opcode {
    /// An invalid opcode.
    NotAnOpcode,
    /// `jump EBB, args`. (Jump)
    Jump,
    /// `brz c, EBB, args`. (Branch)
    /// Type inferred from `c`.
    Brz,
    ...
}

With the current opcodes, Opcode is a single byte, but Option<Opcode> is two bytes. We use the explicit null to indicate the empty slots in a precomputed hash table:

const OPCODE_HASH_TABLE: [Opcode; 128] = [
    Opcode::Imul,
    Opcode::Fsub,
    Opcode::IconcatLohi,
    Opcode::Sshr,
    Opcode::Fmaxnum,
    Opcode::NotAnOpcode,
    Opcode::Nearest,
    ...
}

This table would double in size if we used Option<Opcode>. Since this table is only used by the parser when mapping strings to opcodes, perhaps we should just bite the bullet and use an Option<Opcode> here. Hopefully, the Rust compiler will soon learn how to represent this sum type efficiently. It's one of the easier nested enum optimizations.

Encoding tables also use NotAnOpcode currently. These tables are more critical to keep small, but there may be an alternative representation of the empty slots.

@stoklund stoklund added the E-easy Issues suitable for newcomers to investigate, including Rust newcomers! label Oct 28, 2016
@eddyb
Copy link

eddyb commented Oct 30, 2016

cc rust-lang/rfcs#1230 (comment) - I now know what steps should be taken and I'm willing to mentor someone, but I can't take it onto myself for a while.

There was one hack added, in rust-lang/rust#37224, which means you can mark your first legitimate entry with = 1 and, at least in nightly, Option<Opcode> shouldn't take more than a byte or whatever.

@angusholder
Copy link
Contributor

I'd like to have a go at this if that's ok? I think I understand what needs doing

@stoklund
Copy link
Contributor Author

@angusholder That would be great!

@stoklund
Copy link
Contributor Author

This was fixed by @angusholder!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
E-easy Issues suitable for newcomers to investigate, including Rust newcomers!
Projects
None yet
Development

No branches or pull requests

3 participants