Skip to content

Add a fast path for shaping ASCII text - #11273

Merged
bors-servo merged 6 commits into
servo:masterfrom
mbrubeck:fast-shape
May 21, 2016
Merged

bors-servo merged 6 commits into
servo:masterfrom
mbrubeck:fast-shape

Conversation

@mbrubeck

@mbrubeck mbrubeck commented May 19, 2016 •

Copy link
Copy Markdown
Contributor

On both my Linux laptop and iMac, this is about twice as fast as Harfbuzz text shaping on https://en.wikipedia.org/wiki/Barack_Obama.

I haven't tested this on any high-DPI (retina) displays, and I'm not 100% certain that the font unit scaling is correct there.

Depends on servo/core-text-rs#50.


This change is Reviewable

@highfive

Copy link
Copy Markdown

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!
  • These commits modify gfx code, but no tests are modified. Please consider adding a test!

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label May 19, 2016
@mbrubeck mbrubeck changed the title r? @pcwalton Add a fast path for shaping ASCII text May 19, 2016
@mbrubeck

Copy link
Copy Markdown
Contributor Author

r? @pcwalton

@highfive highfive assigned pcwalton and unassigned glennw May 19, 2016
@mbrubeck mbrubeck added A-platform/fonts I-perf-slow Unnecessary performance degredation. A-layout/text S-blocked-on-external Something, somewhere else, needs to happen before this PR can be merged. labels May 19, 2016
let value = BigEndian::read_i16(&pairs[i * KERN_PAIR_LEN + 4..]);
trace!("Kerning for {}/{}: {:?}", first_glyph, second_glyph, value);
return value as f64 * table.scale;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this use Vec::binary_search()?

Even if not, I still think I'd rather have this be a separate function.

@highfive

Copy link
Copy Markdown

New code was committed to pull request.

@mbrubeck mbrubeck removed the S-blocked-on-external Something, somewhere else, needs to happen before this PR can be merged. label May 19, 2016
handle: fctx.clone(),
can_do_fast_kerning: false,
};
handle.can_do_fast_kerning = handle.has_table(KERN) || !handle.has_table(GPOS);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I would expect handle.has_table(KERN) && !handle.has_table(GPOS). Am I missing something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to has_table(KERN) && !has_table(GPOS) for now.

@highfive

Copy link
Copy Markdown

New code was committed to pull request.

@mbrubeck

Copy link
Copy Markdown
Contributor Author

Addressed review comments.

@pcwalton:

Hmm. I would expect handle.has_table(KERN) && !handle.has_table(GPOS). Am I missing something?

I assumed that if a font has both 'kern' and 'GPOS', then falling back to the 'kern' table would be sufficient for shaping ASCII-only strings. It would be more conservative to use the slow path in this case, however. This affects some commonly-used fonts like ArialMT and TimesNewRoman on Mac OS, and the DejaVu fonts on Linux.

Even if we implement partial GPOS support in the fast path, we'll need to make a similar assumption that ASCII characters aren't affected by any unsupported GPOS features (or, somehow scan the whole table to verify this).

We could also make this depend on the text-rendering CSS property…

@pcwalton

Copy link
Copy Markdown
Contributor

@mbrubeck I was actually assuming we'd scan the table to verify this. I looked at the tables in FontForge and they seemed to pass the test from first glance.

But this can be a followup.

@highfive

Copy link
Copy Markdown

New code was committed to pull request.

@highfive

Copy link
Copy Markdown

New code was committed to pull request.

@highfive

Copy link
Copy Markdown

New code was committed to pull request.

@highfive

Copy link
Copy Markdown

New code was committed to pull request.

}
let num_subtables = BigEndian::read_u16(&table[2..]);
let mut start = 4;
for _ in 0..num_subtables {

@pcwalton pcwalton May 20, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda weird that the spec doesn't say what the subtable version number is supposed to be. Maybe add a TODO to perhaps check this in the future?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@pcwalton

Copy link
Copy Markdown
Contributor

Looks good, just had a couple of nits!

@highfive

Copy link
Copy Markdown

New code was committed to pull request.

@mbrubeck

Copy link
Copy Markdown
Contributor Author

@bors-servo r=pcwalton

  • Fixed nits. Here we go…

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit 5991afa has been approved by pcwalton

@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels May 20, 2016
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit 5991afa with merge c0c70ef...

bors-servo pushed a commit that referenced this pull request May 21, 2016
Add a fast path for shaping ASCII text

On both my Linux laptop and iMac, this is about twice as fast as Harfbuzz text shaping on https://en.wikipedia.org/wiki/Barack_Obama.

I haven't tested this on any high-DPI (retina) displays, and I'm not 100% certain that the font unit scaling is correct there.

Depends on servo/core-text-rs#50.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/11273)
<!-- Reviewable:end -->
@bors-servo

Copy link
Copy Markdown
Contributor

☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel, mac-dev-unit, mac-rel-css, mac-rel-wpt, windows

@bors-servo
bors-servo merged commit 5991afa into servo:master May 21, 2016
@highfive highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label May 21, 2016
@khaledhosny

Copy link
Copy Markdown

I think this is going backwards, Geko does not have a fast path and Blink is killing its fast path as well because it is proven to be a bad idea on the long run. HarfBuzz should be pretty fast when no OpenType layout is involved anyway, so if the code is slow then this is probably an issue in the Servo side.

CC @behdad.

@pcwalton

pcwalton commented May 29, 2016 •

Copy link
Copy Markdown
Contributor

The fast path in WebKit doesn't do kerning at all from what I can tell. That's why it's being removed. But our fast path is different. By design, our fast path only kicks in when we detect that there will be no quality difference between HarfBuzz and what we do. It's been shown to result in a 2x speedup, and I don't see anything wrong that we're doing in Servo that would cause HB to be slow.

Additionally, we have potential vectorization opportunities, so we may well pull further ahead of HB with this fast path in the long run.

@khaledhosny

Copy link
Copy Markdown

I don’t know Rust that well, but the code seems to do the fast path when the font has a kern table and no GPOS or GSUB, but this is probably to match any AAT font, so on Mac OS X you won’t get any layout features for such fonts (not even kerning since will unlikely be a version 0 kern table). This would also match any Graphite font having a kern table (though I don’t know if it is common to have such a table in Graphite fonts).

Also if HarfBuzz path in Servo is slow, I think making it faster benefits everyone not only user of ASCII text and fonts with no OpenType layout table. Both Gecko and Blink cache the shaping result in various way to speed things up.

@mbrubeck

mbrubeck commented Jun 7, 2016 •

Copy link
Copy Markdown
Contributor Author

This is still an experiment; it remains to be seen whether it will still be faster than HarfBuzz on typical fonts once it supports ligatures and other OpenType features. Perhaps it won't be. If we do find any significant wins, I'll definitely see if they they can be shared with HarfBuzz.

Part of the speedup here may be from better inlining. (We might also be able to get this from Harfbuzz by compiling it with Clang and using cross-language LTO, which is also something we plan to pursue.) Part of it is also from writing data directly into Servo's internal data structures without allocating any intermediate buffers. And having a separate path for ASCII inside of Servo might be a win whether or not it uses HarfBuzz.

this is probably to match any AAT font, so on Mac OS X you won’t get any layout features for such fonts

Yeah, we ought to disable the fast path if any ASCII character is covered by a table that it doesn't support. I'm working on this.

@behdad

behdad commented Jun 9, 2016

Copy link
Copy Markdown

This is so not what we expect HarfBuzz clients to be doing... My 0.02USD.

mrobinson added a commit to mrobinson/servo that referenced this pull request Nov 25, 2026
For years, Servo has had a rudimentary shaper that read the `kern` tables
and tried to do a very basic version of shaping itself. This change
removes that shaper and all associated code because:

1. It was rarely used: the fast shaper only ran on LTR ASCII text for
   fonts with neither `GSUB` nor `GPOS` tables. That covers a very small
   number of situations.
2. It isn't really faster: My measurements showed that even when bending
   over backwards to use the fast shaper the difference in performance
   was within noise or slower on both macOS and Linux. HarfBuzz is a lot
   faster nowadays and we do a lot more caching.
3. It produces inaccurate results. My testing showed that the fast
   shaper can produce degraded shaping results in certain conditions.
4. It added a lot of code complexity and was always considered to be a
   bad idea. For example see this comment:
   <servo#11273 (comment)>.

There are interesting optimizations to shaping coming, so we should
finally retire this workaround for a real shaper.

Signed-off-by: Martin Robinson <[email protected]>
Daniel-1600 pushed a commit to Daniel-1600/servo that referenced this pull request Nov 25, 2026
For years, Servo has had a rudimentary shaper that read the `kern`
tables and tried to do a very basic version of shaping itself. This
change removes that shaper and all associated code because:

1. It was rarely used: the fast shaper only ran on LTR ASCII text for
fonts with neither `GSUB` nor `GPOS` tables. That covers a very small
number of situations.
2. It isn't really faster: My measurements showed that even when bending
over backwards to use the fast shaper the difference in performance was
within noise or slower on both macOS and Linux. HarfBuzz is a lot faster
nowadays and we do a lot more caching.
3. It produces inaccurate results. My testing showed that the fast
shaper can produce degraded shaping results in certain conditions.
4. It added a lot of code complexity and was always considered to be a
bad idea. For example see this comment:
<servo#11273 (comment)>.

There are interesting optimizations to shaping coming, so we should
finally retire this workaround for a real shaper.

Testing: This should have a very tiny change in behavior, which is
unlikely
to be reflected in test results. There isn't a good way to test removal
of this obsolete fast path.

Signed-off-by: Martin Robinson <[email protected]>
devin-ai-integration Bot pushed a commit to baigojahanzaib/servo that referenced this pull request Nov 28, 2026
For years, Servo has had a rudimentary shaper that read the `kern`
tables and tried to do a very basic version of shaping itself. This
change removes that shaper and all associated code because:

1. It was rarely used: the fast shaper only ran on LTR ASCII text for
fonts with neither `GSUB` nor `GPOS` tables. That covers a very small
number of situations.
2. It isn't really faster: My measurements showed that even when bending
over backwards to use the fast shaper the difference in performance was
within noise or slower on both macOS and Linux. HarfBuzz is a lot faster
nowadays and we do a lot more caching.
3. It produces inaccurate results. My testing showed that the fast
shaper can produce degraded shaping results in certain conditions.
4. It added a lot of code complexity and was always considered to be a
bad idea. For example see this comment:
<servo#11273 (comment)>.

There are interesting optimizations to shaping coming, so we should
finally retire this workaround for a real shaper.

Testing: This should have a very tiny change in behavior, which is
unlikely
to be reflected in test results. There isn't a good way to test removal
of this obsolete fast path.

Signed-off-by: Martin Robinson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-layout/text A-platform/fonts I-perf-slow Unnecessary performance degredation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants