Skip to content

Commit

Permalink
perf(backend): use Small String Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
SARDONYX-sard committed Jan 23, 2024
1 parent 73a85fe commit 7460726
Show file tree
Hide file tree
Showing 27 changed files with 274 additions and 140 deletions.
2 changes: 1 addition & 1 deletion dar2oar_core/src/condition_parser/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ mod tests {
ConditionSet::IsActorBase(IsActorBase {
negated: false,
actor_base: PluginValue {
plugin_name: "Skyrim.esm".to_string(),
plugin_name: "Skyrim.esm".into(),
form_id: "7".into(),
},
..Default::default()
Expand Down
8 changes: 4 additions & 4 deletions dar2oar_core/src/condition_parser/dar_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl From<&FnArg<'_>> for NumericValue {
form_id,
} => Self::GlobalVariable(
PluginValue {
plugin_name: plugin_name.to_string(),
plugin_name: (*plugin_name).into(),
form_id: NumericLiteral::from(form_id).into(),
}
.into(),
Expand Down Expand Up @@ -137,7 +137,7 @@ impl TryFrom<FnArg<'_>> for PluginValue {
plugin_name,
form_id,
} => Ok(Self {
plugin_name: plugin_name.to_string(),
plugin_name: (*plugin_name).into(),
form_id: NumericLiteral::from(form_id).into(),
}),
FnArg::Number(num) => Err(ParseError::UnexpectedValue(
Expand All @@ -157,7 +157,7 @@ impl TryFrom<&FnArg<'_>> for PluginValue {
plugin_name,
form_id,
} => Ok(Self {
plugin_name: plugin_name.to_string(),
plugin_name: (*plugin_name).into(),
form_id: NumericLiteral::from(form_id).into(),
}),
FnArg::Number(num) => Err(ParseError::UnexpectedValue(
Expand All @@ -176,7 +176,7 @@ impl From<&FnArg<'_>> for Keyword {
form_id,
} => Self::Form(FormValue {
form: PluginValue {
plugin_name: plugin_name.to_string(),
plugin_name: (*plugin_name).into(),
form_id: NumericLiteral::from(form_id).into(),
},
}),
Expand Down
5 changes: 3 additions & 2 deletions dar2oar_core/src/conditions/and.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{condition::default_required_version, is_false, ConditionSet};
use compact_str::CompactString;
use serde::{Deserialize, Serialize};

/// - OAR: AND
Expand All @@ -11,10 +12,10 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct And {
/// Condition name "AND"
pub condition: String,
pub condition: CompactString,
#[serde(default = "default_required_version")]
#[serde(rename = "requiredVersion")]
pub required_version: String,
pub required_version: CompactString,
#[serde(default)]
#[serde(skip_serializing_if = "is_false")]
pub negated: bool,
Expand Down
24 changes: 14 additions & 10 deletions dar2oar_core/src/conditions/compare_values.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use super::{condition::default_required_version, is_false};
use crate::values::{Cmp, NumericValue};
use compact_str::CompactString;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CompareValues {
/// Condition name "CompareValues"
pub condition: String,
pub condition: CompactString,
#[serde(default = "default_required_version")]
#[serde(rename = "requiredVersion")]
pub required_version: String,
pub required_version: CompactString,
#[serde(default)]
#[serde(skip_serializing_if = "is_false")]
pub negated: bool,
Expand Down Expand Up @@ -40,12 +41,11 @@ impl Default for CompareValues {

#[cfg(test)]
mod tests {
use super::*;
use crate::values::{
ActorValue, ActorValueType, GraphValue, GraphVariableType, NumericLiteral, NumericValue,
PluginValue, StaticValue,
};

use super::*;
use pretty_assertions::assert_eq;

#[test]
Expand All @@ -55,6 +55,7 @@ mod tests {
value_b: NumericValue::StaticValue(StaticValue { value: 42.0 }),
..Default::default()
};
let serialized = serde_json::to_string_pretty(&compare_values).unwrap();

let expected = r#"{
"condition": "CompareValues",
Expand All @@ -67,7 +68,7 @@ mod tests {
"value": 42.0
}
}"#;
let serialized = serde_json::to_string_pretty(&compare_values).unwrap();

assert_eq!(serialized, expected);
}

Expand All @@ -85,6 +86,7 @@ mod tests {
comparison: Cmp::Ge,
..Default::default()
};
let serialized = serde_json::to_string_pretty(&compare_values).unwrap();

let expected = r#"{
"condition": "CompareValues",
Expand All @@ -99,7 +101,7 @@ mod tests {
"actorValueType": "Max"
}
}"#;
let serialized = serde_json::to_string_pretty(&compare_values).unwrap();

assert_eq!(serialized, expected);
}

Expand All @@ -118,6 +120,7 @@ mod tests {
comparison: Cmp::Ne,
..Default::default()
};
let serialized = serde_json::to_string_pretty(&compare_values).unwrap();

let expected = r#"{
"condition": "CompareValues",
Expand All @@ -132,7 +135,7 @@ mod tests {
"graphVariableType": "Int"
}
}"#;
let serialized = serde_json::to_string_pretty(&compare_values).unwrap();

assert_eq!(serialized, expected);
}

Expand All @@ -141,21 +144,22 @@ mod tests {
let compare_values = CompareValues {
value_a: NumericValue::GlobalVariable(
PluginValue {
plugin_name: "my_plugin.esm".to_string(),
plugin_name: "my_plugin.esm".into(),
form_id: 1usize.into(),
}
.into(),
),
value_b: NumericValue::GlobalVariable(
PluginValue {
plugin_name: "another_plugin.esp".to_string(),
plugin_name: "another_plugin.esp".into(),
form_id: "2".into(),
}
.into(),
),
comparison: Cmp::Gt,
..Default::default()
};
let serialized = serde_json::to_string_pretty(&compare_values).unwrap();

let expected = r#"{
"condition": "CompareValues",
Expand All @@ -174,7 +178,7 @@ mod tests {
}
}
}"#;
let serialized = serde_json::to_string_pretty(&compare_values).unwrap();

assert_eq!(serialized, expected);
}
}
33 changes: 17 additions & 16 deletions dar2oar_core/src/conditions/condition.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use super::is_false;
use compact_str::CompactString;
use serde::{Deserialize, Serialize};

