9 ms·
Rewriting Libimagequant in Rust for Portability
- bz2 5y agoKornel is also behind the DSSIM library (in Rust), which I've found useful for comparing images before and after compression. https://github.com/kornelski/dssim https://github.com/kornelski/dssim
- Y-bar 5y agoAnd the excellent Mac OS image compression utility ImageOptim: https://imageoptim.com/mac https://imageoptim.com/mac
- pornel 5y agoFun fact: I wrote DSSIM as a development tool specifically for pngquant, because I needed a way to measure improvements in dithering. Classic per-pixel algorithms only saw it as added noise.
- roblabla 5y ago> Trying to make the library more object-oriented exposed a "drum-stick" design issue: does play(drum, stick) translate to drum.play(stick) or stick.hit(drum)? In cases where OO doesn't make sense, the solution is to simply provide a functional API instead. Rust stdlib has plenty of such APIs, it's fine to have free-standing functions!
- ape4 5y agoAgree, there's no rule that everything has to be a class in C++. I suppose drum.play() could default to using a stick.
- nine_k 5y agoYou can even make everything a class, and still provide a functional-ish interface, by using the class as a namespace with a common immutable context.
- lostcolony 5y agoBut...that was exactly the question the article had. Should drum be the enclosing class, or should stick be? What he cares about is the verb play, but he has to encapsulate it in a noun, and none of the ones actually involved dominate so clearly as to moot having to make a choice.
- nine_k 5y agomusicPart.play(drum, sticks); Basicslly you can replace the module namespace with a class namespace if desired. What module could that be?
- ReleaseCandidat 5y agoBut now the traits aren't connected with drums (or sticks) but MusicParts. Or you end up having some `drum.foo` too.
- deleted 5y ago[deleted]
- lostcolony 5y agoRight. But that's just it. "musicPart" is an object. A noun. What the hell is a "Music Part"? It's a completely synthetic object you've had to create solely to namespace functions. It's like how so many large Java projects have static "Utils" classes. What you've done is not OO; in fact, it's an anti-pattern in OO. You've created, as you correctly note, a namespace or module around static functions; there is no meaningful encapsulation of data. My point isn't that it can't be done, it's that trying to follow best OO practices, you run into this sort of problem (which has had a lot of digital ink spilled on it). The fact there is a pragmatic solution isn't in any doubt; it's that such a solution intentionally eschews the data encapsulation that OO demands (but not imperative or functional approaches, which instead say it's the right approach to do that).
- 5y ago
- mikepurvis 5y agoI think the GP's idea is to just do play(drum, stick) as its own function and not tie it to either class or have it be a passthru into internal methods for either.
- ReleaseCandidat 5y agoI guess he is talking about traits. How do you 'functionize' them?
- pornel 5y agoI wasn't thinking about traits when I wrote that, but you make a great point — that's a whole another way to design things. Maybe it could have been something fancy like: impl<T> Sound for T where T: Percussion<Accessory=Stick>
- hedgehog 5y agoThere's also the pattern used in from+into where one implementation is wrapped to provide its counterpart on the other type. Nice to read but feels a little extra magical for someone without a lot of Rust background.
- tialaramex 5y agoBlanket implementation is a very common idea in Rust. The standard library "owns" its own Traits and so it gets to provide blanket implementations for those, but you can do the same for your own. Here's (really, this is literally the code) how std provides a blanket implementation of Into given From: impl<T, U> Into<U> for T where U: From<T>, { fn into(self) -> U { U::from(self) } } There's actually a chain, From implies Into, Into implies TryFrom, then TryFrom implies TryInto.
- nicoburns 5y agoI think this is an excellent example of why people get so excited about Rust. It can't do anything you can't do in C or C++, but it makes a whole bunch of things significantly easier and less stressful. Which makes maintenance easier, and in practice often leads to performance improvements because more ambitious designs can be attempted.
- stouset 5y agoCareful now, you'll get accused of being in the Rust Hype Squad. Snark aside, this is what Rust proponents like myself have been trying to say. Rust isn't some magical panacea to all of the problems we face in computing, but it does solve some frustrating and endemic problems outright while making others significantly more tractable. And that it manages to do so without sacrificing execution performance (though at a generally reasonable cost in compilation times) and keeping code complexity more manageable than equivalent C/C++ code is just icing on the cake. What the language adds in complexity—in my experience at least—reduces the complexity of my own programs and makes it much simpler to write them and reason about them. There's definitely a learning curve as the language "wants" you to structure programs a certain way while it takes time to build up the intuition for doing so. But having done that, I've been able to apply those lessons to my projects in other languages to great success.
- sobkas 5y ago> I think this is an excellent example of why people get so excited about Rust. It can't do anything you can't do in C or C++, but it makes a whole bunch of things significantly easier and less stressful. Which makes maintenance easier, and in practice often leads to performance improvements because more ambitious designs can be attempted. For most people Rust is just overkill, what I really want is secure subset of c++, enforced by compiler with hardened standard library(that is sanctioned by standard body, might be not 100% compatible with generic one, because it contains only a subset of functionality and some functions might work differently).
- nicoburns 5y agoI'm not sure about this. Remember that most people (even most software developers) don't know C++ (or even C). And IMO (as someone who has tried to learn both) Rust is a lot easier to learn than C++
- pdimitar 5y agoThat's the kind of analysis I want to see. Really good write-up.
- Arch-TK 5y agoLots of extreme generalisations and exaggerations of the situation with C. It's great that you like rust and want to write your code in it, I don't really care at the end of the day, but vague statements such are not very useful or productive. >There's no standard for building C programs. Is there a written standard for compiling rust programs? I don't think so. Rust doesn't do standards after all. Jokes aside, this is basically irrelevant. Why does there need to be a standard for building C programs? Making something build across platforms (including cross compilation to other platforms) has been solved by everyone and their grandmother at this point. This means that aside from taking the route of re-implementing it yourself (not particularly difficult) you have literally dozens of perfectly good options to chose from. In fact, most of this rant sounds like it stems from your dislike of OpenMP not C. >I could have kept the library in C and replaced OpenMP with some other solution instead, but I wasn't keen on reinventing this wheel. Even basic things like spawning a thread run into C's portability woes. Thankfully, you don't need to reinvent the wheel, the problem of cross platform multiprocessing in C has been solved at least a dozen times by now. It should be trivial to use one of these many options (or even write your own, even if you don't like the idea of "reinventing the wheel" it's certainly not particularly difficult either and can likely be lighter than any other more general option). >The platonic ideal portable C exists only as a hypothetical construct in the C standard. The C that exists in the real world is whatever Microsoft, Apple, and others have shipped. That C is a mess of vendor-specific toolchains, each with its own way of doing things, missing features, broken headers, and leaky abstractions. Shouting "it's not C's fault, screw <insert vendor name>!" doesn't solve the problem, but switching to Rust does. I mean, rust is hardly helping you here. There's one implementation of rust doing their own thing so far, nothing stops anyone from making this situation as "bad" as in C. You may as well just say you only support clang or only support GCC and base your project around that assumption. In fact, if you're making the mistake of trying to support a microsoft compiler then I would argue that you've made your life difficult for no reason and blamed it on C. >dreadful state of C dependency management did. Hardly any more dreadful than rust's solution to the problem.
- sanxiyn 5y agoI disagree about dependency management. Let's say, to avoid reinventing the wheel, I decided to use GLib for cross-platform threading. How do I add GLib as a dependency to my C project? Now compare it to adding Tokio as a dependency to my Rust project, which is entirely trivial.
- jjnoakes 5y agoI am excited about rust supporting more platforms so that this kind of thing can be done more widely without sacrificing another kind of portability.
- bastardoperator 5y agoIt's funny because I've been using pngquant for optimizing assets we display in our Rust video game oxide extensions(by facepunch).
- steveklabnik 5y agoWe get closer and closer to getting Rust re-written in Rust every day... hahaha
- CyberRabbi 5y agoThis is one of those rare situations when monoculture is superior. Integrating multiple ad-hoc build systems is a nightmare, especially when cross compiling. Using cargo and rustc is a dream by comparison. It would benefit the C/C++ community to rally around a single build/compilation tool chain. Excellent write up!