<feed xmlns='http://www.w3.org/2005/Atom'>
<title>mirrors/foot.git/grid.c, branch releases/1.8</title>
<subtitle>A fast, lightweight and minimalistic Wayland terminal emulator</subtitle>
<id>https://git.0x0f.dev/mirrors/foot.git/atom?h=releases%2F1.8</id>
<link rel='self' href='https://git.0x0f.dev/mirrors/foot.git/atom?h=releases%2F1.8'/>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/'/>
<updated>2021-06-24T15:30:49Z</updated>
<entry>
<title>composed: store compose chains in a binary search tree</title>
<updated>2021-06-24T15:30:49Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-06-24T11:17:07Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=fe8ca23cfee77a6cac433892755dd7455e354a9d'/>
<id>urn:sha1:fe8ca23cfee77a6cac433892755dd7455e354a9d</id>
<content type='text'>
The previous implementation stored compose chains in a dynamically
allocated array. Adding a chain was easy: resize the array and append
the new chain at the end. Looking up a compose chain given a compose
chain key/index was also easy: just index into the array.

However, searching for a pre-existing chain given a codepoint sequence
was very slow. Since the array wasn’t sorted, we typically had to scan
through the entire array, just to realize that there is no
pre-existing chain, and that we need to add a new one.

Since this happens for *each* codepoint in a grapheme cluster, things
quickly became really slow.

Things were ok:ish as long as the compose chain struct was small, as
that made it possible to hold all the chains in the cache. Once the
number of chains reached a certain point, or when we were forced to
bump maximum number of allowed codepoints in a chain, we started
thrashing the cache and things got much much worse.

So what can we do?

We can’t sort the array, because

a) that would invalidate all existing chain keys in the grid (and
iterating the entire scrollback and updating compose keys is *not* an
option).

b) inserting a chain becomes slow as we need to first find _where_ to
insert it, and then memmove() the rest of the array.

This patch uses a binary search tree to store the chains instead of a
simple array.

The tree is sorted on a “key”, which is the XOR of all codepoints,
truncated to the CELL_COMB_CHARS_HI-CELL_COMB_CHARS_LO range.

The grid now stores CELL_COMB_CHARS_LO+key, instead of
CELL_COMB_CHARS_LO+index.

Since the key is truncated, collisions may occur. This is handled by
incrementing the key by 1.

Lookup is of course slower than before, O(log n) instead of
O(1).

Insertion is slightly slower as well: technically it’s O(log n)
instead of O(1). However, we also need to take into account the
re-allocating the array will occasionally force a full copy of the
array when it cannot simply be growed.

But finding a pre-existing chain is now *much* faster: O(log n)
instead of O(n). In most cases, the first lookup will either
succeed (return a true match), or fail (return NULL). However, since
key collisions are possible, it may also return false matches. This
means we need to verify the contents of the chain before deciding to
use it instead of inserting a new chain. But remember that this
comparison was being done for each and every chain in the previous
implementation.

With lookups being much faster, and in particular, no longer requiring
us to check the chain contents for every singlec chain, we can now use
a dynamically allocated ‘chars’ array in the chain. This was
previously a hardcoded array of 10 chars.

Using a dynamic allocated array means looking in the array is slower,
since we now need two loads: one to load the pointer, and a second to
load _from_ the pointer.

As a result, the base size of a compose chain (i.e. an “empty” chain)
has now been reduced from 48 bytes to 32. A chain with two codepoints
is 40 bytes. This means we have up to 4 codepoints while still using
less, or the same amount, of memory as before.

