From bba41629f4754da51602899ac17e38cd86fe9796 Mon Sep 17 00:00:00 2001 From: Lionel Sambuc Date: Fri, 18 Oct 2019 20:54:47 +0200 Subject: [PATCH] Removing memory allocations --- src/rest_api/actions.rs | 4 ++-- src/rest_api/core.rs | 2 +- src/rest_api/cores.rs | 4 ++-- src/rest_api/mod.rs | 12 +++++------- src/rest_api/space.rs | 2 +- src/rest_api/spaces.rs | 4 ++-- src/rest_api/spatial_object.rs | 4 ++-- src/rest_api/spatial_objects.rs | 6 +++--- src/shared_state.rs | 6 +++--- 9 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/rest_api/actions.rs b/src/rest_api/actions.rs index f047f4e..2c1f666 100644 --- a/src/rest_api/actions.rs +++ b/src/rest_api/actions.rs @@ -24,8 +24,8 @@ impl Query { &self.query } - pub fn resolution(&self) -> Option> { - self.resolution.clone() + pub fn resolution(&self) -> &Option> { + &self.resolution } pub fn volume(&self) -> Option { diff --git a/src/rest_api/core.rs b/src/rest_api/core.rs index d23d6b5..04f2daa 100644 --- a/src/rest_api/core.rs +++ b/src/rest_api/core.rs @@ -22,7 +22,7 @@ fn get((core, state): (Path, Data>)) -> HandlerResul let core = core.to_string(); let context = state.read().unwrap(); - match context.db().core(core) { + match context.db().core(&core) { Ok(core) => ok_200(&Core::from(core)), Err(_) => error_404(), } diff --git a/src/rest_api/cores.rs b/src/rest_api/cores.rs index 9c9f711..5657378 100644 --- a/src/rest_api/cores.rs +++ b/src/rest_api/cores.rs @@ -46,7 +46,7 @@ fn post((parameters, state): (Json, Data>)) -> Hand match context.filter( filter, core, - space.clone(), + &space, parameters.volume(), ¶meters.view_port, parameters.resolution(), @@ -69,7 +69,7 @@ fn post((parameters, state): (Json, Data>)) -> Hand ok_200( &results .drain() - .map(|x| Core::from(db.core(x).unwrap())) + .map(|x| Core::from(db.core(&x).unwrap())) .collect::>(), ) } diff --git a/src/rest_api/mod.rs b/src/rest_api/mod.rs index 776f8f8..639695e 100644 --- a/src/rest_api/mod.rs +++ b/src/rest_api/mod.rs @@ -64,7 +64,7 @@ impl Filters { } } - pub fn space(&self, db: &mercator_db::DataBase) -> Result, HandlerResult> { + pub fn space(&self, db: &mercator_db::DataBase) -> Result<&Option, HandlerResult> { if let Some(space_id) = &self.space { if !db.space_keys().contains(&space_id.to_string()) { return Err(error_422(format!( @@ -73,19 +73,17 @@ impl Filters { ))); } } - Ok(self.space.clone()) + Ok(&self.space) } - pub fn resolution(&self) -> Option> { - self.resolution.clone() + pub fn resolution(&self) -> &Option> { + &self.resolution } pub fn volume(&self) -> Option { match &self.view_port { None => None, - Some((low, high)) => { - Some(Shape::BoundingBox(low.clone().into(), high.clone().into()).volume()) - } + Some((low, high)) => Some(Shape::BoundingBox(low.into(), high.into()).volume()), } } } diff --git a/src/rest_api/space.rs b/src/rest_api/space.rs index 65a8e63..70382e3 100644 --- a/src/rest_api/space.rs +++ b/src/rest_api/space.rs @@ -22,7 +22,7 @@ fn get((path, state): (Path, Data>)) -> HandlerResul let name = path.to_string(); let context = state.read().unwrap(); - match context.db().space(name) { + match context.db().space(&name) { Ok(space) => { let space: model::Space = space.into(); ok_200(&space) diff --git a/src/rest_api/spaces.rs b/src/rest_api/spaces.rs index db370c3..738bfc6 100644 --- a/src/rest_api/spaces.rs +++ b/src/rest_api/spaces.rs @@ -47,7 +47,7 @@ fn post((parameters, state): (Json, Data>)) -> Hand match context.filter( filter, core, - space.clone(), + &space, parameters.volume(), ¶meters.view_port, parameters.resolution(), @@ -70,7 +70,7 @@ fn post((parameters, state): (Json, Data>)) -> Hand ok_200( &results .drain() - .map(|id| match db.space(id) { + .map(|id| match db.space(&id) { Err(_) => None, Ok(x) => Some(model::Space::from(x)), }) diff --git a/src/rest_api/spatial_object.rs b/src/rest_api/spatial_object.rs index d3ff799..fc9c0f5 100644 --- a/src/rest_api/spatial_object.rs +++ b/src/rest_api/spatial_object.rs @@ -33,10 +33,10 @@ fn get((path, state): (Path<(String, String)>, Data>)) -> Ha // Enforce highest resolution index. threshold_volume: None, view_port: &None, - resolution: Some(vec![0]), + resolution: &Some(vec![0]), }; - match db.core(core) { + match db.core(&core) { Ok(core) => match core.get_by_id(¶meters, &id) { Ok(objects) => { let results = to_spatial_objects(db, objects); diff --git a/src/rest_api/spatial_objects.rs b/src/rest_api/spatial_objects.rs index 86f79e8..771ddff 100644 --- a/src/rest_api/spatial_objects.rs +++ b/src/rest_api/spatial_objects.rs @@ -25,7 +25,7 @@ fn post( let context = state.read().unwrap(); let db = context.db(); - match db.core(core_id.clone()) { + match db.core(&core_id) { Err(_) => error_404(), Ok(core) => match parameters.space(db) { Err(e) => e, @@ -33,7 +33,7 @@ fn post( None => { let mut results = HashSet::new(); for property in core.keys().iter() { - results.insert(property.id().clone()); + results.insert(property.id()); } if parameters.ids_only() { @@ -73,7 +73,7 @@ fn post( if parameters.ids_only() { let mut uniques = HashSet::new(); for o in objects.iter() { - uniques.insert(o.value.id().clone()); + uniques.insert(o.value.id()); } ok_200(&uniques.drain().collect::>()) diff --git a/src/shared_state.rs b/src/shared_state.rs index 7945fac..1f96ae2 100644 --- a/src/shared_state.rs +++ b/src/shared_state.rs @@ -36,10 +36,10 @@ impl SharedState { &self, filter: &str, core: &str, - output_space: Option, + output_space: &Option, volume: Option, view_port: &Option<(Vec, Vec)>, - resolution: Option>, + resolution: &Option>, ) -> mercator_db::ResultSet { let parser = self.filter_parser(); let parse; @@ -98,7 +98,7 @@ impl SharedState { core: &str, volume: Option, view_port: &Option<(Vec, Vec)>, - resolution: Option>, + resolution: &Option>, ) -> mercator_db::ResultSet { let parser = self.query_parser(); let parse;