binyomen / binyomen/loxide

Increase the number of constants that can be stored in a chunk

Open
#5 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Rust
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

Currently, chunks can only store up to 256 constants. This can be increased by adding extended constant instructions which take more than a single byte as a constant index. See https://craftinginterpreters.com/chunks-of-bytecode.html:

> Because OP_CONSTANT uses only a single byte for its operand, a chunk may only contain up to 256 different constants. That’s small enough that people writing real-world code will hit that limit. We could use two or more bytes to store the operand, but that makes every constant instruction take up more space. Most chunks won’t need that many unique constants, so that wastes space and sacrifices some locality in the common case to support the rare case.
>
> To balance those two competing aims, many instruction sets feature multiple instructions that perform the same operation but with operands of different sizes. Leave our existing one-byte OP_CONSTANT instruction alone, and define a second OP_CONSTANT_LONG instruction. It stores the operand as a 24-bit number, which should be plenty.
>
> Implement this function:
>
> ```C
> void writeConstant(Chunk* chunk, Value value, int line) {
> // Implement me...
> }
> ```
>
> It adds value to chunk’s constant array and then writes an appropriate instruction to load the constant. Also add support to the disassembler for OP_CONSTANT_LONG instructions.
>
> Defining two instructions seems to be the best of both worlds. What sacrifices, if any, does it force on us?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at writeConstant(Chunk*, Value, int) and read the existing OP_CONSTANT handling and disassembler entry points. Implement the one-byte and 24-bit constant instruction paths described in the issue, then verify that the disassembler recognizes OP_CONSTANT_LONG and that chunks can load more than 256 constants.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.