Furthermore, the Unicode random test (i.e. write random “unicode”
chars) is now **faster** than current master (i.e. before text-shaping
support was added), **with** test-shaping enabled. With text-shaping
disabled, we’re _even_ faster.
</content>
</entry>
<entry>
<title>grid: reflow: ‘amount’ has already been added to ‘from’</title>
<updated>2021-06-02T18:13:52Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-06-02T17:30:01Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=3292bb5b8eaddb8a0354387fcc2d8f9ffd0d1ac4'/>
<id>urn:sha1:3292bb5b8eaddb8a0354387fcc2d8f9ffd0d1ac4</id>
<content type='text'>
</content>
</entry>
<entry>
<title>grid: reflow: URI range start: take over ownership of URI string</title>
<updated>2021-06-02T18:13:52Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-28T16:19:21Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=a003e56fdc2300818760bcdf020219604bb285ab'/>
<id>urn:sha1:a003e56fdc2300818760bcdf020219604bb285ab</id>
<content type='text'>
Instead of strdup:ing the URI, take over ownership. This is ok since
the old URI range will be free:d anyway.
</content>
</entry>
<entry>
<title>grid: reflow: debug logging</title>
<updated>2021-06-02T18:13:51Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-28T15:42:25Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=8a9643de67458e1958839e994350621e4588808f'/>
<id>urn:sha1:8a9643de67458e1958839e994350621e4588808f</id>
<content type='text'>
</content>
</entry>
<entry>
<title>grid: reflow: when determining row end coord, check *last* URI range</title>
<updated>2021-06-02T18:13:51Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-28T15:42:09Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=ceab9b93675f523f25b1eea410cd42d408d8c020'/>
<id>urn:sha1:ceab9b93675f523f25b1eea410cd42d408d8c020</id>
<content type='text'>
</content>
</entry>
<entry>
<title>grid: reflow: when determining row end coord, check *last* tracking point</title>
<updated>2021-06-02T18:13:51Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-28T15:41:47Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=5a08ed641b39fcd84373ac120ecfb2d11f9987b9'/>
<id>urn:sha1:5a08ed641b39fcd84373ac120ecfb2d11f9987b9</id>
<content type='text'>
</content>
</entry>
<entry>
<title>grid: reflow: check the *entire* row for non-empty cells</title>
<updated>2021-06-02T18:13:51Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-28T15:41:13Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=ef1fdc40c8fd10411e7c40e0fe0d6982a702f505'/>
<id>urn:sha1:ef1fdc40c8fd10411e7c40e0fe0d6982a702f505</id>
<content type='text'>
</content>
</entry>
<entry>
<title>grid: reflow: avoid unnecessary if-statements before chunking a row</title>
<updated>2021-06-02T18:13:51Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-26T10:27:12Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=c2314d689e9e36dc4e104051e2c2b15d0e71d99b'/>
<id>urn:sha1:c2314d689e9e36dc4e104051e2c2b15d0e71d99b</id>
<content type='text'>
</content>
</entry>
<entry>
<title>grid: disable reflow timing by default</title>
<updated>2021-06-02T18:13:51Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-26T10:22:28Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=2029d201b571753585f5df1b0f7265adfca722a5'/>
<id>urn:sha1:2029d201b571753585f5df1b0f7265adfca722a5</id>
<content type='text'>
</content>
</entry>
<entry>
<title>grid: reflow: comments</title>
<updated>2021-06-02T18:13:51Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-25T17:40:10Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=ac97f20f997e300f87cb4c38ace331e529168d66'/>
<id>urn:sha1:ac97f20f997e300f87cb4c38ace331e529168d66</id>
<content type='text'>
</content>
</entry>
<entry>
<title>grid: no need to keep the tp_col/uri_col variables around</title>
<updated>2021-06-02T18:13:51Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-25T17:34:21Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=5325ea042decae3d44d1cba54abb782a6fcd532c'/>
<id>urn:sha1:5325ea042decae3d44d1cba54abb782a6fcd532c</id>
<content type='text'>
</content>
</entry>
<entry>
<title>grid: fix col max calculation when row contains URI ranges</title>
<updated>2021-06-02T18:13:51Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-25T17:32:25Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=3453f091a3349e6a977d69e07c1be6d3fe39c939'/>
<id>urn:sha1:3453f091a3349e6a977d69e07c1be6d3fe39c939</id>
<content type='text'>
</content>
</entry>
<entry>
<title>grid: set tp/uri break flags explicitly when we know them to be true</title>
<updated>2021-06-02T18:13:51Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-25T17:31:38Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=a56b54ad2f3cc0f5d43a1f32227d4f3867c07867'/>
<id>urn:sha1:a56b54ad2f3cc0f5d43a1f32227d4f3867c07867</id>
<content type='text'>
</content>
</entry>
<entry>
<title>grid: reflow: slightly simplified logic for end-coordinate calculation</title>
<updated>2021-06-02T18:13:51Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-25T17:28:11Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=4b7e4fb885a3d9aa7fa2152ee9439964a420ea20'/>
<id>urn:sha1:4b7e4fb885a3d9aa7fa2152ee9439964a420ea20</id>
<content type='text'>
</content>
</entry>
<entry>
<title>grid: reflow: rename _range -&gt; range</title>
<updated>2021-06-02T18:13:51Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-25T17:22:52Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=315865f18ca5b12dbf93ee4cdebd1b4116dadd1d'/>
<id>urn:sha1:315865f18ca5b12dbf93ee4cdebd1b4116dadd1d</id>
<content type='text'>
</content>
</entry>
<entry>
<title>grid: reflow: remove dead code</title>
<updated>2021-06-02T18:13:50Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-25T17:20:25Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=7c3a4b24d9358a65331a5c4e8b134e8df637ccdf'/>
<id>urn:sha1:7c3a4b24d9358a65331a5c4e8b134e8df637ccdf</id>
<content type='text'>
</content>
</entry>
<entry>
<title>grid: reflow: memcpy() chunks of cells, instead of single cell-by-cell</title>
<updated>2021-06-02T18:13:50Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-23T19:15:06Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=40ca86b2d30873ac13c73360a777cfa40b705a7e'/>
<id>urn:sha1:40ca86b2d30873ac13c73360a777cfa40b705a7e</id>
<content type='text'>
Instead of walking the old grid cell-by-cell, and checking for
tracking points, OSC-8 URIs etc on each cell, memcpy() sequences of
cells.

