Leyr / changelogOpen Leyr ↗

PERFORMANCE WEEK · NOVEMBER 12, 2026

Lighter, faster, gapless.

This week went into making Leyr cheaper to run and quicker to respond. The server now needs about a seventh of the memory it did. Jumping ahead in a long track starts in a tenth of a second. The app downloads less than half as much when it opens. Albums play without a gap between tracks. Here is what changed, layer by layer, and how each number was measured.

Infographic. Server memory six minutes after start: 920 MB before, 138 MB after. Jump to 25:00 in a 27-minute FLAC: up to 2.6 s before, 0.10 s after. First download of the app, gzip: 550 KB before, 228 KB after. Server threads: about 50 before, 12 after. Below, Leyr's stack from the browser to the audio pipeline and what changed in each layer.
Open the infographic on its own to save or share it.

The numbers

Before and after, measured on the live server on November 12, 2026
WhatBeforeAfterChange
Server memory, 6 min after start920 MB138 MB6.7× less
Server threads, 6 min after startabout 5012about 4× fewer
Jump to 25:00 in a 27-minute FLAC0.43–2.6 s0.08–0.10 sup to 26× faster
Jump to 5:00 in the same file0.68 s0.18 s3.8× faster
Main app script, raw1,961 KB740 KB2.6× smaller
Main app script, gzip550 KB228 KB2.4× smaller
Gap between album tracks (browser test)a fresh request per trackunder 120 ms of silencegapless

The stack

Leyr is one Rust process serving a React app. The server, its databases and ffmpeg run in a single rootless container, with Caddy in front.

Browser
React 18, TypeScript, Vite 6, Tailwind. Two <audio> elements for playback.
Edge
Caddy, compressing with zstd or gzip, in front of the server.
Server
Rust 1.93, axum 0.8 on tokio 1.53, mimalloc 0.1 as the global allocator.
Library
SQLite in WAL mode through sqlx 0.8. Two databases: the music library and the app's own.
Audio
ffmpeg for live transcodes to MP3, Opus or AAC.

SERVER

920 MB to 138 MB: an allocator story

Six minutes after a restart the server held 920 MB, and the run before had peaked at 2.4 GB within 45 minutes. Most of that memory was not in use. It had been freed, but never handed back to the system.

The cause was glibc's malloc. To avoid lock contention it gives each thread that allocates its own arena, a heap that reserves memory in 64 MB blocks and rarely shrinks. The async runtime started one worker per CPU core, 32 of them, and the SQLite driver added about 18 more threads. A memory map of the live process showed 52 arenas holding about 519 MB.

Two changes fixed it:

  • mimalloc as the global allocator. It returns freed pages to the system. In Rust it is two lines:
    #[global_allocator]
    static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
  • Four async workers, not 32. A music server spends its time waiting on disks and sockets, not computing, so four workers keep up easily. LEYR_WORKER_THREADS raises the cap for a busier server.

That brought the server to 208 MB. Then the database work below closed idle SQLite connections, and memory jumped to 1.6 GB. SQLite is written in C, so it calls glibc's malloc directly and never touches the Rust allocator. Each time the pool opened a fresh connection on a fresh thread, glibc gave that thread another arena: 45 of them, holding 1.3 GB. One line in the container image caps glibc at two arenas, whichever thread asks:

ENV MALLOC_ARENA_MAX=2

Six minutes after that deploy: 138 MB, 12 threads, and no runaway heaps.

AUDIO

Seeking: one argument in the right place

When a device can't play a file as it is, Leyr converts it with ffmpeg while it streams. Seeking passes a start time, and ffmpeg cares a great deal where that goes:

ffmpeg -i track.flac -ss 1500 …   # decodes 25 minutes, then throws them away
ffmpeg -ss 1500 -i track.flac …   # seeks in the file, then decodes

Placed after the input, -ss makes ffmpeg decode everything up to that point. Placed before, ffmpeg jumps straight there in the file. On a 27-minute FLAC on network storage, a jump to 25:00 went from 0.43–2.6 s to 0.08–0.10 s. A jump to 5:00 went from 0.68 s to 0.18 s.

