← the effing blog

Effing 0.45.0: on all cores

We've moved the dev server's renders and canvas encodes onto threads of their own, so local effie renders get a lot faster, and fns that fail partway through now fail cleanly.

Effing 0.45.0 is out, and both headlines are about threads. The dev server behind effing dev and effing render now serves requests on worker threads, so the annies in an effie render in parallel instead of taking turns. And @effing/canvas encodes frames on native threads of its own rather than on Node’s small shared threadpool. Together they make local renders noticeably faster. The release also fixes how every server behaves when an fn fails after it has started streaming its response.

Annies in parallel

When FFS renders an effie, it fetches all of its annies at once. Until now the dev server took those requests on its one thread, so the annies were drawn one after the other, with only their encodes overlapping.

In 0.45.0 the dev server hands requests to worker threads. Workers start on demand, up to a new dev.workers option in effing.config.ts, and a request goes to an idle worker or, once they’ve all started, to the least busy one:

import { defineConfig } from "@effing/dev";

export default defineConfig({
  project: "my-app",
  // ...
  dev: {
    workers: 4,
  },
});

The default is one worker per core less one, capped at 8. Below 3 cores it’s 0, since a single worker would only add a hop and a second copy of the app without rendering anything in parallel. Setting it to 0 yourself serves everything on the main thread, as before, which is handy when you want to step through an fn in a debugger.

On the starter project’s simple-slideshow, six annies at 1080×1920 on an 18-core Mac, fetching all six annies from a warm effing dev went from 4.3 seconds to 2.75, and effing render effie simple-slideshow end to end from 8.9 seconds to 6.4. Raising workers past the default didn’t help, since by then most cores were busy. The rendered frames are identical either way.

Edits still reach every worker, the same way they reach a single thread, and an fn that crashes takes down only its own worker, which is replaced on the next request. There are two things to keep in mind, though. Each worker evaluates fns on its own, so module-level state like a cache or a counter isn’t shared between requests that land on different workers. And each busy worker holds its own copy of the app, the fns and their canvas and font state, so if memory is tight, lower dev.workers.

Encodes off the threadpool

@effing/canvas now depends on @effing/skia 1.0.10-effing.7, which moves canvas.encode(), toDataURLAsync(), toBlob() and convertToBlob() onto native threads of their own, one per core. Previously they ran on libuv’s threadpool, which has 4 threads unless you set UV_THREADPOOL_SIZE before Node starts, and which the rest of the process shares. So at most four frames could encode at a time, and file system calls, DNS lookups, zlib and crypto work all queued up behind them.

Now as many frames encode at once as there are cores, whether the encode is started from the main thread or a worker thread, and the threadpool is left to everything else. On the same slideshow, this alone took effing render from 11.3 seconds to 8.2. The encoded bytes don’t change. If you want a different pool size, set the EFFING_SKIA_THREADS environment variable.

toBlob() also behaves more like it does in browsers: it calls back with null when an encode fails, and a callback that throws becomes an uncaught exception. Both used to crash the process.

Failing partway, properly

An fn can fail after its response has already started, for instance an annie that throws after its first frame. By then the status and headers are out, so the only honest thing a server can do is abort the response. None of ours did:

  • The fn server ended the response cleanly. The client got a TAR that looked complete but was missing frames, and FFS would render the annie with those frames missing, or cache the truncated annie during warmup. The response is now aborted, so the render fails instead.
  • The dev server let the error go unhandled. With dev.workers: 0 that stopped effing dev, and with workers it stopped the worker, failing every other request it was serving. It now logs the error with the request URL and aborts just that response.
  • FFS’s direct video stream could end as a short MP4. When GET /render/:id/video failed partway, the client got a truncated video that looked complete, and when FFmpeg itself exited with an error mid-stream, the error was uncaught and stopped the FFS server. The response is now aborted in both cases.

While tracking these down, a few more spots in FFS turned out to stop the server or hang when a source failed partway. A warmup fetch whose source drops its connection now fails and caches nothing, rather than waiting forever. A source that fails mid-download is reported as Failed to fetch <url>: response body failed: terminated, with code FETCH_FAILED, instead of a bare terminated with INTERNAL_ERROR. And proxied warmup and render progress streams always finish with a complete or error event, reporting a failed backend stream as BACKEND_FAILED.

Also in this release

  • Previews use parsed props. The preview endpoints and the effie preview page validated previewProps against the fn’s propsSchema but then passed the fn the raw object, so schema defaults and transforms didn’t apply and a preview could differ from the final render. They now get the parsed props.
  • No more listener warning on busy ports. Every taken port the dev server walked past while looking for a free one left a listener behind, so getting past ten of them printed a MaxListenersExceededWarning.

Upgrading

Bump your @effing/* packages to 0.45.0. Things to check:

  • If your fns rely on module-level state being shared across requests in effing dev or effing render, that no longer holds with workers. Set dev.workers: 0 to keep everything on the main thread.
  • Each busy dev worker holds its own copy of your fns and fonts. Lower dev.workers if the dev server uses more memory than you’d like.
  • If you match on FFS error codes, a source that fails mid-download now reports FETCH_FAILED (a 422 from GET /render/:id/video) instead of INTERNAL_ERROR, and a failed backend progress stream reports BACKEND_FAILED.
  • If you use proxyBinaryStream() from @effing/ffs/handlers, it no longer ends the response when the backend stream fails. It rejects, and aborting the response is up to you.

The full per-package changelogs are in the release PR.