For each row, find the end column, by scanning backward, looking for
the first non-empty cell.

Chunk the row based on tracking point coordinates. If there aren’t any
tracking coordinates, or OSC-8 URIs on the current row, the entire row
is copied in one go.

The chunk of cells is copied to the new grid. We may have to split it
up into multiple copies, since not all cells may fit on the current
“new” row.

Care must also be taken to not line break in the middle of a
multi-column character.
</content>
</entry>
<entry>
<title>grid: don’t cut multi-column chars in half when resizing the alt screen</title>
<updated>2021-06-02T17:32:05Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-06-02T17:32:05Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=b7709cc013766c40a2f7cb286a043e84411d5dfa'/>
<id>urn:sha1:b7709cc013766c40a2f7cb286a043e84411d5dfa</id>
<content type='text'>
When we resize the alt screen, we don’t reflow the text, we simply
truncate all the lines.

When doing this, make sure we don’t truncate in the middle of a
multi-column character.
</content>
</entry>
<entry>
<title>grid: reflow: uri-ranges: avoid looping URI ranges when reflowing</title>
<updated>2021-05-23T08:29:45Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-22T18:29:10Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=9a849b25cc01df9ace9dd775f72655a972bf5ec0'/>
<id>urn:sha1:9a849b25cc01df9ace9dd775f72655a972bf5ec0</id>
<content type='text'>
Since we know the following:

* URI ranges are sorted
* URI coordinates are unique
* URI ranges don’t cross rows

We can optimize URI range reflowing by:

* Checking if the *first* URI range’s start coordinate is on the
  current column. If so, keep a pointer to it.
* Use this pointer as source when instantiating the reflowed URI range
* If we already have a non-NULL range pointer, check its end
  coordinate instead.
* If it matches, close the *last* URI range we inserted on the new
  row, and free/remove the range from the old row.
* When line breaking, we only need to check if the *last* URI range is
  unclosed.
</content>
</entry>
<entry>
<title>grid: row_add_uri_range: ensure the URIs are sorted</title>
<updated>2021-05-22T16:16:54Z</updated>
<author>
<name>Daniel Eklöf</name>
<email>daniel@ekloef.se</email>
</author>
<published>2021-05-22T15:12:58Z</published>
<link rel='alternate' type='text/html' href='https://git.0x0f.dev/mirrors/foot.git/commit/?id=7272a5469ef602458545d8b5409509d29fefb86d'/>
<id>urn:sha1:7272a5469ef602458545d8b5409509d29fefb86d</id>
<content type='text'>
</content>
</entry>
</feed>
