From 6c616a1b69799dec7cf27e64e319422c837a42ec Mon Sep 17 00:00:00 2001 From: dvec Date: Wed, 12 Jan 2022 13:33:06 +0300 Subject: [PATCH] egui_glow: Reduce memory allocations in Painter::set_texture (#1096) Use bytemuck::cast_slice --- egui_glow/Cargo.toml | 3 ++- egui_glow/src/painter.rs | 8 +++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/egui_glow/Cargo.toml b/egui_glow/Cargo.toml index 8945b5f0..af53d9ac 100644 --- a/egui_glow/Cargo.toml +++ b/egui_glow/Cargo.toml @@ -23,8 +23,9 @@ include = [ all-features = true [dependencies] -egui = { version = "0.16.0", path = "../egui", default-features = false, features = ["single_threaded"] } +egui = { version = "0.16.0", path = "../egui", default-features = false, features = ["single_threaded", "convert_bytemuck"] } +bytemuck = "1.7" epi = { version = "0.16.0", path = "../epi", optional = true } glow = "0.11" memoffset = "0.6" diff --git a/egui_glow/src/painter.rs b/egui_glow/src/painter.rs index 0ba75f2e..5e087e63 100644 --- a/egui_glow/src/painter.rs +++ b/egui_glow/src/painter.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; +use bytemuck::cast_slice; use egui::{ emath::Rect, epaint::{Color32, Mesh, Vertex}, @@ -434,17 +435,14 @@ impl Painter { "Mismatch between texture size and texel count" ); - let mut data = Vec::with_capacity(image.pixels.len() * 4); - for srgba in &image.pixels { - data.extend_from_slice(&srgba.to_array()); - } + let data: &[u8] = cast_slice(image.pixels.as_ref()); let gl_texture = srgb_texture2d( gl, self.is_webgl_1, self.srgb_support, self.texture_filter, - &data, + data, image.size[0], image.size[1], );