Skip to content

Pre-allocate some ShapedText storage - #702

Merged
tomcur merged 1 commit into
linebender:mainfrom
tomcur:push-tloqtrpoutms
Jul 21, 2026
Merged

Pre-allocate some ShapedText storage#702
tomcur merged 1 commit into
linebender:mainfrom
tomcur:push-tloqtrpoutms

Conversation

@tomcur

@tomcur tomcur commented Jul 21, 2026

Copy link
Copy Markdown
Member

Implementing #679 (comment) seems to be a nice little speed up.

This pre-allocates at the granularity of items, which means the glyph allocation is speculative. An alternative is to pre-allocate at the smaller granularity of runs, once you know how many glyphs you need to push, but that turns out to be slower as you need to reallocate more often.

We could expose ShapedText::reserve publicly and let users handle pre-allocation (at, e.g., the paragraph level), which theoretically could be slightly faster still, but that adds API surface for something that mostly stops mattering once you're reusing the ShapedText allocation.

$ cargo bench --bench main -- compare ../target/benchmarks/main -t 8.
Default Style - arabic 20 characters               [   9.0 us ...   8.8 us ]      -2.09%*
Default Style - latin 20 characters                [   4.3 us ...   4.1 us ]      -3.14%*
Default Style - japanese 20 characters             [   8.3 us ...   8.3 us ]      -0.33%
Default Style - arabic 1 paragraph                 [  48.7 us ...  47.8 us ]      -1.80%*
Default Style - latin 1 paragraph                  [  16.8 us ...  16.5 us ]      -2.07%*
Default Style - japanese 1 paragraph               [  70.6 us ...  70.1 us ]      -0.76%
Default Style - arabic 4 paragraph                 [ 208.1 us ... 201.5 us ]      -3.13%*
Default Style - latin 4 paragraph                  [  64.0 us ...  62.8 us ]      -1.84%*
Default Style - japanese 4 paragraph               [  99.9 us ...  98.8 us ]      -1.03%*
Styled - arabic 20 characters                      [  10.1 us ...   9.8 us ]      -2.32%*
Styled - latin 20 characters                       [   5.4 us ...   5.3 us ]      -1.94%*
Styled - japanese 20 characters                    [   8.9 us ...   9.0 us ]      +1.59%*
Styled - arabic 1 paragraph                        [  51.2 us ...  50.2 us ]      -1.86%*
Styled - latin 1 paragraph                         [  21.4 us ...  21.1 us ]      -1.11%*
Styled - japanese 1 paragraph                      [  77.2 us ...  76.7 us ]      -0.73%
Styled - arabic 4 paragraph                        [ 227.8 us ... 221.3 us ]      -2.88%*
Styled - latin 4 paragraph                         [  83.2 us ...  82.3 us ]      -1.09%*
Styled - japanese 4 paragraph                      [ 109.1 us ... 108.1 us ]      -0.98%

Implementing
linebender#679 (comment)
seems to be a nice little speed up.

```
$ cargo bench --bench main -- compare ../target/benchmarks/main -t 8.
Default Style - arabic 20 characters               [   9.0 us ...   8.8 us ]      -2.09%*
Default Style - latin 20 characters                [   4.3 us ...   4.1 us ]      -3.14%*
Default Style - japanese 20 characters             [   8.3 us ...   8.3 us ]      -0.33%
Default Style - arabic 1 paragraph                 [  48.7 us ...  47.8 us ]      -1.80%*
Default Style - latin 1 paragraph                  [  16.8 us ...  16.5 us ]      -2.07%*
Default Style - japanese 1 paragraph               [  70.6 us ...  70.1 us ]      -0.76%
Default Style - arabic 4 paragraph                 [ 208.1 us ... 201.5 us ]      -3.13%*
Default Style - latin 4 paragraph                  [  64.0 us ...  62.8 us ]      -1.84%*
Default Style - japanese 4 paragraph               [  99.9 us ...  98.8 us ]      -1.03%*
Styled - arabic 20 characters                      [  10.1 us ...   9.8 us ]      -2.32%*
Styled - latin 20 characters                       [   5.4 us ...   5.3 us ]      -1.94%*
Styled - japanese 20 characters                    [   8.9 us ...   9.0 us ]      +1.59%*
Styled - arabic 1 paragraph                        [  51.2 us ...  50.2 us ]      -1.86%*
Styled - latin 1 paragraph                         [  21.4 us ...  21.1 us ]      -1.11%*
Styled - japanese 1 paragraph                      [  77.2 us ...  76.7 us ]      -0.73%
Styled - arabic 4 paragraph                        [ 227.8 us ... 221.3 us ]      -2.88%*
Styled - latin 4 paragraph                         [  83.2 us ...  82.3 us ]      -1.09%*
Styled - japanese 4 paragraph                      [ 109.1 us ... 108.1 us ]      -0.98%
```
@tomcur
tomcur added this pull request to the merge queue Jul 21, 2026
Merged via the queue into linebender:main with commit 6c81e1d Jul 21, 2026
24 checks passed
@tomcur
tomcur deleted the push-tloqtrpoutms branch July 21, 2026 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants