Use Vec instead of SmallVec for glyphs field of ShapedRun (#30664)

This glyphs field is usually larger than 8 elements, and SmallVec is not
efficient when it cannot store the value inline.

This change also adds precise glyphs run preallocation in some places
`ShapedRun` is constructed.

Release Notes:

- N/A
This commit is contained in:
Michael Sloan 2025-05-14 09:02:38 +02:00 committed by GitHub
parent a4766e296f
commit 25cc05b45c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 6 additions and 7 deletions

View file

@ -595,7 +595,7 @@ impl PlatformTextSystem for NoopTextSystem {
.unwrap() .unwrap()
.width .width
/ metrics.units_per_em as f32; / metrics.units_per_em as f32;
let mut glyphs = SmallVec::default(); let mut glyphs = Vec::new();
for (ix, c) in text.char_indices() { for (ix, c) in text.char_indices() {
if let Some(glyph) = self.glyph_for_char(FontId(0), c) { if let Some(glyph) = self.glyph_for_char(FontId(0), c) {
glyphs.push(ShapedGlyph { glyphs.push(ShapedGlyph {

View file

@ -16,7 +16,7 @@ use pathfinder_geometry::{
rect::{RectF, RectI}, rect::{RectF, RectI},
vector::{Vector2F, Vector2I}, vector::{Vector2F, Vector2I},
}; };
use smallvec::{SmallVec, smallvec}; use smallvec::SmallVec;
use std::{borrow::Cow, sync::Arc}; use std::{borrow::Cow, sync::Arc};
pub(crate) struct CosmicTextSystem(RwLock<CosmicTextSystemState>); pub(crate) struct CosmicTextSystem(RwLock<CosmicTextSystemState>);
@ -443,7 +443,7 @@ impl CosmicTextSystemState {
} else { } else {
runs.push(ShapedRun { runs.push(ShapedRun {
font_id, font_id,
glyphs: smallvec![shaped_glyph], glyphs: vec![shaped_glyph],
}); });
} }
} }

View file

@ -480,7 +480,7 @@ impl MacTextSystemState {
}; };
let font_id = self.id_for_native_font(font); let font_id = self.id_for_native_font(font);
let mut glyphs = SmallVec::new(); let mut glyphs = Vec::with_capacity(run.glyph_count().try_into().unwrap_or(0));
for ((glyph_id, position), glyph_utf16_ix) in run for ((glyph_id, position), glyph_utf16_ix) in run
.glyphs() .glyphs()
.iter() .iter()

View file

@ -5,7 +5,6 @@ use anyhow::{Result, anyhow};
use collections::HashMap; use collections::HashMap;
use itertools::Itertools; use itertools::Itertools;
use parking_lot::{RwLock, RwLockUpgradableReadGuard}; use parking_lot::{RwLock, RwLockUpgradableReadGuard};
use smallvec::SmallVec;
use windows::{ use windows::{
Win32::{ Win32::{
Foundation::*, Foundation::*,
@ -1089,7 +1088,7 @@ impl IDWriteTextRenderer_Impl for TextRenderer_Impl {
} else { } else {
context.text_system.select_font(&font_struct) context.text_system.select_font(&font_struct)
}; };
let mut glyphs = SmallVec::new(); let mut glyphs = Vec::with_capacity(glyph_count);
for index in 0..glyph_count { for index in 0..glyph_count {
let id = GlyphId(*glyphrun.glyphIndices.add(index) as u32); let id = GlyphId(*glyphrun.glyphIndices.add(index) as u32);
context context

View file

@ -34,7 +34,7 @@ pub struct ShapedRun {
/// The font id for this run /// The font id for this run
pub font_id: FontId, pub font_id: FontId,
/// The glyphs that make up this run /// The glyphs that make up this run
pub glyphs: SmallVec<[ShapedGlyph; 8]>, pub glyphs: Vec<ShapedGlyph>,
} }
/// A single glyph, ready to paint. /// A single glyph, ready to paint.