Skip to main content

New top story on Hacker News: Ask HN: I just wrote an O(N) diffing algorithm – what am I missing?

Ask HN: I just wrote an O(N) diffing algorithm – what am I missing?
7 by keithwhor | 5 comments on Hacker News.
Hey folks, I've been building a rendering engine for a code editor the past couple of days. Rendering huge chunks of highlighted syntax can get laggy. It's not worth switching to React at this stage, so I wanted to just write a quick diff algorithm that would selectively update only changed lines. I found this article: http://bit.ly/2MXx3ol With a link to this paper, the initial Git diff implementation: http://bit.ly/2MRG8Pf I couldn't find the PDF to start with, but read "edit graph" and immediately thought — why don't I just use a hashtable to store lines from LEFT_TEXT and references to where they are, then iterate over RIGHT_TEXT and return matches one by one, also making sure that I keep track of the last match to prevent jumbling? The algorithm I produced is only a few lines and seems accurate. It's O(N) time complexity, whereas the paper above gives a best case of O(ND) where D is minimum edit distance. function lineDiff (left, right) { left = left.split('\n'); right = right.split('\n'); let lookup = {}; // Store line numbers from LEFT in a lookup table left.forEach(function (line, i) { lookup[line] = lookup[line] || []; lookup[line].push(i); }); // Last line we matched var minLine = -1; return right.map(function (line) { lookup[line] = lookup[line] || []; var lineNumber = -1; if (lookup[line].length) { lineNumber = lookup[line].shift(); // Make sure we're looking ahead if (lineNumber > minLine) { minLine = lineNumber; } else { lineNumber = -1 } } return { value: line, from: lineNumber }; }); } RunKit link: http://bit.ly/2BR89QU What am I missing? I can't find other references to doing diffing like this. Everything just links back to that one paper.

Comments

Popular posts from this blog

Artificial intelligence model finds potential drug molecules a thousand times faster

submitted by /u/Sweep145 [link] [comments] from /r/Technology https://bit.ly/3IEQZaK via IFTTT

Tesla is raising the price of its full self-driving option

In a few weeks, Tesla buyers will have to pay more for an option that isn’t yet completely functional, but that CEO Elon Musk promises will one day deliver full autonomous driving capabilities. Musk tweeted Saturday that the price of its full self-driving option will “increase substantially over time” beginning May 1. Tesla vehicles are not self-driving. Musk has promised that the advanced driver assistance capabilities on Tesla vehicles will continue to improve until eventually reaching that full automation high-water mark. Please note that the price of the Tesla Full Self-Driving option will increase substantially over time — Elon Musk (@elonmusk) April 13, 2019 Musk didn’t provide a specific figure, but in response to a question on Twitter, he said the increase would be “something like” around the $3,000+ figure. Full self-driving currently costs $5,000. Something like that — Elon Musk (@elonmusk) April 13, 2019 The price hike comes amid several notable changes a...