-
Notifications
You must be signed in to change notification settings - Fork 61
Perf optimizations around clone() usage
#1481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -367,10 +367,10 @@ pub fn enable_tracing(trace_level_arg: Option<&TraceLevel>, trace_format_arg: Op | |
| // command-line args override setting value, but not policy | ||
| if !policy_is_used { | ||
| if let Some(v) = trace_level_arg { | ||
| tracing_setting.level = v.clone(); | ||
| tracing_setting.level = *v; | ||
| } | ||
| if let Some(v) = trace_format_arg { | ||
| tracing_setting.format = v.clone(); | ||
| tracing_setting.format = *v; | ||
|
Comment on lines
369
to
+373
|
||
| } | ||
| } | ||
|
|
||
|
|
@@ -443,7 +443,7 @@ pub fn get_input(input: Option<&String>, file: Option<&String>) -> String { | |
| error!("{}", t!("util.inputIsFile")); | ||
| exit(EXIT_INVALID_INPUT); | ||
| } | ||
| input.clone() | ||
| input.to_owned() | ||
| } else if let Some(path) = file { | ||
| debug!("{} {path}", t!("util.readingInputFromFile")); | ||
| // check if need to read from STDIN | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -680,26 +680,26 @@ pub trait SchemaUtilityExtensions { | |||||
|
|
||||||
| //************************ $id keyword functions *************************// | ||||||
| /// Retrieves the `$id` values for any entries in the `$defs` keyword. | ||||||
| /// | ||||||
| /// | ||||||
| /// A compound schema resource document, also called a "bundled schema", includes referenced | ||||||
| /// schema resources in the `$defs` keyword. A schema resource always defines the `$id` keyword. | ||||||
| /// | ||||||
| /// | ||||||
| /// This method retrieves those IDs and returns a hashset containing the unique values. | ||||||
| /// | ||||||
| /// | ||||||
| /// Optionally, you can recursively search for schema resource IDs to handle cases where the | ||||||
| /// a bundled schema resource may itself have bundled resources. | ||||||
| /// | ||||||
| /// | ||||||
| /// If the schema doesn't have any bundled schema resources, this method returns an empty | ||||||
| /// hashset. | ||||||
| /// | ||||||
| /// | ||||||
| /// # Examples | ||||||
| /// | ||||||
| /// | ||||||
| /// This example demonstrates the difference between invoking the method for the top-level | ||||||
| /// only and recursively returning nested bundled schema resources. | ||||||
| /// | ||||||
| /// | ||||||
| /// ```rust | ||||||
| /// use std::collections::HashSet; | ||||||
| /// | ||||||
| /// | ||||||
| /// use schemars::json_schema; | ||||||
| /// use serde_json::json; | ||||||
| /// use dsc_lib_jsonschema::schema_utility_extensions::SchemaUtilityExtensions; | ||||||
|
|
@@ -725,7 +725,7 @@ pub trait SchemaUtilityExtensions { | |||||
| /// } | ||||||
| /// } | ||||||
| /// }); | ||||||
| /// | ||||||
| /// | ||||||
| /// let non_recursive_result: HashSet<&str> = [ | ||||||
| /// "https://contoso.com/schemas/example/string.json", | ||||||
| /// "https://contoso.com/schemas/example/object.json" | ||||||
|
|
@@ -734,7 +734,7 @@ pub trait SchemaUtilityExtensions { | |||||
| /// schema.get_bundled_schema_resource_ids(false), | ||||||
| /// non_recursive_result | ||||||
| /// ); | ||||||
| /// | ||||||
| /// | ||||||
| /// let recursive_result: HashSet<&str> = [ | ||||||
| /// "https://contoso.com/schemas/example/string.json", | ||||||
| /// "https://contoso.com/schemas/example/object.json", | ||||||
|
|
@@ -1474,20 +1474,20 @@ pub trait SchemaUtilityExtensions { | |||||
| //************************ $ref keyword functions ************************// | ||||||
| /// Retrieves the value for every `$ref` keyword from the [`Schema`] as a [`HashSet`] of | ||||||
| /// unique values. | ||||||
| /// | ||||||
| /// | ||||||
| /// | ||||||
| /// | ||||||
| /// This method recurses through a given schema for the `$ref` keyword and inserts the value | ||||||
| /// into a hashset to return. If the schema doesn't define any references, this method returns | ||||||
| /// an empty hashset. | ||||||
| /// | ||||||
| /// | ||||||
| /// # Examples | ||||||
| /// | ||||||
| /// | ||||||
| /// This example shows how the method returns a unique set of references by recursively | ||||||
| /// searching the schema for the `$ref` keyword. | ||||||
| /// | ||||||
| /// | ||||||
| /// ```rust | ||||||
| /// use std::collections::HashSet; | ||||||
| /// | ||||||
| /// | ||||||
| /// use schemars::json_schema; | ||||||
| /// use dsc_lib_jsonschema::schema_utility_extensions::SchemaUtilityExtensions; | ||||||
| /// | ||||||
|
|
@@ -1499,12 +1499,12 @@ pub trait SchemaUtilityExtensions { | |||||
| /// "age": { "$ref": "/schemas/example/properties/age.json" }, | ||||||
| /// }, | ||||||
| /// }); | ||||||
| /// | ||||||
| /// | ||||||
| /// let references: HashSet<&str> = [ | ||||||
| /// "/schemas/example/properties/name.json", | ||||||
| /// "/schemas/example/properties/age.json" | ||||||
| /// ].into(); | ||||||
| /// | ||||||
| /// | ||||||
| /// assert_eq!( | ||||||
| /// schema.get_references(), | ||||||
| /// references | ||||||
|
|
@@ -1559,7 +1559,7 @@ pub trait SchemaUtilityExtensions { | |||||
| /// "nestedBar": { "$ref": "#/$defs/bar" }, | ||||||
| /// }, | ||||||
| /// }, | ||||||
| /// | ||||||
| /// | ||||||
| /// }, | ||||||
| /// }); | ||||||
| /// | ||||||
|
|
@@ -1576,17 +1576,17 @@ pub trait SchemaUtilityExtensions { | |||||
| fn get_references_to_bundled_schema_resource(&self, resource_id: &str) -> HashSet<&str>; | ||||||
| /// Searches the schema for instances of the `$ref` keyword defined as a | ||||||
| /// given value and replaces each instance with a new value. | ||||||
| /// | ||||||
| /// | ||||||
| /// This method simplifies replacing references programmatically, especially | ||||||
| /// for converting references to use the canonical ID of a bundled schema | ||||||
| /// resource. | ||||||
| /// | ||||||
| /// | ||||||
| /// # Examples | ||||||
| /// | ||||||
| /// | ||||||
| /// This example replaces the reference to `#/$defs/name.json` with the | ||||||
| /// canonical ID URI for the referenced schema resource, which is bundled | ||||||
| /// in the schema. | ||||||
| /// | ||||||
| /// | ||||||
| /// ```rust | ||||||
| /// use schemars::json_schema; | ||||||
| /// use dsc_lib_jsonschema::schema_utility_extensions::SchemaUtilityExtensions; | ||||||
|
|
@@ -1604,34 +1604,34 @@ pub trait SchemaUtilityExtensions { | |||||
| /// } | ||||||
| /// } | ||||||
| /// }); | ||||||
| /// | ||||||
| /// | ||||||
| /// schema.replace_references( | ||||||
| /// "#/$defs/name", | ||||||
| /// "https://contoso.com/schemas/example/properties/name.json" | ||||||
| /// ); | ||||||
| /// | ||||||
| /// | ||||||
| /// assert_eq!( | ||||||
| /// schema.get_references().into_iter().nth(0).unwrap(), | ||||||
| /// "https://contoso.com/schemas/example/properties/name.json" | ||||||
| /// ); | ||||||
| /// ``` | ||||||
| fn replace_references(&mut self, find_value: &str, new_value: &str); | ||||||
| /// Checks whether a given reference maps to a bundled schema resource. | ||||||
| /// | ||||||
| /// | ||||||
| /// This method takes the value of a `$ref` keyword and searches for a matching entry in the | ||||||
| /// `$defs` keyword. The method returns `true` if the reference resolves to an entry in | ||||||
| /// `$defs` and otherwise false. | ||||||
| /// | ||||||
| /// | ||||||
| /// The reference can be any of the following: | ||||||
| /// | ||||||
| /// - A URI identifier fragment, like `#/$defs/foo` | ||||||
| /// - An absolute URL for the referenced schema, like `https://contoso.com/schemas/example.json` | ||||||
| /// - A site-relative URL for the referenced schema, like `/schemas/example.json`. The function | ||||||
| /// can only resolve site-relative URLs when the schema itself defines `$id` with an absolute | ||||||
| /// URL, because it uses the current schema's `$id` as the base URL. | ||||||
| /// | ||||||
| /// | ||||||
| /// # Examples | ||||||
| /// | ||||||
| /// | ||||||
| /// ```rust | ||||||
| /// use schemars::json_schema; | ||||||
| /// use dsc_lib_jsonschema::schema_utility_extensions::SchemaUtilityExtensions; | ||||||
|
|
@@ -1645,7 +1645,7 @@ pub trait SchemaUtilityExtensions { | |||||
| /// } | ||||||
| /// } | ||||||
| /// }); | ||||||
| /// | ||||||
| /// | ||||||
| /// // Resolving reference as pointer | ||||||
| /// assert_eq!(schema.reference_is_for_bundled_resource("#/$defs/name"), true); | ||||||
| /// // Resolving reference as site-relative URI | ||||||
|
|
@@ -1991,7 +1991,7 @@ impl SchemaUtilityExtensions for Schema { | |||||
| if k.as_str() == old_key { | ||||||
| (new_key.to_string(), v.clone()) | ||||||
| } else { | ||||||
| (k.clone(), v.clone()) | ||||||
| (std::mem::take(k), v.clone()) | ||||||
|
||||||
| (std::mem::take(k), v.clone()) | |
| (k.clone(), v.clone()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,13 +30,15 @@ impl RegistryHelper { | |
| /// | ||
| /// * `RegistryError` - The error that occurred. | ||
| pub fn new_from_json(config: &str) -> Result<Self, RegistryError> { | ||
| let registry: Registry = match serde_json::from_str(config) { | ||
| let mut registry: Registry = match serde_json::from_str(config) { | ||
| Ok(config) => config, | ||
| Err(e) => return Err(RegistryError::Json(e)), | ||
| }; | ||
| let key_path = registry.key_path.clone(); | ||
| let key_path = std::mem::take(&mut registry.key_path); | ||
| let (hive, subkey) = get_hive_from_path(&key_path)?; | ||
|
|
||
| // Restore the key_path since we moved it out | ||
| registry.key_path = key_path; | ||
|
Comment on lines
36
to
+41
|
||
| Ok( | ||
| Self { | ||
| config: registry, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This uses the bitwise OR operator (
|) on booleans, which forces evaluation of both sides and defeats short-circuiting (and any perf win from avoiding clones). Use||here, and consider restructuring to avoid repeatedto_lowercase()allocations (e.g., compute the lowercased filter once whendescriptionisSome).