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