From e154e549d31733d599ccf2a33a3caf7cf393ab30 Mon Sep 17 00:00:00 2001 From: Lionel Sambuc Date: Wed, 13 Nov 2019 15:34:56 +0100 Subject: [PATCH] Reduce allocations, prevent a String object creation unless required. --- src/database/db_core.rs | 6 +++--- src/database/space/axis.rs | 4 ++-- src/database/space_db.rs | 3 +-- src/database/space_index.rs | 15 +++++---------- src/json/model.rs | 4 ++-- 5 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/database/db_core.rs b/src/database/db_core.rs index 5bef116..0079e73 100644 --- a/src/database/db_core.rs +++ b/src/database/db_core.rs @@ -36,7 +36,7 @@ pub enum Properties { } impl Properties { - pub fn id(&self) -> &String { + pub fn id(&self) -> &str { match self { Properties::Feature(id) => id, Properties::Unknown(id, _) => id, @@ -268,7 +268,7 @@ impl Core { // Do we have this ID registered at all? if let Ok(offset) = self .properties - .binary_search_by_key(&&id, |properties| properties.id()) + .binary_search_by_key(&id.as_str(), |properties| properties.id()) { // Yes, so now let's find all the position linked to it, per // reference space @@ -321,7 +321,7 @@ impl Core { if let Ok(offset) = self .properties - .binary_search_by_key(&&id, |properties| properties.id()) + .binary_search_by_key(&id.as_str(), |properties| properties.id()) { // Generate the search volume. Iterate over all reference spaces, to // retrieve a list of SpaceSetObjects linked to `id`, then iterate diff --git a/src/database/space/axis.rs b/src/database/space/axis.rs index 4ffa91c..d31b00c 100644 --- a/src/database/space/axis.rs +++ b/src/database/space/axis.rs @@ -139,8 +139,8 @@ impl Axis { }) } - pub fn measurement_unit(&self) -> String { - self.measurement_unit.to_str().into() + pub fn measurement_unit(&self) -> &str { + self.measurement_unit.to_str() } pub fn unit_vector(&self) -> &Position { diff --git a/src/database/space_db.rs b/src/database/space_db.rs index 1f9bcae..3f7dac8 100644 --- a/src/database/space_db.rs +++ b/src/database/space_db.rs @@ -290,8 +290,7 @@ impl SpaceDB { let view_port = parameters.view_port(space); // Select the objects - let objects = - self.resolutions[index].find_by_value(&SpaceFields::new(self.name().into(), id.into())); + let objects = self.resolutions[index].find_by_value(&SpaceFields::new(self.name(), id)); let results = if let Some(view_port) = view_port { objects diff --git a/src/database/space_index.rs b/src/database/space_index.rs index d9e85b1..2f6bc6b 100644 --- a/src/database/space_index.rs +++ b/src/database/space_index.rs @@ -22,10 +22,6 @@ impl SpaceSetObject { } } - pub fn id(&self) -> &Coordinate { - &self.value - } - pub fn space_id(&self) -> &String { &self.space_id } @@ -54,12 +50,11 @@ pub struct SpaceFields { } impl SpaceFields { - pub fn new(space_id: String, value: Coordinate) -> Self { - SpaceFields { space_id, value } - } - - pub fn space_id(&self) -> &String { - &self.space_id + pub fn new(space_id: &str, value: usize) -> Self { + SpaceFields { + space_id: space_id.into(), + value: value.into(), + } } pub fn value(&self) -> &Coordinate { diff --git a/src/json/model.rs b/src/json/model.rs index 35bb143..658bd9a 100644 --- a/src/json/model.rs +++ b/src/json/model.rs @@ -81,7 +81,7 @@ impl From for space::Axis { impl From<&space::Axis> for Axis { fn from(axis: &space::Axis) -> Self { Axis { - measurement_unit: axis.measurement_unit(), + measurement_unit: axis.measurement_unit().into(), graduation: axis.graduation().into(), unit_vector: axis.unit_vector().into(), } @@ -118,7 +118,7 @@ impl From<&&database::Properties> for Properties { fn from(p: &&database::Properties) -> Self { Properties { type_name: p.type_name().to_string(), - id: p.id().clone(), + id: p.id().into(), } } }