# Problem code `'beforeCommit-slow'`

> Contract 2.0.0 · package sha256 `a857eca95b35921109faf997094bb4192f12527d9d0fcd4be7e97f06425bad6e` · generated from `configurator-runtime.d.ts`, `tables/problems.json` and `services/onsite-scripter/scripts/contract-docs-what-to-do.json` by `services/onsite-scripter/scripts/build-contract-docs.mjs`; edit the source, not this file.

A member of [`ProblemCode`](../api/ProblemCode.md) · stable · since 2.0.0

A [`beforeCommit`](../api/Session.beforeCommit.md) function averaged over about 5 ms across its last 10 commits; recorded at most once per function per 10 s, with the count and the mean in `message`.

## What to do

Keep each [`session.beforeCommit`](../api/Session.beforeCommit.md) function under 5 ms (the mean over its last 10 commits; `message` has it): do only the layout arithmetic and the `commit.tx` writes inside it, read sizes you measured once per asset outside it (cache what [`labs.geometry.read`](../api/Labs.geometry.read.md) returns), and never load assets in it. The hook is labs: register it through [`has(session, 'beforeCommit')`](../api/has.md) ([`has`](../api/has.md) from the package root) with an [`on('committed')`](../api/Session.on.md) fallback, as below.

```ts
const widths = new Map<string, number>(); // measured once per asset, outside the hook
const row = async (writer: Writer): Promise<void> => {
  let x = 0;
  for (const n of writer.getSceneModel()?.root ?? []) {
    await writer.node(n.nodeId).set({ pose: { position: { x, y: 0, z: 0 } } });
    x += widths.get(n.nodeId) ?? 0;
  }
};
has(session, 'beforeCommit') // either branch unregisters when `signal` aborts
  ? session.beforeCommit('row', (commit) => row(commit.tx), { signal })
  // keep this branch: a runtime without the labs hook gets the same writes, one frame later
  : session.on('committed', ({ recorded }) => { if (recorded) void row(session); }, { signal });
```

## Where the declaration names it

- [`Session.beforeCommit`](../api/Session.beforeCommit.md): Keep it under 5 ms ('beforeCommit-slow'); never load assets (engine queries such as [`labs.geometry.read`](../api/Labs.geometry.read.md) are allowed, so it may be async; cache their results); mint ids from state, never at random.

The set is open: handle a code you don't know (`'other'`).

## Recorded by

- the table row `hold/rules/rule-5` (`tables/hold.json`)