pub const REQUIRED_VERSION: &str = "1.0.0.0";

pub fn default_required_version() -> String {
REQUIRED_VERSION.to_string()
pub fn default_required_version() -> CompactString {
REQUIRED_VERSION.into()
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Condition {
/// Condition name (e.g. IsWornHasKeyword)
#[serde(default)]
pub condition: String,
pub condition: CompactString,
#[serde(default = "default_required_version")]
#[serde(rename = "requiredVersion")]
/// The required version for this condition.
pub required_version: String,
pub required_version: CompactString,
/// condition to **Not** (default is `false`).
#[serde(default)]
// NOTE: There is code written under the assumption that it is skipped when false (e.g. IsEquipped).
Expand All @@ -26,8 +27,8 @@ pub struct Condition {
impl Default for Condition {
fn default() -> Self {
Self {
condition: String::default(),
required_version: REQUIRED_VERSION.to_string(),
condition: CompactString::default(),
required_version: default_required_version(),
negated: false,
}
}
Expand All @@ -37,7 +38,7 @@ impl Condition {
/// Creates a new `Condition` with the specified name.
pub fn new(condition: &str) -> Self {
Self {
condition: condition.to_string(),
condition: condition.into(),
..Default::default()
}
}
Expand Down Expand Up @@ -68,19 +69,19 @@ mod tests {
#[test]
fn serialize_condition() {
let condition = Condition {
condition: "SomeCondition".to_string(),
required_version: REQUIRED_VERSION.to_string(),
condition: "SomeCondition".into(),
required_version: REQUIRED_VERSION.into(),
negated: true,
};
let serialized_json = serde_json::to_string_pretty(&condition).unwrap();

let expected_json = r#"{
"condition": "SomeCondition",
"requiredVersion": "1.0.0.0",
"negated": true
}"#;

let serialized_json = serde_json::to_string_pretty(&condition).unwrap();
assert_eq!(expected_json, serialized_json);
assert_eq!(serialized_json, expected_json);
}

#[test]
Expand All @@ -90,14 +91,14 @@ mod tests {
"requiredVersion": "1.0.0.0",
"negated": false
}"#;
let deserialized: Condition = serde_json::from_str(json_str).unwrap();

let expected_condition = Condition {
condition: "AnotherCondition".to_string(),
required_version: REQUIRED_VERSION.to_string(),
let expected = Condition {
condition: "AnotherCondition".into(),
required_version: REQUIRED_VERSION.into(),
negated: false,
};

let deserialized_condition: Condition = serde_json::from_str(json_str).unwrap();
assert_eq!(expected_condition, deserialized_condition);
assert_eq!(deserialized, expected);
}
}
8 changes: 6 additions & 2 deletions dar2oar_core/src/conditions/condition_config.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
use super::ConditionSet;
use compact_str::CompactString;
use serde::{Deserialize, Serialize};

/// Each animation root config.json
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ConditionsConfig {
/// # NOTE
/// An arbitrary name given by the user (value in the mapping table) will probably exceed 24bytes.
/// Therefore, it should not be a [CompactString].
#[serde(default)]
pub name: String,
#[serde(default)]
pub description: String,
pub description: CompactString,
#[serde(default)]
pub priority: i32,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "overrideAnimationsFolder")]
pub override_animations_folder: Option<String>,
pub override_animations_folder: Option<CompactString>,
#[serde(default)]
pub conditions: Vec<ConditionSet>,
}
15 changes: 9 additions & 6 deletions dar2oar_core/src/conditions/current_weather.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use super::{condition::default_required_version, is_false};
use crate::values::PluginValue;
use compact_str::CompactString;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CurrentWeather {
/// Condition name "CurrentWeather"
pub condition: String,
pub condition: CompactString,
#[serde(default = "default_required_version")]
#[serde(rename = "requiredVersion")]
pub required_version: String,
pub required_version: CompactString,
#[serde(default)]
#[serde(skip_serializing_if = "is_false")]
pub negated: bool,
Expand Down Expand Up @@ -37,6 +38,7 @@ mod tests {
#[test]
fn should_serialize_current_weather() {
let current_weather = CurrentWeather::default();
let serialized = serde_json::to_string_pretty(&current_weather).unwrap();

let expected = r#"{
"condition": "CurrentWeather",
Expand All @@ -46,8 +48,8 @@ mod tests {
"formID": ""
}
}"#;
let serialized = serde_json::to_string_pretty(&current_weather).unwrap();
assert_eq!(expected, serialized);

assert_eq!(serialized, expected);
}

#[test]
Expand All @@ -60,9 +62,10 @@ mod tests {
"formID": ""
}
}"#;
let deserialized: CurrentWeather = serde_json::from_str(json_str).unwrap();

let expected = CurrentWeather::default();
let deserialized: CurrentWeather = serde_json::from_str(json_str).unwrap();
assert_eq!(expected, deserialized);

assert_eq!(deserialized, expected);
}
}
Loading

0 comments on commit 7460726

Please sign in to comment.