Effing 0.47.0: that was quick
We've reworked how frames get from your fns into FFmpeg, so renders take 28 to 40 percent less time and up to a third less memory, with the same output.
Effing 0.47.0 is out, and nearly all of it is about speed. Canvas frames
now rasterize off the JS thread, FFS starts FFmpeg while annies are still
being generated and decodes JPEG annies on several threads, opaque
full-frame layers no longer get blended over a background, and
effing render warms up and winds down its workers at better moments.
The output doesn’t change: both videos below come out the same, byte for
byte.
We measured with effing render effie on an 18-core Mac, using two
1080×1920 videos: the starter project’s slideshow (13 seconds, with
photo-zoom and typewriter annies) and a 21-second promo with three video
clips, two panned photos, title cards and an animated logo annie.
| Video | 0.46 | 0.47 | Peak memory |
|---|---|---|---|
| Slideshow | 4.9 s | 2.9 s | 3.5 GB → 2.8 GB |
| Promo | 4.3 s | 3.1 s | 3.5 GB → 2.2 GB |
The numbers in the sections below are for each change on its own, so they don’t add up exactly. Some changes only pay off once another one has removed the bottleneck in front of them.
Rasterizing off the JS thread
In 0.45.0, canvas.encode() moved encoding onto native threads. But
before a frame could be encoded, it had to be rasterized: the recorded
draws (resampled images, anti-aliased paths, gradients, text) played onto
the canvas’s pixels. That still happened on the JS thread.
@effing/tween works on up to eight frames at once, but only their
encodes could overlap, so an annie still rasterized one frame at a time.
@effing/canvas now depends on @effing/skia 1.0.10-effing.9, where
encode() hands the recorded drawing to the encode threads, which
rasterize it and then encode it. The JS thread is left with layout and
recording the draws. With eight frames in flight:
| Frame | JS thread per frame | Time per frame |
|---|---|---|
| 1296×1920 photo, JPEG | 5.75 → 0.08 ms | 6.12 → 2.55 ms |
| 1080×1920, 1,100 translucent rects | 7.60 → 0.13 ms | 7.60 → 2.24 ms |
| 1296×1920 photo, QOI | 6.02 → 0.05 ms | 6.04 → 2.40 ms |
On the slideshow, this alone took effing render from 4.88 seconds to
4.23, for about 2% more CPU time. The encoded bytes are unchanged.
It works for the usual way annies draw, where each frame goes onto a new
or cleared canvas and is encoded. A canvas that was read since it was
last cleared (with getImageData() or toBuffer(), say), one created
with alpha: false, or one with a Lottie frame on it still rasterizes on
the JS thread, and so does a second encode() of the same drawing in
most cases. encodeSync(), toBuffer(), toDataURL(),
toDataURLAsync(), toBlob() and convertToBlob() rasterize on the
calling thread, as before.
Since the encode threads now do the rasterizing too, their number matters
more. The default of one per core is the right one, and setting
EFFING_SKIA_THREADS lower makes photo-heavy annies noticeably slower.
The same @effing/skia release changes how system fonts load. Every
worker thread that loaded @effing/canvas used to scan them itself, and
when workers started together, each spent 72 to 83 ms on it. They’re now
loaded once per process, on a thread of their own, and loading the
package in a worker takes about a millisecond. Anything that uses fonts
waits for that load, so you see the same fonts as before. On Linux, fonts
in /usr/local/share/fonts now load as system fonts too.
FFmpeg starts sooner
When FFS renders an effie, it fetches all of its sources at once, and until now it waited for every one of them, down to the last frame of the slowest annie, before starting FFmpeg. That annie is usually still being generated, so FFmpeg sat idle for most of the render and then did all of its work at the end.
FFS now starts FFmpeg once the other sources are ready and every annie has its first frame, and streams each annie into FFmpeg through a pipe as its frames come in. Frames still land on disk first, so a slow FFmpeg never holds up generation. On its own, this took 0.30 seconds off the promo and 0.84 off the slideshow. FFmpeg now runs alongside generation, so a render uses somewhat more CPU and memory at once.
An annie whose frames mix image formats, such as JPEG and PNG, now fails the render. FFmpeg used to decode every frame with the first frame’s decoder and silently drop the frames it couldn’t read, leaving gaps in the video. Frames that change size or channels within one format still work.
JPEG frames, decoded in parallel
With annies streamed, the slideshow ran into the next limit: FFmpeg’s JPEG decoder isn’t multi-threaded, so the slideshow’s 1296×1920 photo frames were decoded one after another, at about 5 ms each. The PNG and QOI decoders don’t have this problem.
FFS now splits a streamed JPEG annie round-robin over several tracks of a
Matroska stream, with a decoder for each, and interleaves them back into
one stream in the filter graph, with the same frames and timestamps. It
uses one track per 4 cores, at most 4. FFmpegRunner has a new
decodeTracks option to change that limit, and 1 turns splitting off.
On the slideshow this took another 0.5 to 0.6 seconds off, and 0.8 with everything else in this release in place. The promo, whose only JPEG annie is short, barely changes.
To split an annie, FFS needs a frame on disk for each decoder. Before starting FFmpeg, it waits at most 50 ms for them, and an annie that has fewer is split over fewer. An annie whose frames on disk differ in size or format, a short annie that’s already complete, and PNG and QOI annies aren’t split.
Opaque layers skip the blend
In a typical render, the bottom layer of most segments is a full-frame photo or a JPEG annie. FFS used to put that layer on top of the segment’s background color: it converted every frame to a format with alpha and then blended it over the background, pixel by pixel, only to end up with the layer’s own pixels again.
Now, when a layer is opaque and covers the frame for its whole segment, over a color background, FFS uses it as the segment’s base and leaves out the background and anything below it. To know which layers are opaque, FFS reads the size and format from each fetched image’s header, or from an annie’s first frame, before it starts FFmpeg.
On its own, this saved 2.1 to 2.6 seconds of CPU time per render and lowered its peak memory from 2.6 to 1.6 GB on the promo and from 2.0 to 1.4 GB on the slideshow. The promo also got 0.26 seconds faster.
For JPEG layers, the output is unchanged byte for byte. An opaque RGB PNG or QOI base layer, in a render that also has JPEG layers, can come out a few levels of rounding different in its segment. Segments over a video or image background render exactly as before.
A leaner effing render
Two changes to the dev server behind effing render make local renders
start faster and peak lower:
- Workers warm up while FFS starts.
effing render effienow starts half the app workers and loads your fns in them while FFS is starting, instead of when FFS first asks for the effie and its layers. That took 0.15 to 0.2 seconds off both videos. It imports every fn in the project, though, so module-level code in fns the render doesn’t use now runs too. - Idle workers stop. FFS asks for the effie and then all of its
sources at once, so a worker that has served its share gets nothing
more. It still kept its fn modules, canvases and decoded images, often
over 100 MB, until the end of the render, including while FFmpeg
encodes, which is when memory peaks.
effing rendernow stops a worker once it has been idle for half a second, which lowered the peak memory of the whole render by about 0.6 GB on the promo and 0.4 GB on the slideshow. How much you save depends on how your platform’s allocator returns freed memory, so expect less on Linux.
effing dev is unchanged, and keeps its workers warm for previews.
Also in this release
- Rounded borders stay inside the box. A rounded border whose sides
weren’t all the same width or color, such as a colored
borderLefton a card withborderRadius, was drawn as a band of constant width. It stuck out of the box by half its width and kept its full width round the corners. Borders are now filled between the outer edge and the padding box, as browsers do, so a left-only border tapers off where it meets the top and bottom edges, and sides of different colors meet along the diagonal. Inset box shadows are clipped to the same padding box. - Border shorthands parse like CSS.
borderand the per-side shorthands no longer split a color likergba(0, 0, 255, 0.5)at its spaces, and take the width, style and color in any order. A style ofnoneorhiddengives no border, a style without a width gives a 3px one, thethin,mediumandthickkeywords work, andborderColortakes one to four values. - Borders default to the text color. A border without a color, such
as
border: "1px solid", is drawn in the element’scolor, as in CSS, where it used to be black. Andcolor: "inherit","currentColor"or an invalid color now take the parent’s color, for text as well as borders. Text used to be drawn in whatever fill was set last.
Upgrading
Bump your @effing/* packages to 0.47.0. Things to check:
- If an element has a non-black
colorand a border without a color, the border now comes out in that color instead of black. - If any of your annies mix image formats across frames, they now fail the render. Pick one format per annie.
- If your fns do work at import time, such as fetching or logging, it now
runs at the start of
effing render effiefor every fn in the project, not just the ones the render uses. - If you use FFS’s direct video stream route, an annie that fails after FFmpeg has started sending video now aborts the response rather than returning a 422.
- If a JPEG annie changes frame size within its first few frames, it can
fail to render when it’s split. Set
decodeTracks: 1onFFmpegRunnerif that affects you. - A font added to your user font directory while a process is running is no longer picked up by workers that start later. Restart the process to see it.
The full per-package changelogs are in the release PR.