alacritty/copypasta

Reading the clipboard is surprisingly slow

bluenote10 opened this issue · 6 comments

I'm using copypasta within an egui app, and due to the nature of the immediate mode rendering I noticed that the UI shortly freezes when I read from the clipboard. A short measurement

use std::time::Instant;

use copypasta::{ClipboardContext, ClipboardProvider};

fn main() {
    let mut ctx = ClipboardContext::new().unwrap();

    let content = {
        let now = Instant::now();
        let content = ctx.get_contents().unwrap();
        let elapsed = now.elapsed().as_millis();
        println!("Time to get context: {} ms", elapsed);
        content
    };

    println!("Content: {}", content);
}

reveals that it always seems to take 50 ms. Within an egui app it would be nice to stay well below 16.6 ms to keep constant 60 fps in the UI.

Cross checking with other programming language and/or solutions indicates that reading the clipboard isn't that slow per se. For instance, here is a short benchmark in Python:

image

OS: Ubuntu 22.04, X11

that's a known issue of upstream x11 crate(it's basically throttled by 50ms), in general winit will have clipboard support which will solve this issue and this crate will became obsolete.

Either way I'm not sure I can recommend treating a clipboard read as something immediate, since it depends on the application at the other side of the clipboard channel. It should basically always be treated as a blocking operation from what I can tell.

Yeah, the operation is async and with upcoming winit API it'll be async as well (Not the rust async thing, but concept).

When eui says immediate, they mean it in the graphics programmer sense of the word I think, which I'm not a real graphics programmer, so I can't be sure, but I would say anything "immediate" is sub-human-perceptible, so maybe in the sub-ms, just to be safe?

(Not the rust async thing, but concept).

Nothing is async (or is everything?!)

The delivery of data to you is depending on the application which provides the data in the first place. Yes, the X11 backend has artificial latency of 50ms, due to their reasons, and yes they won't change anything about it.

The operation is async, simply because from whatever you're taking the clipboard can even die in-between. So that's why the clipboard reading is async.

The thing is that the crate exposes sync API in a sense that you have to make it async on your own somehow.

The thing is that the crate exposes sync API in a sense that you have to make it async on your own somehow.

Sorry, I know.

I was just making a silly point about how the universe works I guess...