The conversion commands live in the library database, where you can edit them. On startup Leyr updates any command that exactly matches an old built-in one, and leaves any command you've changed alone.

PLAYER

Gapless playback and crossfade

A track used to end, and only then did the next one start loading. Now the player keeps two <audio> elements: one playing, one standing by.

  • About 25 seconds before the end, or as soon as the current track has fully downloaded, the standby element loads the next track. A converted track has no fixed length or cache, so it gets the same head start.
  • The standby starts 50 ms before the current track ends and takes over. In browser tests the silence between tracks stays under 120 ms.
  • With crossfade on (Account, Playback, 0 to 12 seconds), the two overlap. Their volumes follow an equal-power curve, cos and sin, so the sum stays level through the middle.
  • Crossfade skips tracks that follow each other on the same album, so live albums and DJ mixes stay seamless.

Some details matter more than the main feature:

  • A standby track that never plays is released after 30 seconds of pause, because a live conversion holds a server process while it is open.
  • Podcasts are never loaded ahead.
  • A song saved on your phone counts as fully downloaded the moment it starts. Without a special case, the next song would begin downloading straight away. Saved songs now wait for the last 25 seconds, so playing them offline or on limited data stays quiet.

On iPhone and iPad the player keeps one element. iOS only lets an element play if a tap started it, and ignores volume changes from code. A handoff on a locked phone could go silent, so gapless and crossfade are off there, and lock-screen playback works as it always has.

DELIVERY

Opening the app, and opening it again

  • Pages load on demand. Every page except sign-in, setup and search now loads when you first visit it, through React.lazy. The main script went from 1,961 KB to 740 KB, or 550 KB to 228 KB compressed. If a page's file has gone missing after an update, the app reloads once to fetch the new version instead of showing an error.
  • Files that never change are kept for good. Vite names each script and stylesheet after its contents, so a changed file gets a new name. Those files are now sent with Cache-Control: public, max-age=31536000, immutable, which means your browser never asks for them again. The page itself is sent no-cache, so you always get the latest version.
  • Your browser keeps audio files. Original files are sent with Cache-Control: private, max-age=3600 and Last-Modified. Your own browser may reuse them for an hour, and nothing in between may store them.
  • Compression was already there. Caddy compresses every response with zstd or gzip before it leaves, so adding compression to the server would have done the same work twice.

LIBRARY

SQLite, tuned for a music server

Every connection now applies the same settings:

PRAGMA journal_mode = WAL;     -- readers never block the writer
PRAGMA synchronous  = NORMAL;  -- safe under WAL, far fewer disk flushes
PRAGMA mmap_size    = 67108864;  -- reads come straight from memory-mapped pages
PRAGMA cache_size   = -8000;   -- about 8 MB of page cache per connection

Each pool opens at most four connections and closes them after 10 seconds idle. Under WAL with synchronous=NORMAL, a power cut can lose the last few writes, but it cannot corrupt the database.

Also this week

The seek bar's blocks grow on hover by CSS transform rather than by height, so the player no longer shifts while you scrub. Transforms are animated by the compositor and cause no layout work.

Tried and dropped

We measured ffmpeg's probe and flush options (-probesize, -analyzeduration, -flush_packets) on the same long FLAC. The first audio came out in 45–65 ms with or without them. With no gain to show, Leyr keeps ffmpeg's defaults: probing less can misread some files.

How it was checked

  • More than 1,000 Rust tests and nearly 1,500 web unit tests pass. New tests cover the worker cap, the command rewrite, the caching headers, the SQLite settings, and the timing rules for gapless playback and crossfade.
  • Browser tests in Chromium play real audio through the handoff, a skip, a crossfade across albums and one within an album, and a saved song.
  • Memory, threads and arenas were read from the live process's memory map, six minutes after each deploy. Seek times were measured on the live library.
  • Each converted stream now logs how long ffmpeg took to send its first audio. Future work on start-up speed starts from those numbers.

Run your own Leyr? Update your server to get all of this. Back to the changelog