Better HN
topnewbestaskshowjobs
Log inSign up

raphlinus

14,081 karma · joined Mar 7, 2014 (12y ago) · 1,887 submissions

I do research on fundamental UI technology and 2D graphics, with a focus on Rust and fonts. @raph@mastodon.online
Submissions

comment 4y ago · link · parent

In fact I do, Bellingcat has published extensive analysis[1] of this attack.

[1]: https://www.bellingcat.com/news/2022/04/14/russias-kramators...

comment 4y ago · link · parent

Markdown parsing can be quite fast. I just benchmarked pulldown-cmark parsing all the Markdown in my blog (10k lines, 1MB) and writing HTML, and it does that in 14ms. Admittedly that's on an M1 Max.

If indeed premature optimization is the root of all evil, then I have lots to atone for.

comment 4y ago · link · parent

If by "controversy" you mean disinformation techniques designed to cast doubt on credible reports of war crimes, you are correct. See [1] for an analysis of this claim, including tracing it back to WarOnFakes and denials by Peskov. One of the great things about that report (making it I think particularly suitable for a Hacker News audience) is that it invites skeptical or critical readers to check the evidence themselves. Likely there is a reader of this thread who is competent at analyzing satellite imagery metadata and figuring out shadow angles.

[1]: https://www.info-res.org/post/disinformation-denial-russia-s...

comment 4y ago · link · parent

For now.

Unfortunately, even if you have very fast drawing, there are plenty of other ways to make software slow. But high performance GUI software is the main thing I'm working on at the moment and I expect to have interesting results in the coming months. If nothing else, it should be a data point of how fast GUI software can be.

comment 4y ago · link · parent

The main distinction I'd draw is whether you'd doing formal or informal reasoning. In the formal reasoning world, undefined behavior is mostly a good thing. On one side of the contract, it's a set of proof obligations for the code, and not especially onerous in the grand scheme of things - correct programs won't do UB. On the other side, it's a clear statement of what the compiler is and is not allowed to optimize. The more UB, the more opportunities to optimize.

When you're doing informal reasoning, the calculus changes. There's all kinds of stuff that can go wrong that is not motivated by what the machine is actually doing. In fact, it's something of a nightmare. Doing a memcpy of a struct that has padding in it? What are the exact semantics of restrict? And threading. Benign data races used to be a thing, but in an undefined behavior world, it's game over. C makes things worse than they need to be with its wonky integer rules (left shift of a negative integer and wrapping multiplication of two unsigned shorts are both UB), but a lot of that is potentially fixable and those mistakes won't be repeated in new languages.

In the context of Rust, more undefined behavior makes sense, and Ralf's work takes us much closer to a solid spec. But when you're doing mostly informal reasoning, I can see why people are so emotionally against it, and decisions such as turning off strict aliasing might be justified.

comment 4y ago · link · parent

I vouched this. I have no idea whether it's true, but I think it's worth discussing and relevant to the topic. My first real introduction to fixed points was in Dijkstra's weakest precondition semantics of imperative programs, where supplies the semantics of a while loop (via the Knaster-Tarski theorem). I wouldn't be surprised if there were indeed something deeper and more general there.

comment 4y ago · link · parent

A ton of stuff, arguably too many things. Right at the moment, my main focus is redoing fundamental 2D vector graphics geometry operations (stroking, path intersection) to make them more robust and higher quality. Some of this will also run in the GPU as compute shaders, which adds some interesting constraints and ups the difficulty level.

I'm also building a new, high performance, high quality 2D rendering engine that runs primarily on the GPU.

And I'm also exploring new approaches to building GUI, primarily in Rust. A new focus is to integrate that more tightly with the renderer mentioned above. A benefit I expect is that I'll be able to do the CPU side of the rendering work in multiple threads, where existing architectures make it very difficult to break out of a single thread.

How did I end up working on this? It's a natural evolution of what I've been working on for a long time, in Ghostscript, on Google Fonts, working on the text stack on Android, and now fortunate enough to have a research position back on Google Fonts where I am able to focus on this and have great support from the rest of the team.

comment 4y ago · link · parent

Can you send me some screenshots? Very possibly you have some ClearType parameters set wrong on your system. We absolutely do test on 1080p (though I admit my work machines are all retina/4k at this point).

comment 4y ago · link · parent

Behdad had this result earlier: https://twitter.com/behdadesfahbod/status/958065026262183936

