Module: Mesa
Branch: main
Commit: 106acbbed9c814bc504421527e6eae884f8e242e
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=106acbbed9c814bc504421527e6eae884f8e242e

Author: LingMan <18294-ling...@users.noreply.gitlab.freedesktop.org>
Date:   Sun Nov  5 02:22:16 2023 +0100

rusticl: Avoid repeatedly creating Vecs during Platform initialization

Creates a Vec once instead of four times.

Reviewed-by: Karol Herbst <kher...@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26050>

---

 src/gallium/frontends/rusticl/core/device.rs      |  4 +--
 src/gallium/frontends/rusticl/core/platform.rs    |  2 +-
 src/gallium/frontends/rusticl/mesa/pipe/device.rs | 33 ++++++++++-------------
 3 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/src/gallium/frontends/rusticl/core/device.rs 
b/src/gallium/frontends/rusticl/core/device.rs
index abfec04d82a..8fcd02d56e5 100644
--- a/src/gallium/frontends/rusticl/core/device.rs
+++ b/src/gallium/frontends/rusticl/core/device.rs
@@ -660,8 +660,8 @@ impl Device {
             .shader_param(pipe_shader_type::PIPE_SHADER_COMPUTE, cap)
     }
 
-    pub fn all() -> Vec<Arc<Device>> {
-        load_screens().into_iter().filter_map(Device::new).collect()
+    pub fn all() -> impl Iterator<Item = Arc<Device>> {
+        load_screens().filter_map(Device::new)
     }
 
     pub fn address_bits(&self) -> cl_uint {
diff --git a/src/gallium/frontends/rusticl/core/platform.rs 
b/src/gallium/frontends/rusticl/core/platform.rs
index 24f60f2af9d..9b6df3dfc8f 100644
--- a/src/gallium/frontends/rusticl/core/platform.rs
+++ b/src/gallium/frontends/rusticl/core/platform.rs
@@ -122,7 +122,7 @@ impl Platform {
             glsl_type_singleton_init_or_ref();
         }
 
-        self.devs.extend(Device::all());
+        self.devs = Device::all().collect();
     }
 
     pub fn init_once() {
diff --git a/src/gallium/frontends/rusticl/mesa/pipe/device.rs 
b/src/gallium/frontends/rusticl/mesa/pipe/device.rs
index 301042c4e3e..cb34580fc72 100644
--- a/src/gallium/frontends/rusticl/mesa/pipe/device.rs
+++ b/src/gallium/frontends/rusticl/mesa/pipe/device.rs
@@ -37,17 +37,14 @@ impl Drop for PipeLoaderDevice {
     }
 }
 
-fn load_devs() -> Vec<PipeLoaderDevice> {
+fn load_devs() -> impl Iterator<Item = PipeLoaderDevice> {
     let n = unsafe { pipe_loader_probe(ptr::null_mut(), 0, true) };
     let mut devices: Vec<*mut pipe_loader_device> = vec![ptr::null_mut(); n as 
usize];
     unsafe {
         pipe_loader_probe(devices.as_mut_ptr(), n, true);
     }
 
-    devices
-        .into_iter()
-        .filter_map(PipeLoaderDevice::new)
-        .collect()
+    devices.into_iter().filter_map(PipeLoaderDevice::new)
 }
 
 fn get_enabled_devs() -> HashMap<String, u32> {
@@ -90,23 +87,21 @@ fn get_enabled_devs() -> HashMap<String, u32> {
     res
 }
 
-pub fn load_screens() -> Vec<PipeScreen> {
+pub fn load_screens() -> impl Iterator<Item = PipeScreen> {
     let devs = load_devs();
     let mut enabled_devs = get_enabled_devs();
 
-    devs.into_iter()
-        .filter(|dev| {
-            let driver_name = unsafe { 
c_string_to_string(dev.ldev.as_ref().unwrap().driver_name) };
+    devs.filter(move |dev| {
+        let driver_name = unsafe { 
c_string_to_string(dev.ldev.as_ref().unwrap().driver_name) };
 
-            if let Some(enabled_devs) = enabled_devs.get_mut(&driver_name) {
-                let res = (*enabled_devs & 1) == 1;
-                *enabled_devs >>= 1;
+        if let Some(enabled_devs) = enabled_devs.get_mut(&driver_name) {
+            let res = (*enabled_devs & 1) == 1;
+            *enabled_devs >>= 1;
 
-                res
-            } else {
-                false
-            }
-        })
-        .filter_map(PipeLoaderDevice::load_screen)
-        .collect()
+            res
+        } else {
+            false
+        }
+    })
+    .filter_map(PipeLoaderDevice::load_screen)
 }

Reply via email to