From fa1c5a84ece937b4fc134e78067018ec6b4f246a Mon Sep 17 00:00:00 2001 From: philippeitis <33013301+philippeitis@users.noreply.github.com> Date: Mon, 10 Oct 2022 17:08:58 -0700 Subject: [PATCH 1/4] Use BTreeSet instead of BTreeMap --- src/generator/templates/api/api.rs.mako | 2 +- src/generator/templates/api/lib/mbuild.mako | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/generator/templates/api/api.rs.mako b/src/generator/templates/api/api.rs.mako index cf15ee2aff..9f58e0cdee 100644 --- a/src/generator/templates/api/api.rs.mako +++ b/src/generator/templates/api/api.rs.mako @@ -18,7 +18,7 @@ use std::collections::HashMap; use std::cell::RefCell; use std::default::Default; -use std::collections::BTreeMap; +use std::collections::BTreeSet; use std::error::Error as StdError; use serde_json as json; use std::io; diff --git a/src/generator/templates/api/lib/mbuild.mako b/src/generator/templates/api/lib/mbuild.mako index e6db099470..90fde7b508 100644 --- a/src/generator/templates/api/lib/mbuild.mako +++ b/src/generator/templates/api/lib/mbuild.mako @@ -126,7 +126,7 @@ pub struct ${ThisType} ${api.properties.params}: HashMap, % if method_default_scope(m): ## We need the scopes sorted, to not unnecessarily query new tokens - ${api.properties.scopes}: BTreeMap + ${api.properties.scopes}: BTreeSet % endif } @@ -188,9 +188,8 @@ ${self._setter_fn(resource, method, m, p, part_prop, ThisType, c)}\ pub fn ${ADD_SCOPE_FN}(mut self, scope: T) -> ${ThisType} where T: Into>, St: AsRef { - match scope.into() { - Some(scope) => self.${api.properties.scopes}.insert(scope.as_ref().to_string(), ()), - None => None, + if let Some(scope) = scope.into() { + self.${api.properties.scopes}.insert(scope.as_ref().to_string()); }; self } @@ -637,8 +636,8 @@ else { } % endif % else: - if self.${api.properties.scopes}.len() == 0 { - self.${api.properties.scopes}.insert(${scope_url_to_variant(name, default_scope, fully_qualified=True)}.as_ref().to_string(), ()); + if self.${api.properties.scopes}.is_empty() { + self.${api.properties.scopes}.insert(${scope_url_to_variant(name, default_scope, fully_qualified=True)}.as_ref().to_string()); } % endif @@ -707,7 +706,7 @@ else { loop { % if default_scope: - let token = match ${auth_call}.get_token(&self.${api.properties.scopes}.keys().map(String::as_str).collect::>()[..]).await { + let token = match ${auth_call}.get_token(&self.${api.properties.scopes}.iter().map(String::as_str).collect::>()[..]).await { // TODO: remove Ok / Err branches Ok(Some(token)) => token.clone(), Ok(None) => { From d611a319de18a3f9edbe69bd4e7f599b81337f17 Mon Sep 17 00:00:00 2001 From: philippeitis <33013301+philippeitis@users.noreply.github.com> Date: Mon, 10 Oct 2022 17:13:38 -0700 Subject: [PATCH 2/4] Fix bug in removing used parameters The current implementation removes parameters if they are used - however, it only removes the first instance, and removes instances by index. However, when multiple items are being removed, following indices must be decremented by 1 to account for previously removed items. --- src/generator/templates/api/lib/mbuild.mako | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/generator/templates/api/lib/mbuild.mako b/src/generator/templates/api/lib/mbuild.mako index 90fde7b508..42bc244b12 100644 --- a/src/generator/templates/api/lib/mbuild.mako +++ b/src/generator/templates/api/lib/mbuild.mako @@ -670,15 +670,8 @@ else { } ## Remove all used parameters { - let mut indices_for_removal: Vec = Vec::with_capacity(${len(replacements)}); - for param_name in [${', '.join(reversed(['"%s"' % r[1] for r in replacements]))}].iter() { - if let Some(index) = params.iter().position(|t| &t.0 == param_name) { - indices_for_removal.push(index); - } - } - for &index in indices_for_removal.iter() { - params.remove(index); - } + let to_remove = [${', '.join(reversed(['"%s"' % r[1] for r in replacements]))}]; + params.retain(|(n, _)| !to_remove.contains(n)); } % endif From 98a02a73a44dc25cd2c34bbe92f5cbbb88a8f66f Mon Sep 17 00:00:00 2001 From: philippeitis <33013301+philippeitis@users.noreply.github.com> Date: Mon, 10 Oct 2022 17:14:43 -0700 Subject: [PATCH 3/4] Simplify searching for "alt=json" param --- src/generator/templates/api/lib/mbuild.mako | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/generator/templates/api/lib/mbuild.mako b/src/generator/templates/api/lib/mbuild.mako index 42bc244b12..18654c9c22 100644 --- a/src/generator/templates/api/lib/mbuild.mako +++ b/src/generator/templates/api/lib/mbuild.mako @@ -582,17 +582,15 @@ match result { % if supports_download: let (json_field_missing, enable_resource_parsing) = { let mut enable = true; - let mut field_present = true; + let mut field_missing = true; for &(name, ref value) in params.iter() { if name == "alt" { - field_present = false; - if >::as_ref(&value) != "json" { - enable = false; - } + field_missing = false; + enable = value == "json"; break; } } - (field_present, enable) + (field_missing, enable) }; if json_field_missing { params.push(("alt", "json".to_string())); From 24f361749a0b67043e66f973661923f420066756 Mon Sep 17 00:00:00 2001 From: philippeitis <33013301+philippeitis@users.noreply.github.com> Date: Mon, 10 Oct 2022 17:37:16 -0700 Subject: [PATCH 4/4] Use .to_string() directly for reduction in build size Went from 3,325,128b to 3,287,744b for accessapproval1 by using .to_string() directly, under --release config. --- src/generator/lib/util.py | 13 +++++++------ src/generator/templates/api/lib/mbuild.mako | 6 +++--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/generator/lib/util.py b/src/generator/lib/util.py index a9f789c3e7..f3316fbbf9 100644 --- a/src/generator/lib/util.py +++ b/src/generator/lib/util.py @@ -1242,13 +1242,14 @@ def size_to_bytes(size): def string_impl(p): + """Returns a function which will convert instances of p to a string""" return { - "google-duration": "::client::serde::duration::to_string", - "byte": "::client::serde::urlsafe_base64::to_string", - "google-datetime": "::client::serde::datetime_to_string", - "date-time": "::client::serde::datetime_to_string", - "google-fieldmask": "(|x: &client::FieldMask| x.to_string())" - }.get(p.get("format"), "(|x: &dyn std::fmt::Display| x.to_string())") + "google-duration": lambda x: f"::client::serde::duration::to_string(&{x})", + "byte": lambda x: f"::client::serde::urlsafe_base64::to_string(&{x})", + "google-datetime": lambda x: f"::client::serde::datetime_to_string(&{x})", + "date-time": lambda x: f"::client::serde::datetime_to_string(&{x})", + "google-fieldmask": lambda x: f"{x}.to_string()" + }.get(p.get("format"), lambda x: f"{x}.to_string()") if __name__ == '__main__': diff --git a/src/generator/templates/api/lib/mbuild.mako b/src/generator/templates/api/lib/mbuild.mako index 18654c9c22..74859442dd 100644 --- a/src/generator/templates/api/lib/mbuild.mako +++ b/src/generator/templates/api/lib/mbuild.mako @@ -556,15 +556,15 @@ match result { % if p.get('repeated', False): if ${pname}.len() > 0 { for f in ${pname}.iter() { - params.push(("${p.name}", ${to_string_impl}(f))); + params.push(("${p.name}", ${to_string_impl("f")})); } } % elif not is_required_property(p): if let Some(value) = ${pname}.as_ref() { - params.push(("${p.name}", ${to_string_impl}(value))); + params.push(("${p.name}", ${to_string_impl("value")})); } % else: - params.push(("${p.name}", ${to_string_impl}(&${pname}))); + params.push(("${p.name}", ${to_string_impl(pname)})); % endif % endfor ## Additional params - may not overlap with optional params