comment 4y ago · link · parent

I find Neil Mitchell's categories of small[1], medium[2], and huge[3] build systems useful. Blaze is absolutely fantastic as a huge build system, as it can correctly specify the exact semantics of systems with lots of components and complexities such as cross-language bindings, autogenerated files, etc. If you are building a huge system, then a small or medium build tool just won't be up to the task. At that kind of scale, somebody is going to be responsible for the build system working, possibly as a full time job, and it makes sense to tap somebody who understands how Bazel works and knows how to put those ideas into practice.

Conversely, as many comments here observe, it is terrible as a small build system. There, you want to be able to get started quickly and pull in dependencies without thinking too hard. A simple but easy to understand approach (even one based on make) might work. This is just a different problem than what Bazel solves.

My personal opinion (and I should emphasize, I am absolutely not speaking for anyone here, least of all Google) is that the best way forward is to take the ideas of Bazel (hermetic and deterministic builds) and package them as a good small build system, perhaps even compatible with Bazel so you don't have to rewrite build rules all the time. I also think compilers and tools can and should evolve to become good citizens in such a world. But I have no idea how things will go, it's equally plausible the entire space of build systems will continue to suck as they have for decades.

[1]: http://neilmitchell.blogspot.com/2021/09/small-project-build...

[2]: https://neilmitchell.blogspot.com/2021/09/reflecting-on-shak...

[3]: https://neilmitchell.blogspot.com/2021/09/huge-project-build...

comment 4y ago · link · parent

That's a reasonable choice and I do agree it has nice properties, for example (a << b) << c is equal to a << (b + c) (unless that latter addition overflows), but it also does put you pretty firmly in the category of "not squeezing out every last cycle like you can in C." Usually that's one of the the things people expect in a tradeoff involving safety.

Regarding "never," I am an adventurous programmer[1], so am fully willing to accept that I am the exception that proves the rule.

On the more general point of UB for arithmetic overflow, I think we're on the same side: this is a pretty broken story in the way C has ended up, and one of the clear motivations for a cleaned up better-than-C language. I'm more interested in your thoughts on data races, where I suspect we have a substantive difference in perspective.

[1]: https://github.com/linebender/kurbo/blob/de52170e084cf658a2a...

comment 4y ago · link · parent

Requiring the shift amount to be unsigned is very sensible.

There are two other issues. One is when the shift amount is greater than or equal to the machine word size, for example 1u32 << 32. Java and Rust (release mode; debug is allowed to panic similarly to other forms of arithmetic overflow) take the position that the shift amount is masked to be modulo the word size, which happens to match Intel and aarch64 assembly instructions but not 32-bit ARM, so (unless the optimizer can prove the range) requires an additional mask instruction. This is the motivation for not explicitly specifying the behavior in C, though I believe a very strong case can be made that it should be implementation defined rather than undefined. In any case, it sounds like you are making the opposite decision as Java, so there's extra logic needed to correctly implement it on Intel and aarch64 but it should work efficiently on 32-bit ARM.

But I was specifically referring to the fact that -1 << 1 is UB in C. It was fixed in C++20 (so the value is specified to be -2, as arithmetic is now defined to be twos complement), but that fix is not in the C23 draft. I consider this perhaps the canonical example of programmer-hostile UB.

comment 4y ago · link · parent

I'll give a concrete example. It's not the most compelling optimization in the world, but I think illustrates the tradeoffs clearly. The following is pseudocode, not any particular language, but hopefully will be clear.

    let i = mystruct.i;
    if (i < array.length) {
        let x = expensive_computation(i);
        array[i] = x;
    }
