perf: eliminate unnecessary clones in write_to_clipboard

write_to_clipboard took ownership of WriteRequest but immediately
cloned both fields into locals, then cloned bytes again for the text
path -- three heap allocations on a potentially large byte buffer.

Destructure the owned request directly and move the fields. The text
path still needs one clone (bytes goes into two MimeSources), but the
extra upfront clones are gone.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Eduard Tolosa 2026-05-25 03:43:33 -05:00
parent 64d5bbfdc3
commit eb64476a2a

View file

@ -12,8 +12,10 @@ pub struct WriteRequest {
}
pub fn write_to_clipboard(req: WriteRequest) -> Result<()> {
let mime = req.mime_type.clone();
let bytes = req.content.clone();
let WriteRequest {
mime_type: mime,
content: bytes,
} = req;
// For text, offer both the specific type and text/plain for compatibility.
let sources: Vec<MimeSource> = if mime.starts_with("text/") {
@ -23,7 +25,7 @@ pub fn write_to_clipboard(req: WriteRequest) -> Result<()> {
mime_type: MimeType::Specific(mime.clone()),
},
MimeSource {
source: Source::Bytes(bytes.clone().into()),
source: Source::Bytes(bytes.into()),
mime_type: MimeType::Specific("text/plain;charset=utf-8".into()),
},
]