Better HN
topnewbestaskshowjobs
Log inSign up

mikeeeek

2 karma · joined Aug 5, 2019 (7y ago) · 3 submissions

Submissions

comment 6y ago · link · parent

interesting idea, although not obvious this would have the intended affect. E.g., consider a string search across 10 different string objects (thus 10 different backing arrays), vs a search across a single array. Much more likely for increase in cache misses when you hit 10 different arrays at potentially 10 different locations in memory.

Also, this implies some copy-on-write semantics. At first thought, this makes code significantly more complex.

comment 6y ago · link · parent

Briefly looking at the code:

  1. data structures don't support efficient insertion/removal at arbitrary positions in the string

  2. "replace" method is O(n) where n == length of the string

  3. string searching inside replace is sub-optimal. e.g., given buffer of "aaaa" and replace("aaab", "xxxx"), you need to do 4 comparisons to determine there is no match.
1 is easily rectified by using a gap buffer, or possibly a rope or a piece table, but to be honest Gap Buffer is so simple.

For 2, given a more efficient backing store (e.g., gap buffer), you should be able to eliminate the O(n) buffer copy.

For 3, you can implement Boyer-Moore string search.

Gap Buffer: https://en.wikipedia.org/wiki/Gap_buffer

Rope: https://en.wikipedia.org/wiki/Rope_%28data_structure%29

Piece Table: https://en.wikipedia.org/wiki/Piece_table

Boyer-Moore: https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string-sea...

comment 7y ago · link · parent

use a gap buffer: https://en.wikipedia.org/wiki/Gap_buffer.

For scaling large docs, you can do a linked list of gap buffers and avoid re-allocs of large buffers.

Super simple, efficient enough for most “i wrote my own text editor”, can always plug in a more complex structure later.

Author’s implementation will drag down if you have very long lines (e.g., transpiled JS).