Reduce allocations, prevent a String object creation unless required.

This commit is contained in:
2019-11-13 15:34:56 +01:00
parent 85d322877e
commit e154e549d3
5 changed files with 13 additions and 19 deletions

View File

@@ -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

View File

@@ -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 {

View File

@@ -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

View File

@@ -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 {

View File

@@ -81,7 +81,7 @@ impl From<Axis> 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(),
}
}
}