6 ms·
Improving performance of rav1d video decoder
- robertknight 1y agoGood post! The inefficient code for comparing pairs of 16-bit integers was an interesting find.
- ohr 1y agoThanks! Would be interesting to see if Rust/LLVM folks can get the compiler to apply this optimization whenever possible, as Rust can be much more accurate w.r.t memory initialization.
- Ygg2 1y agoWould be great, but wouldn't hold my breath for it. LLVM and Rustc can be both be kinda slow to stabilize.
- pornel 1y agoIt varies. New public APIs or language features may take a long time, but changes to internals and missed optimizations can be fixed in days or weeks, in both LLVM and Rust.
- adgjlsfhk1 1y agoI think rust may be able to get it by adding a `freeze` intrinsic to the codegen here. that would force LLVM to pick a deterministic value if there was poison, and should thus unblock the optimization (which is fine here because we know the value isn't poison)
- kukkamario 1y agoI think in this case Rust and C code aren't equivalent which maybe caused this slow down. Union trick also affects the alignment. C side struct is 32 bit aligned, but Rust struct only has 16bit alignment because it only contains fields with 16bit alignment. In practice the fields are likely anyway correctly aligned to 32bits, but compiler optimizations may have hard time verifying that. Have you tried manually defining alignment of Rust struct?
- infogulch 1y agoYou know it's a good post when it starts with a funny meme. Seems related to the recent discussion: $20K Bounty Offered for Optimizing Rust Code in Rav1d AV1 Decoder (memorysafety.org) | 108 comments | https://news.ycombinator.com/item?id=43982238 https://news.ycombinator.com/item?id=43982238
- deleted 1y ago[deleted]
- HappyPanacea 1y agoA clear case of Nominative determinism!
- mbeavitt 1y agoHaha I was just thinking to myself "I wonder if anyone made any progress on that rav1d bounty yet?"
- brookst 1y agoTitle undersells post; it’s actually 2.3% faster with two good optimizations.
- ohr 1y agoI think that since the 1.5% one is only for aarch64 it's a bit unfair to claim the full number, more like 1/2 if you consider arm/x86 to be the majority of the (future) deployments
- brookst 1y agoI suppose that’s fair, but I’d give credit for a 2.3% improvement in the test environment. For all we know it may be a net loss in other environments due to quirks (probably not, admittedly).
- mmastrac 1y agoThe associated issue for comparing two u16s is interesting. https://github.com/rust-lang/rust/issues/140167 https://github.com/rust-lang/rust/issues/140167
- heybales 1y agoThe thing I like most about this is that the discussion isn't just 14 pages of "I'm having this issue as well" and "Any updates on when this will be fixed?" As a web dev, GitHub issues kinda suck.
- eterm 1y agoIt was worse before emoji reactions were added and 90% of messages were literally just "+1"
- heybales 1y ago+1
- NoMoreNicksLeft 1y agoWonder if it's a poor interface issue... if people could just click a button that says "me too" but didn't add a full comment but rather just added some minimal notation at the bottom of the comment that indicated their username, 1) would people use it and 2) would that be not overly-busy enough to not be annoying? It could even mute notifications for the me-toos.
- IshKebab 1y agoThis seems like an area where LLMs would actually be extremely useful. You can manually mark comments as irrelevant. Why can't GitHub use AI to do it automatically? Or to highlight the "resolution" comment automatically? On very big issues it can take a non-trivial amount of time just to find out what the outcome was.
- rhdjsjebshjffn 1y ago
- tialaramex 1y agoAll being equal codecs ought to be in WUFFS† rather than Rust, but I can well imagine that it's a much bigger lift to take something as complicated as dav1d and write the analogous WUFFS than to clean up the c2rust translation, if you said a thousand times harder I'd have no trouble believing that. I just think it's worth it for us as a civilisation. † Or an equivalent special purpose language, but WUFFS is right there
- IgorPartola 1y agoWUFFS would be great for parsing container files (Matroska, webm, mp4) but it does not seem at all suitable for a video decoder. Without dynamic memory allocation it would be challenging to deal with dynamic data. Video codecs are not simply parsing a file to get the data, they require quite a bit of very dynamic state to be managed.
- lubesGordi 1y agoRequiring dynamic state seems not obvious to me. At the end of the day you have a fixed number of pixels on the screen. If every single pixel changes from frame to frame that should constitute the most work your codec has to do, no? I'm not a codec writer but that's my intuition based on the assumption that codecs are basically designed to minimize the amount of 'work' being done from frame to frame.
- throwawaymaths 1y agocompression algorithms can get very clever in recursive ways
- dylan604 1y agoMaybe you're not familiar with how long GOP encoding works with IPB frames? If all frames were I-frames, maybe what you're thinking might work. Everything you need is in the one frame to be able to describe every single pixel in that frame. Once you start using P-frames, you have to hold on to data from the I-frame to decode the P-frame. With B-frames, you might need data from frames not yet decoded as the are bi-direction references.
- IgorPartola 1y agoAV1 is an amazing codec. I really hope it replaces proprietary codecs like h264 and h265. It has a similar, if not better, performance to h265 while being completely free. Currently on an Intel-based Macbook it is only supported in some browsers, however it seems that newer video cards from AMD, Nvidia, and Intel do include hardware decoders.
- karn97 1y ago9070xt records gameplay by default in av1
- monster_truck 1y agoRDNA3 cards also have AV1 encode. RDNA 2 only has decode. With the bitrate set to 100MB/s it happily encodes 2160p or even 3240p, the maximum resolution available when using Virtual Super Resolution (which renders at >native res and downsamples, is awesome for titles without resolution scaling when you don't want to use TAA)
- kennyadam 1y agoIsn't that expected? 4K Blurays only encode up to like 128Mbps, which is 16MB/s. 100MB/s seems like complete overkill.
- vlovich123 1y agoI think op just didn’t type Mbps properly. 100MB/s or ~800Mbps is way higher than the GPU can even encode at a HW level even I would think
- monster_truck 1y ago100,000kbps. It will more than double that for 3240p. https://i.imgur.com/LyrhNXZ.png https://i.imgur.com/LyrhNXZ.png
- lubesGordi 1y agoHonestly its a little surprising the first optimization he found was something fairly obvious just by using perf. I thought they had discussed the zeroing buffers issue in the first post? The second optimization was definitely more involved/interesting but was still pointed at by perf. Don't underestimate that tool!
- sounds 1y agoHe came from the aarch64 perspective on an Apple device. I often experience someone spotting an "obvious in hindsight" gap because they come from a different background.
- Sesse__ 1y agoAFAICS, it wasn't “just perf”; it was doing a differential profile between the C and Rust versions, with manual matching up. (perf diff exists, but can't match across the differing symbol names, and few people seem to use it.)
- nemothekid 1y agoIntersting to see this article on the perfromance advantage of not having to zero buffers after this article 2 days ago: https://news.ycombinator.com/item?id=44032680 https://news.ycombinator.com/item?id=44032680
- mdf 1y agoThere's something about real optimization stories that I find fascinating – particularly the detailed ones including step-by-step improvements and profiling to show how numbers got better. In some way, they are satisfying to read. Nicholas Nethercote's "How to speed up the Rust compiler" writings[1] fall into this same category for me. Any others? [1] https://nnethercote.github.io/ https://nnethercote.github.io/
- ohr 1y ago(Author here) I'm a huge fan of the "How to speed up the Rust compiler" series! I was hoping to capture the same feeling :)
- dirtyhippiefree 1y agoHaving your last name be Ravid really is the icing on your cake. Real is about the only other codec I see that could be a name, but nobody uses that anymore.
- aidenn0 1y agoDo your part: name your kids "ffmpeg" and "vp-IX"!
- dpacmittal 1y agoI read an article a while ago where the goal is to process a file as fast as possible and the article talks about compressing the data chunks so they fit in L1 cache. The cache misses were slower than compressing and decompressing the data from L1 cache. I've been trying to find that article ever since but I'm not able to. Anyone knows the article I'm talking about?
- Voultapher 1y agoSince you seem to enjoy this kind of writing I'd love to get your feedback on something I've written a while back about branchless partitioning [1]. Despite it being content wise the most work to create of the things I've written about the topic, it found much less attention than other things I've written. So far I've wondered if it was maybe too technical? Would love to get an honest opinion. [1] https://github.com/Voultapher/sort-research-rs/blob/main/writeup/lomcyc_partition/text.md https://github.com/Voultapher/sort-research-rs/blob/main/wri...
- anon-3988 1y agoIs skipping initialization of buffers a hard problem for compilers?
- empath75 1y agoIt's easy to not initialize the buffer, the hard part is guaranteeing that it's safe to read something that might not be initialized.
- adgjlsfhk1 1y agoyeah. Proving that the zero initialization is useless requires proving that the rest of the program never reads one of the zeroed values. This is really difficult because compilers generally don't track individual array indices (since you often don't even know how big the array is)
- brigade 1y agoIt’s especially hard to elide the compiler initialization when the intended initialization is by a function written in assembly
- mastax 1y agoIn this case I assume the difficulty is the initialization happens in assembly which the compiler has no visibility into.
- jebarker 1y agoBeautiful work and nice write-up. Profiling and optimization is absolutely my favorite part of software development.
- matanx69 1y ago[dead]
- renewiltord 1y agoOh this stuff is what’s prompting the ffmpeg Twitter account to make a stand against Rust https://x.com/ffmpeg/status/1924137645988356437?s=46 https://x.com/ffmpeg/status/1924137645988356437?s=46
- mmastrac 1y agoReading the ffmpeg twitter account is enough to turn me off using ffmpeg. It's a shame there's no real alternative -- the devs seem very toxic. I mean sure, max performance is great if you control every part of your pipeline, but if you're accepting untrusted data from users-at-large ffmpeg has at least a half-dozen remotely exploitable CVEs a year. Better make sure your sandbox is tight. https://ffmpeg.org/security.html https://ffmpeg.org/security.html I feel like there's a middle ground where everyone works towards a secure and fast solution, rather than whatever position they've staked out here.
- throwaway94487 1y agoHow many of those "remotely exploitable CVEs" have actually been exploited in the wild? Quite a few are denial-of-service and memory leak CVEs too, which Rust doesn't consider to be unsafe.
- saagarjha 1y agoMore than enough are exploitable for this to be a problem.
- izacus 1y agoI've worked with ffmpeg for literally a decade and I've never found them particularly toxic. What I have found that they (as many others who do great work) have very little tolerance of random junior language fanboys criticizing their decades of work without even understanding what they're talking about and constantly throwing out silly rewrite ideas.
- mmastrac 1y ago
- Mr_Eri_Atlov 1y agoAV1 continues to be the most fascinating development in media encoding. AVG-SVT-PSY is particularly interesting to read up on as well.
- smallpipe 1y agoThis is really fun. Is there anything stopping rustc from performing the transmute trick ? Edit: If I had read the next paragraph, I'd have learn about [1] before commenting [1] https://github.com/rust-lang/rust/issues/140167 https://github.com/rust-lang/rust/issues/140167
- TinkersW 1y ago[flagged]
- adgjlsfhk1 1y agotranspiled code is rarely good. Rust is often better than C for SIMD code (it actually has useful SIMD instructions exposed, and aliasing guarantees make it a lot easier for the compiler to figure out obvious optimization. By transpiling, however you loose most of the structure of an idiomatic project and generally make a bit of a mess of things.
- khuey 1y agoThe "comparing a pair of i16s" thing is clearly not just a Rust issue since the C version had the same hack.
- josteingutt 1y ago[flagged]
- gitroom 1y ago[dead]
- saagarjha 1y agoI am very curious what you did to embed the profiler results into your blog post. Literally copy the HTML nodes?
- ohr 1y agoUsed the Save Page WE Chrome extension to capture the html (after cleaning up with inspect element + delete), and added a bit of custom JavaScript to scroll everything to the right place. Needed the extension for the styling to be captured correctly.
- sylware 1y agoI don't understand this project. dav1d is 99% assembly (x86_64/risc-v 64bits/etc) with very little simple and plain C as coordinating code.
- canucker2016 1y agoThe read-only dav1d github repo says 79.8% assembly, 19.7% C language, 0.5% other. see https://github.com/videolan/dav1d https://github.com/videolan/dav1d
- sylware 1y agoomg... how is this possible to miss that much the point?