Repository navigation
Description
Having finished a first iteration of the encoder & decoder for the spec, I'd like to make a couple of small suggestions regarding the structure of sections in the binary. Lumping the together here.
Section Headers
Instead of
(payload_size; name_string; payload)
as the overall section structure, can we swap that to
(name_string; payload_size; payload)
? Two minor advantages:
- It makes the offset computation slightly less confusing.
- It allows simply viewing a section as a pair
(name_string; payload_string),
which is particularly natural wrt handling and skipping unknown sections.
With that change, skipping over sections requires skipping over their name first. But I have a hard time imagining a reason for skipping a section without even knowing what it is.
Section Names
Honestly, the current names are super verbose. Would anybody be opposed to shorten them a bit, to something nicer? I'd suggest:
"signatures" -> "types"
"import_table" -> "imports"
"function_signatures" -> "functions"
"export_table" -> "exports"
"start_function" -> "start"
"function_bodies" -> "code"
"data_segments" -> "data"
Note that the signatures section may be generalised to contain other kinds of type definitions in the future, so the current name is not a good fit.
Function Bodies
Function bodies are implicitly delimited by the byte size of their encoding. This is unfortunate for a couple of reasons:
- The expression decoder cannot just operate on an abstract stream, it has to take this secondary end-of-stream condition into account. The spec currently handles this by a somewhat ad-hoc notion of "substream", but really, this pierces the stream abstraction in an ugly manner.
- This is the only piece of the binary format that stands in the way of formulating the entire format unambiguously as a grammar. Because it depends on non-local (and lower-level) context information. I find it rather sad to get 99% there but lose it on the last meter.
I'd hence like to suggest adding an explicit end opcode to functions. Pros:
- The binary format is fully structured and can be entirely parsed linearly from an abstract byte stream, without paying attention to any size information.
- Sizes would only be needed to (a) seek through the stream if desired, (b) validating that they are consistent. That decouples concerns nicely.
I'm aware that there are concerns that the extra byte is "redundant", but is one byte per function a big deal? We recently saved much more by improving the representation of locals (or would by shortening section names :) ).
Activity
wanderer commentedon Mar 24, 2016
If I understand correctly the whole string is going into the binary. Could each section type be mapped to an
intand represented with a single byte?If you have an end op could you drop the function body size? Could you reuse the
endsection instead of a new op?kripken commentedon Mar 24, 2016
+1 to all of these.
For the last one, we'd be adding an extra byte, but removing the varuint32 for "body size", shouldn't that be an overall win? 1 byte vs. 1+.
binji commentedon Mar 24, 2016
Agreed, I found it strange that the size of the section occurs before the section name as well.
+1. These are the basic sections that all modules will have, let's make them succinct.
OK with all of these except maybe
functionsandcode. Don't have better ideas though.+1
binji commentedon Mar 24, 2016
This is how it used to be, but was changed to allow extending the format later with new section types.
The postorder format already adds an
endop, so we'd just reuse it there, I think.jfbastien commentedon Mar 24, 2016
Does
name_stringinclude the size of the string? The purpose it to be able to skip sections with unknown names, and since they're not null-terminated we need a size. Reordering sounds fine to me.kripken commentedon Mar 24, 2016
For fast skipping of function bodies, we could allow multiple function sections. However, I'm not sure we need to focus on optimizing skipping of bodies? Skipping entire sections is useful, but skipping bodies seems unnecessary given we now have signatures beforehand anyhow. Normal VM operation won't want to skip function bodies.
binji commentedon Mar 24, 2016
@kripken we'll want to quickly skip function bodies for parallel decoding.
kripken commentedon Mar 24, 2016
Oh, right, thanks.
sunfishcode commentedon Mar 25, 2016
Section names: it's a bikeshed of course, but I prefer the more descriptive names. In particular, "functions", "types", and "code" seem likely to confuse.
lukewagner commentedon Mar 28, 2016
+1 That is actually how I implemented it initially in SM and I also found the switch to make the code a bit grosser.
Hah, this was also on my personal "tidy-up todo".
+1. Symmetric to this, there's also an implicit dependency on the whole module's byte size in the current proposal in #597 that we'd probably want to replace with, perhaps, an up-front section count (after magic+version, before first section).
titzer commentedon Mar 29, 2016
On Thu, Mar 24, 2016 at 3:36 PM, rossberg-chromium <[email protected]
Ok by me.
+1
rossberg commentedon Apr 5, 2016
[Back from vacation.]
It seems there is mostly consensus for the first two suggestions, and sympathy for the latter. I'll create separate PRs for each suggestion, and we can iterate on the details there.
@binji, I picked "code" and "data" to mirror the usual code/data dichotomy familiar from other binaries. Not attached to that, though.
@jfbastien, yes, "string" is supposed to denote a (length, bytes) pair.
@kripken, as others pointed out, we still have use for the function size.
@JSStats, regarding the types vs signatures section, we already decided at the Feb summit to make the function type constructor explicit to support extensibility. It's just that nobody got around turning that one into a PR yet. I'll do.
@titzer, the size bound introduces a secondary end-of-stream condition. With actual streaming, you cannot know upfront which end-of-stream condition will fire first (as the size value might be invalid), so you always have to check against both.
jfbastien commentedon Dec 15, 2016
I think this is long done. @rossberg-chromium please reopen if that's not the case.