Advanced

Crossing the boundary

Across the boundary there are integers. Everything else is a shape both sides agree to, and the shape is chosen for its cost rather than its clarity.

What you will see

One line. A layout version is the thing that makes a mismatch loud.

About 1 min of reading

Try It

Count crossings and items

1

Write the crossing profile

printf 'crossings 1000 items 1\n' > /tmp/x.txt; awk '{print $2*$3}' /tmp/x.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
1000. One item per crossing is the worst shape, and the number shows it.
2

Batch the same work

printf 'crossings 1 items 1000\n' > /tmp/x2.txt; awk '{print $2*$3}' /tmp/x2.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
1000 items in one crossing. Same items, one thousand fewer crossings.
3

Stamp the layout version

printf 'layout=v2 module=calc host=js\n' > /tmp/v.txt; cat /tmp/v.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line. A layout version is the thing that makes a mismatch loud.

Data Flow

A call to a crossing
1 The host collects items
↓
2 The items are encoded once
↓
3 One crossing carries the batch
↓
4 The result is decoded once
Why this works
Why the shape is stamped

The shape is a version. A module built for one version and a host built for another is a bug that reads garbage.

The number that decides the shape

Items per crossing. Below ten, clarity wins. Above a thousand, the layout is worth the effort.

Examples

JSON is the easy shape

It is readable and it is slow. The slowness is the parse, and the parse happens per crossing.

Binary is the fast shape

It is a layout both sides must match. A mismatch is silent, which is why the layout is versioned.

Batching is the real fix

One crossing with a thousand items is cheaper than a thousand crossings. The count of crossings is the cost.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← A lock held across an await
Previous Lesson
A lock held across an await
Next Lesson
When WASM is not worth it
When WASM is not worth it →