For the purpose of this example, let's say that the expensive computation is register-intensive but doesn't write any memory (so we don't need to get into alias analysis issues). Because it is register-intensive, our optimizer would like to free up the register occupied by i, replacing the second use of i by another load from mystruct.i. In C or unsafe Rust, this would be a perfectly fine optimization.

If another thread writes struct.i concurrently, we have a time of check to time of use (TOCTOU) error. In C or unsafe Rust, that's accounted for by the fact that a data race is undefined behavior. One of the behaviors that's allowed (because basically all behavior are allowed) is for the two uses of i to differ, invalidating the bounds check.

Different languages deal with this in different ways. Java succeeds in its goal of avoiding UB, disallowing this optimization; the mechanism for doing so in LLVM is to consider most memory accesses to have "unordered" semantics. However, this comes with its own steep tradeoff. To avoid tearing, all pointers must be "thin," specifically disallowing slices. Go, by contrast, has slices among its fat pointer types, so incurs UB when there's a data race. It's otherwise a fairly safe language, but this is one of the gaps in that promise.

Basically, my argument is this. If you're really rigorous about avoiding UB, you essentially have to define a memory model, then make sure your use of LLVM (or whatever code generation technique) is actually consistent with that memory model. That's potentially an enormous amount of work, very easy to get subtly wrong, and at the end of the day gives you fewer optimization opportunities than C or unsafe Rust. Thus, it's certainly not a tradeoff I personally would make.

comment 4y ago · link · parent

The point of the C11 memory model is that it gives formal bounds on what optimizations the compiler is and is not able to do. In particular, it is free to reorder memory operations as if the program was single-threaded, unless the memory operations are explicitly marked as atomic. My assertion is that if you do these optimizations and then have a data race, it's functionally equivalent to undefined behavior, even if you call it something different and loudly proclaim that your language doesn't have UB.

comment 4y ago · link · parent

How does this work in practice? Is the behavior of a use-after-free defined? Data races? The latter even more so for objects that don't fit in one machine word, such as slices.

While avoiding undefined behavior is a noble goal, my personal feeling is that actually achieving that will be much harder than it might first seem, and will probably end up precluding a good deal of optimization.

Of course, C has an entire class of UB that is much more excessive than useful, for example left shift of a negative integer. It's clearly and obviously possible to do much better than C. I'm just skeptical that "no UB at all" is in reach for a low-level, systems programming language that is also portable and can be compiled with optimization comparable to C.

comment 4y ago · link · parent

Thinking about this a bit more. If you want an interpolation compatible offset, reduce the search to a one dimensional problem by fixing the area. This gives a simple rational formula for the length of one control arm as a function of the other. Then pick the value of that to minimize your error metric, for which least squares is probably the easiest to solve. I expect this will give you O(n^5) scaling.

I'll probably implement this, as it's very likely what you want in the variable font case.

comment 4y ago · link · parent

It is optimizing for the shape, and the parametrization is not guaranteed to match at all. Whether this is a good thing or a bad thing depends on your application. If you just want the parallel curve, then it's a good thing, as you're putting all possible degrees of freedom into making the curve more accurate (this is why you get O(n^6) scaling). If, say, you wanted to interpolate between the original and the offset, for example adding a grade axis to a variable font, it's a bad thing, as the two curves are not necessarily "interpolation compatible." It's not clear to me which of those you care about :)

I could add a correspondence visualization (I thought about something similar when describing the error measurement technique), but you can also see it pretty clearly by looking at the lengths of the control arms. That's easiest to see when accuracy is set to infinity.

comment 4y ago · link · parent

Yes, and I should probably add those links. Quadratics get you O(n^4) scaling, which is pretty good. Figuring out where to put the control point is easy, there's basically only one place it can go if you want to preserve tangents. Deciding where to subdivide is a little trickier, and Suchowolski's approach looks pretty good to me. You do still need to deal with cusps though.

You can of course change curve representations (I did that with the Euler approach), but there are downsides. Just how bad depends on the application. One place where you really want to stay in a cubic representation is in a font or vector graphics editor. If you just want to add a little weight to the font, ideally you don't want the structure to change, or lots of new control points to appear. In other applications it might be fine though.

comment 4y ago · link · parent

I would be quite flattered by an unethical scholar plagiarizing from my work, it would be far better than being ignored.

The actual conversation about motivation to publish a paper is a fairly deep topic. In this particular case, I do think it's worthwhile for a variety of reasons:

* The literature dates back about 40 years, and no really good solution has been published.

* The most commonly cited survey paper is possibly fundamentally wrong.

* One of the most commonly cited and used techniques is surprisingly bad.

* The curve-fitting technique I propose probably has many other applications; in the best case, it could potentially open up a new field of study.

So part of my motivation for publishing the blog post is to see if I can recruit a partner in crime, someone who has incentive for publication in academic circles. We'll see!

comment 4y ago · link · parent

Yes. The core curve-fitting technique is very general and can be adapted to any scenario where you can sample the desired curve (with normals) and compute a couple of numerical integrals on it. I am confident that includes variable-width strokes, though I also imagine it will be a nontrivial amount of work to make everything fit together in a satisfying fashion.

comment 4y ago · link · parent

