mix transition allocates 9.2 MB per instance
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 72/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- c
- Domain
- audio-video-rtc, performance
Research direction
Start in transition_mix.c, focusing on the src_buffer and dest_buffer fields and the get_audio path. Reproduce the allocation behavior with the linked 500-transition example, then compare memory and rebuild time after the buffers are changed to reflect actual use. Done means mix transitions no longer reserve and touch 9.2 MB each when that capacity is unnecessary.
Written by the indexing model from the issue text.
Description
Environment: MLT 7.40.0 (Fedora 44 package, x86_64), FFmpeg 8.1.2. The code involved is unchanged on master (0926a75).
Found while building a GTK4 video editor on MLT; the repro below uses only melt/libmlt.
What happens
transition_mix embeds two MAX_SAMPLES * MAX_CHANNELS float buffers (192,000 × 6 × 4 bytes each) in its private struct (transition_mix.c, src_buffer and dest_buffer), so every mix is a 9.2 MB calloc(), whether or not it ever mixes more than a frame's worth of samples.
With glibc this is cheap at first: allocations that big are served by fresh mmap, whose pages are zero and stay untouched. But the first time one is freed, glibc's dynamic mmap threshold rises above 9.2 MB, and from then on each mix comes from the heap and calloc() zeroes all 9.2 MB. An app that rebuilds a graph with many mixes (one per track, plus one per audio crossfade) then pays gigabytes of resident memory and seconds of zeroing per rebuild.
Repro
Create 500 mix transitions, close them, repeat, with one small allocation kept alive per round (as the next graph's objects are): https://github.com/iDoMeteor/u-studio-video-editor/tree/70841c3/tools/upstream-repros/mlt/mix-struct-size
round 1: 500 mix transitions created in 4 ms, RSS while alive 83 MB, after close 81 MB
round 2: 500 mix transitions created in 33 ms, RSS while alive 147 MB, after close 147 MB
round 3: 500 mix transitions created in 2266 ms, RSS while alive 4475 MB, after close 4475 MB
round 4: 500 mix transitions created in 380 ms, RSS while alive 4475 MB, after close 4475 MB
In a real editor with 1,992 crossfades on a long timeline, rebuilds went from 0.35 s to 5.7 s, and resident memory reached 36 GB within nine rebuilds.
Expected: a mix costs what it uses. Actual: 9.2 MB each, fully touched once the heap is reused.
Notes
Allocating the buffers lazily (on the first get_audio), or sizing them to the frames actually mixed, would fix it. We work around it with mallopt(M_MMAP_THRESHOLD, 4 MiB) before mlt_factory_init(), which also turns off glibc's dynamic adjustment.
- Dominant language
- C
- Stars
- 1.9k
- Forks
- 391
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 12
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- Ships a Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from mltframework/mlt
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
mltframework/mlt#1335 ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
mltframework/mlt#1333 ·
Maintainers usually reply within 1 day
-
_unique_id is assigned with a non-atomic increment, so concurrent services can share an idPossibly taken @iDoMeteor claimed this 7 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 72/100
mltframework/mlt#1319 · 2 comments ·
Maintainers usually reply within 1 day
-
Crash: avformat decoder cache (size 4) evicts state another thread is still decoding withPossibly taken @iDoMeteor claimed this 7 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 52/100
mltframework/mlt#1318 · 1 comment ·
Maintainers usually reply within 1 day
-
Crash: unguarded lazy init in loader, service cache and avformat when producers open on several threadsPossibly taken @iDoMeteor claimed this 7 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 55/100
mltframework/mlt#1317 · 2 comments ·
Maintainers usually reply within 1 day
All issues in mltframework/mlt
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
libsdl-org/SDL#16464 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
backend
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
BasedHardware/omi#20940 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 Under an hour Newbie friendliness 70/100
Maintainers usually reply within 1 day