News for hackers. Stuff that matters.
Re:Open-source online SVG path editor by chrismorgan on Friday February 18, 2022 @07:33AM (#30384928)
That’s not actually very good or compact path data:

• It has three extraneous points: each rectangle is comprised of five points, because it draws to the start point manually (V6/V11/V17), and then says draw to close the path (z). The V6/V11/V17 should just be dropped: z covers that. In fact, even each z can be dropped, provided no stroke is being used, removing three edges.

• It misses compression opportunities, by having the first one go counterclockwise and the remaining two go clockwise, and by using absolute coordinates in places where the same relative coordinate could be used (e.g. V11/V17 could both have been v-1, and if the first one’s direction was sorted out then that V5 could also be v-1; but we’re eliminating these vees anyway, so—).

All up, that hamburger menu data of this:

  M21,6H3v-1h18z M21,11H3v1h18V11z M21,17H3v1h18V17z
Could be replaced with this path data which is more compact, more compressible, and faster to draw by probably at least a femtosecond or two:

  M21 5H3v1h18M21 11H3v1h18M21 17H3v1h18
Note in that how `M21 ` and `H3v1h18` are each repeated three times, with `5`, `11` and `17` being the only things that appear once. This attention to maximising exact repetition will save another few bytes.

Depending on how you do things, it could even be better to replace the 12-point fill with a 6-point stroke (though I freely admit that more care is required because of stroke-width, stroke-linecap, stroke-linejoin, mixing fill and stroke, &c.):

  <path d="M21 5H3v1h18M21 11H3v1h18M21 17H3v1h18" fill="currentColor" stroke="none"/>

  <path d="M3 5.5h18m-18 6h18m-18 6h18" fill="none" stroke="currentColor" stroke-width="1"/>
Re:Open-source online SVG path editor by rickosborne on Friday February 18, 2022 @09:43AM (#30386285)
FWIW, your optimizations assume well-behaved SVG handling. This one, for instance, will cause problems:

> In fact, even each z can be dropped, provided no stroke is being used, removing three edges.

While that may be true according to the spec, I can tell you from a practical standpoint that it will break in Lightburn. It's SVG handling does unexpected things with implicitly closed paths, which suddenly go away when you explicitly move/stroke back to your starting point or "z".

I have not looked deeper into why, but if I had to make a WAG, I'd suspect it comes from their implementation trying to bridge the gap between SVG generators (which are optimized for "shape looks correct") and machine control instructions (which target the lower level "here are the steps to make this shape look correct"). You're then having to take something simple like a path and adding in the complexities of making machine control instructions for the practical rendering of that path, accounting for things like miters, etc. Seems like a complex layer, prone to tons of annoying little bugs like this.

I presume YouTube's designers/coders aren't worried about being able to run their UI widgets through a laser engraver, but I tend to chalk up these kind of "missed optimizations" to having some similar backstory of "we found this bug in this implementation, so here's the workaround".

Re:Open-source online SVG path editor by chrismorgan on Friday February 18, 2022 @10:01AM (#30386567)
I’ve never heard of LightBurn. I presume you’re talking about the laser cutter software? If so, not knowing what it’s actually doing, I presume it’s just cutting along the stroke, rather than trying to do, I dunno, cross-hatch a fill or something? This case is specifically about fill, as I said, so if it’s just a matter of stroke then it’s quite correct in what it’s doing. If it actually is treating it as fill and getting it wrong, then that’s a bug that should be fixed and I have no patience with their implementation because it’s obviously wrong (as in: you’ll come across SVG like this all the time, there’s absolutely nothing anomalous about it).
Re:Open-source online SVG path editor by bavell on Friday February 18, 2022 @12:53PM (#30388867)
These are the comment threads I love HN for.
Re:Open-source online SVG path editor by leavenotracks on Friday February 18, 2022 @03:11PM (#30390439)
+1 yes! this got deep very quickly
Re:Open-source online SVG path editor by jansan on Friday February 18, 2022 @07:40AM (#30385004)
You can make this even 4 chars shorter by using relative move-to commands (small 'm'):

    M21 5H3v1h18m0 5H3v1h18m0 5H3v1h18
Re:Open-source online SVG path editor by chrismorgan on Friday February 18, 2022 @07:50AM (#30385087)
Hmm, I contemplated that at first but decided against it for some reason that’s not quite clear to me now, but I think I mispredicted its verbosity in both literal and compressed form.

For compression purposes, mine is going to be kinda like this:

  ‹M21 ›5‹H3v1h18›‹M21 ›11‹H3v1h18›‹M21 ›17‹H3v1h18›
Yours will probably treat only one block, slightly bigger, as a repeated thing:

  M21‹ 5H3v1h18›m0‹ 5H3v1h18›m0‹ 5H3v1h18›
I expect that would compress 2–5 bytes smaller.

I’d lowercase that M, too; it doesn’t need to be uppercase, so it might as well be lowercase like the others!

Re:Open-source online SVG path editor by mproud on Friday February 18, 2022 @11:49AM (#30388058)
A couple fewer bits or bytes is not going to make a noticeable performance difference.
Re:Open-source online SVG path editor by chrismorgan on Friday February 18, 2022 @09:35PM (#30393923)
So? Golfing things like this is mostly about fun, with a side benefit of being aware of things that, on a (much) larger scale, do start to matter.