Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer::set_text seems very slow? #245

Open
ChangeCaps opened this issue Apr 20, 2024 · 1 comment
Open

Buffer::set_text seems very slow? #245

ChangeCaps opened this issue Apr 20, 2024 · 1 comment

Comments

@ChangeCaps
Copy link

ChangeCaps commented Apr 20, 2024

On both my laptop and my desktop, calling Buffer::set_text takes anywhere from 100µs to 1.3ms, averaging around 600µs for a string of around 20-22 characters using Shaping::Advanced. Using Shaping::Basic takes around 450µs. This to me seems very slow, calling Buffer::set_text a couple of times per frame takes up a serious part of the frame budget.

OS: nixos/arch
Rustc: 1.77.2
Cosmic-text: 0.11.2
Font: Roboto Regular

Edit:

This only seems to be a problem with the mono space version of the font.

@ChangeCaps
Copy link
Author

use std::time::Instant;

use cosmic_text::{
    Attrs, Buffer, CacheKeyFlags, Family, FontSystem, Metrics, Shaping, Stretch, Style, Weight,
};

fn set_text(font_system: &mut FontSystem, buffer: &mut Buffer, family: Family, i: u64) {
    buffer.set_text(
        font_system,
        &format!("rgb(69  , 103  , {i})"),
        Attrs {
            color_opt: None,
            family,
            stretch: Stretch::Normal,
            style: Style::Normal,
            weight: Weight::NORMAL,
            metadata: 0,
            cache_key_flags: CacheKeyFlags::empty(),
        },
        Shaping::Advanced,
    );
}

fn main() {
    let mut font_system = FontSystem::new();

    let mut buffer = Buffer::new(&mut font_system, Metrics::new(12.0, 16.0));

    buffer.set_size(&mut font_system, 100.0, 100.0);

    let serif_start = Instant::now();

    for i in 0..1000 {
        set_text(&mut font_system, &mut buffer, Family::Serif, i);
    }

    let serif_duration = serif_start.elapsed() / 1000;
    println!("Time: {:?}", serif_duration);

    let monospace_start = Instant::now();

    for i in 0..1000 {
        set_text(&mut font_system, &mut buffer, Family::Monospace, i);
    }

    let monospace_duration = monospace_start.elapsed() / 1000;
    println!("Time: {:?}", monospace_duration);

    println!(
        "Monospace was {} times slower than Serif",
        monospace_duration.as_secs_f64() / serif_duration.as_secs_f64()
    );
}

This crude examples gives the result that Monospace is around 28 times slower than Serif on my machine.

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

No branches or pull requests

1 participant