Gotcha gotcha. I just coded this up, and averaging the two incident normals performs about the same as OG Tiller-Hanson, slightly worse. The original version (and my implementation) displaces the edge, which basically means computing the new control point as the intersection of the two displaced edges. I believe there are a bunch of T-H variations out there, but haven't collected any evidence yet that they significantly improve matters.

comment 4y ago · link · parent

I'm not sure "degeneracy" is the best way to think about this. If the curvature exceeds the inverse of the offset distance, you get a cusp, and in general you need to think about how to deal with those. In some cases, that will be boolean path operations (stay tuned for an update on those!). But I'm not convinced there's any shortcut where you can avoid dealing with these cases altogether.

comment 4y ago · link · parent

I'm slightly confused by this. A point on a polyline doesn't have a single normal, as it's incident to two edges, and in general those edges have two different normals. If you push out the edge by the offset amount, that's what Tiller and Hanson proposed in 1984, and one of the things I found is that it performs surprisingly badly. It is in common use though.

If you're talking about a variation of that where you choose some other concept of "normal" relevant to the point, I'd be interested to learn more.

474.

Parallel Curves of Cubic Béziers(raphlinus.github.io ↗)

225 points · by raphlinus · 4y ago · 51 comments

comment 4y ago · link · parent

I believe WebGL compute shader development has been halted[1], in favor of making WebGPU happen. Though it's possible to run it in dev builds in some cases, I'm pretty sure there's no browser that has it on by default, and likely this won't happen.

Apple does support WebGL2, but compute shader support is not part of that core spec. The demos[2] certainly don't work in Safari in my quick test.

[1]: https://registry.khronos.org/webgl/specs/latest/2.0-compute/

[2]: https://9ballsyndrome.github.io/WebGL_Compute_shader/webgl-c...

comment 4y ago · link · parent

Ah, my good friends the Fresnel integrals (functions plotting the Euler spiral, also known as clothoid and Cornu spiral) are there in Chapter 7. One of the things I'm proud of is having better techniques for evaluating short sections of that spiral than just computing the functions a la Abramowitz and Stegun (also with good implementations in the cephes library), though of course that does work.

comment 4y ago · link · parent

Agree 100% and I spend a fair amount of time wondering why this hasn't happened. I built piet-gpu-hal because I couldn't find any abstraction layer over compute shaders that supports precompiled shaders. A motivated person absolutely could write shaders to do all the operations needed by Stable Diffusion, and ship a binary in the megabyte range (obviously not counting the models themselves). That would support Metal, Vulkan, and D3D12. The only thing holding this back is a will to build it.

comment 4y ago · link · parent

> PS, is your monoid work online, is it this https://arxiv.org/abs/2205.11659 ? I'm interested in monoids on GPUs.

Yes, that's the draft on arXiv, and I have a blog post in the pipeline[1] explaining it to a more general audience.

And thanks for the other advice, I'll consider it!

[1] https://github.com/raphlinus/raphlinus.github.io/issues/66

comment 4y ago · link · parent

This topic is close to my heart. I do research on the fundamentals of 2D graphics, some of it cutting edge (especially GPU techniques), others just selecting the best known techniques (for example, right now I'm doing a bit of a deep dive into robust cubic and quartic polynomial root finders, not really academically publishable, but potentially hugely useful for others working on similar problems).

I made a serious attempt to submit some of my GPU monoid work to academic conferences earlier this year, got rejected. As someone who doesn't have the "publish or perish" incentive of actually being in academia, it's really just not worth it.

I'm also doing inquiry into UI, for example architectural patterns in reactive systems. The current state of academic literature on this topic seems to be terrible (though please point out counterexamples!). Most of what's written is marketing material for UI frameworks - there's an explosion of those, especially in the JavaScript world, but very little synthesis of the core concepts. I wouldn't even attempt to try to submit an academic paper on this topic, though I think it would likely be useful to the world.

It feels like there should be a space to publish work that is not novel in an academic sense, but useful. Wikipedia is not it (their articles on cubic and quartic equations are garbage when it comes to numerical concerns), Stack Overflow is not it, academic conferences and journals are not it. I'm using my blog, generally successfully, but it feels there should be a more systematic approach.

comment 4y ago · link · parent

The first implementation will be in the kurbo codebase, in Rust, permissively licensed. If it works out as well as I'd like, the code will be only moderately complex, which I hope means others can implement the ideas without too much difficulty as well.
1
151617
63