-
Notifications
You must be signed in to change notification settings - Fork 526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: switch to resvg
as the SVG renderer
#2581
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 7 out of 9 changed files in this pull request and generated 1 comment.
Files not reviewed (2)
- yazi-plugin/preset/plugins/svg.lua: Language not supported
- yazi-plugin/preset/plugins/video.lua: Language not supported
Comments suppressed due to low confidence (1)
yazi-plugin/src/process/command.rs:46
- Check the return value of libc::setrlimit to handle potential errors when setting memory limits.
libc::setrlimit(libc::RLIMIT_AS, &rlp);
Benchmark with https://commons.wikimedia.org/wiki/File:Cannon-diagram2.svg: Benchmark 1: resvg -w 2000 -h 2000 Cannon-diagram2.svg /tmp/abc.png
Time (mean ± σ): 295.5 ms ± 1.2 ms [User: 257.2 ms, System: 37.0 ms]
Range (min … max): 294.5 ms … 298.5 ms 10 runs
Benchmark 2: rsvg-convert -w 2000 -h 2000 Cannon-diagram2.svg -o /tmp/abc.png
Time (mean ± σ): 747.3 ms ± 6.1 ms [User: 662.8 ms, System: 59.0 ms]
Range (min … max): 744.1 ms … 763.9 ms 10 runs
Summary
resvg -w 2000 -h 2000 Cannon-diagram2.svg /tmp/abc.png ran
2.53 ± 0.02 times faster than rsvg-convert -w 2000 -h 2000 Cannon-diagram2.svg -o /tmp/abc.png Benchmark with https://commons.wikimedia.org/wiki/File:Moldova_(1483)-ro.svg: Benchmark 1: resvg -w 2000 -h 2000 Moldova_\(1483\)-ro.svg /tmp/abc.png
Time (mean ± σ): 601.6 ms ± 1.7 ms [User: 545.4 ms, System: 50.0 ms]
Range (min … max): 598.5 ms … 604.2 ms 10 runs
Benchmark 2: rsvg-convert -w 2000 -h 2000 Moldova_\(1483\)-ro.svg -o /tmp/abc.png
Time (mean ± σ): 1.466 s ± 0.004 s [User: 1.365 s, System: 0.061 s]
Range (min … max): 1.460 s … 1.473 s 10 runs
Summary
resvg -w 2000 -h 2000 Moldova_\(1483\)-ro.svg /tmp/abc.png ran
2.44 ± 0.01 times faster than rsvg-convert -w 2000 -h 2000 Moldova_\(1483\)-ro.svg -o /tmp/abc.png |
@sxyazi Aw, too bad 😅! I literally just finished adding resvg support. Plugged it in just like the image crate — and the binary only got about 10% bigger! But with that we can preview SVGs with text and load fonts for it! Wanna check it out? |
Is there a way to limit its memory usage if it's built-in rather than being
a separate process? Without the limitation, SVGs like the one in
linebender/resvg#814 would immediately consume
all system memory.
…On Tue, Apr 8, 2025 at 1:03 AM JustForFun88 ***@***.***> wrote:
@sxyazi <https://github.com/sxyazi> Aw, too bad 😅! I literally just
finished adding resvg support. Plugged it in just like the image crate —
and the binary only got about 10% bigger!
But with that we can preview SVGs with text and load fonts for it! Wanna
check it out?
—
Reply to this email directly, view it on GitHub
<#2581 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEFWFIE6G4AFWU6GDRSUYKT2YKVWBAVCNFSM6AAAAAB2TMEV4OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOOBUGAZDAMBXGU>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
[image: JustForFun88]*JustForFun88* left a comment (sxyazi/yazi#2581)
<#2581 (comment)>
@sxyazi <https://github.com/sxyazi> Aw, too bad 😅! I literally just
finished adding resvg support. Plugged it in just like the image crate —
and the binary only got about 10% bigger!
But with that we can preview SVGs with text and load fonts for it! Wanna
check it out?
—
Reply to this email directly, view it on GitHub
<#2581 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEFWFIE6G4AFWU6GDRSUYKT2YKVWBAVCNFSM6AAAAAB2TMEV4OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOOBUGAZDAMBXGU>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
I'm not sure about that — I'll see what I can do. One thing I do know for sure: embedding allows to use about half the memory. No idea why, but when rendering SVGs, resvg clones the pixel buffer during demultiplying, so we end up with two copies of the pixel data. I changed it to modify the pixels in-place instead. I’ll try to dig into the OOM issue. There's that hint saying “All you can do right now is to walk over nodes and check their bboxes”, though I was kinda hoping to avoid too much headache here. |
@sxyazi Actually, I have an idea! We could manually check the image size using tree::size, and if rendering it would exceed the memory restrictions, just skip it and display a message like “Image too large to render” or something along those lines. |
This reverts commit 0082d11.
Follow up to #2533
This PR replaces Yazi's SVG previewer from ImageMagick to
resvg
to address the following issues:rsvg
, andinkscape
, which introduces many uncontrollable factors.rsvg
, can exhaust all available memory when rendering certain SVGs, leading to system crashes.-density
and-resize
to maintain image quality, while thersvg
backend only needs-size
. No single parameter combination works correctly across all backends.As a result, several users have reported SVG preview issues recently:
Aside from ImageMagick, the remaining two SVG renderers are
rsvg-convert
andresvg
. I choseresvg
because:resvg
is ~2.5 times faster thanrsvg-convert
in my benchmarksresvg
has better SVG support thanrsvg-convert
resvg
provides an official Windows binary release, allowing Windows users to use it directly, even install it via ScoopCurrently,
resvg
does not support memory limitation. So, this PR adds memory restrictions forresvg
within Yazi:setrlimit(RLIMIT_AS)
CreateJobObjectW()
andAssignProcessToJobObject()
setrlimit
is ineffective, memory usage is continuously monitored withproc_pidinfo()
, and the process is terminated if it exceeds the limitAfter this PR, Yazi will use
resvg
instead ofImageMagick
as the SVG previewer, which is a new optional dependency.You can download it from https://github.com/linebender/resvg/releases