s/ `String` / `Rc<str>` / in `NamespaceQualifier` and `ir::Identifier`.
This CL was motivated by trying to improve the efficiency of
`NamespaceQualifier`'s `Clone` implementation - cloning of `Rc<str>`
requires just increasing the ref-count and is therefore cheaper than
cloning of `String` which requires a heap allocation and copying the
string contents.
This CL also changes the type of `ir::Identifier` because this is
the representation of `ir::Namespace::name`.
`Rc<str>` instead of `String` is possible because these strings are
never mutated.
PiperOrigin-RevId: 490605310
diff --git a/common/code_gen_utils.rs b/common/code_gen_utils.rs
index 78b151a..7087243 100644
--- a/common/code_gen_utils.rs
+++ b/common/code_gen_utils.rs
@@ -59,7 +59,7 @@
// catch some error conditions early (e.g. an empty path component may trigger a
// panic in `make_rs_ident`; a reserved C++ keyword might trigger a late error
// in `format_for_cc` / `format_cc_ident`).
-pub struct NamespaceQualifier(pub Vec<String>);
+pub struct NamespaceQualifier(pub Vec<Rc<str>>);
impl NamespaceQualifier {
pub fn format_for_rs(&self) -> TokenStream {
@@ -435,7 +435,7 @@
}
fn create_namespace_qualifier_for_tests(input: &[&str]) -> NamespaceQualifier {
- NamespaceQualifier(input.into_iter().map(|s| s.to_string()).collect())
+ NamespaceQualifier(input.into_iter().map(|&s| s.into()).collect())
}
#[test]