Ambyte: checked narrowing of a byte value to a smaller literal bound

Open
#437 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
38/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
compilers

Research direction

Start with the language typing and static charge logic in fold_work.rs, then trace the matching rules in Echo’s lowerer and verifier and the runtime charge. The issue’s RED test is the guarded slice; add coverage for the stated narrow(a, 57) type, bound independent charge, and 58-byte input. Done means those checks pass and the jedit#410 rope package records the before and after U measurements.

Written by the indexing model from the issue text.

Description

enhancement release:target-ir type: goalpost
.dag: issue-dag/2
.id: ambyte-lang/ambyte#437
.kind: task
.type: feature
.blocks: [flyingrobots/jim#5]
.size_hours: 6

Outcome

Source can narrow a byte value to a smaller literal bound with a check, for example narrow(b, 57) -> Option<Bytes<max=57>>, which is none when len(b) > 57. The narrowing costs a constant plus at most the new bound, never the old one, and the value's later uses are charged at the new bound.

Why

There is no way in the language to do this today:

  • slice keeps its input's bound.
  • if len(a) <= 57 then slice(a, 0, len(a)) else b"" is refused with "conditional branches do not match the expected type".
  • Copying octet by octet into Bytes<max=57> passes the 4,352-byte atom on every at, so it costs more than it saves.

Under amblicit main (a0542b53, with #424 and #432), every use of a Bytes<max=4352> local as a call argument is still charged at 4,352 bytes. A canonical Branch fact is at most 57 bytes.

Projected savings, measured by simulation

jedit#410 at 9e5863d: static U is 83,964,734 steps and 173,660,515 bytes. I replaced the two branchFact call sites (the closure selector and loadStep) with a parser typed on Bytes<max=57>, fed by a stub that costs one atom use. U fell to 77,883,198 steps and 167,161,699 bytes: 6,081,536 steps (7.2%) and 6,498,816 bytes (3.7%) saved.

Scope

  • A builtin or a checked form, its typing, the static charge rule in fold_work.rs, and the matching rule in Echo's lowerer and verifier, plus the runtime charge.
  • Not in scope: narrowing whose bound is not a literal.

Test Plan

  • RED: the guarded slice above is refused.
  • GREEN: narrow(a, 57) type-checks as Option<Bytes<max=57>>; its charge is independent of a's bound; it is none for a 58-byte input.
  • Rebuild jedit#410's rope package with branchFact on the narrowed value and record U before and after.
Dominant language
Rust
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from flyingrobots/edict

All issues in flyingrobots/edict

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.