-
-
Notifications
You must be signed in to change notification settings - Fork 127
perf!: reduce startup allocations by ~21% and peak heap by ~31% #525
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,7 +106,7 @@ impl crate::protocol::ProtocolNode for AbProp { | |
| } | ||
| let config_value = optional_attr(node, "config_value") | ||
| .ok_or_else(|| anyhow::anyhow!("missing config_value in prop"))? | ||
| .to_string(); | ||
| .into_owned(); | ||
| let config_expo_key = optional_attr(node, "config_expo_key").and_then(|s| s.parse().ok()); | ||
|
|
||
| Ok(Self { | ||
|
|
@@ -189,31 +189,31 @@ impl crate::protocol::ProtocolNode for AbPropConfig { | |
| } | ||
|
|
||
| fn try_from_node_ref(node: &NodeRef<'_>) -> Result<Self, anyhow::Error> { | ||
| use crate::iq::node::optional_attr; | ||
|
|
||
| if node.tag != "prop" { | ||
| return Err(anyhow::anyhow!("expected <prop>, got <{}>", node.tag)); | ||
| } | ||
|
|
||
| let experiment = AbProp::try_from_node_ref(node); | ||
| if let Ok(prop) = experiment { | ||
| return Ok(Self::Experiment(prop)); | ||
| } | ||
|
|
||
| let sampling = SamplingProp::try_from_node_ref(node); | ||
| if let Ok(prop) = sampling { | ||
| return Ok(Self::Sampling(prop)); | ||
| // Check discriminating attribute to avoid double-parse allocations | ||
| let has_config = optional_attr(node, "config_code").is_some(); | ||
| let has_event = optional_attr(node, "event_code").is_some(); | ||
|
|
||
| if has_config && has_event { | ||
| Err(anyhow::anyhow!( | ||
| "prop has both config_code and event_code (attrs: {:?})", | ||
| node.attrs | ||
| )) | ||
| } else if has_config { | ||
| Ok(Self::Experiment(AbProp::try_from_node_ref(node)?)) | ||
| } else if has_event { | ||
| Ok(Self::Sampling(SamplingProp::try_from_node_ref(node)?)) | ||
| } else { | ||
| Err(anyhow::anyhow!( | ||
| "prop has neither config_code nor event_code (attrs: {:?})", | ||
| node.attrs | ||
| )) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Comment on lines
+192
to
+215
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Add regression tests for the new discriminator error branches. The parser logic is improved, but there’s no direct test coverage for the Proposed test additions@@
fn test_sampling_prop_protocol_node_round_trip() {
let prop = SamplingProp {
event_code: 5138,
sampling_weight: -1,
};
let node = prop.clone().into_node();
let parsed = SamplingProp::try_from_node(&node).unwrap();
assert_eq!(parsed.event_code, prop.event_code);
assert_eq!(parsed.sampling_weight, prop.sampling_weight);
}
+
+ #[test]
+ fn test_ab_prop_config_rejects_ambiguous_discriminator() {
+ let node = NodeBuilder::new("prop")
+ .attr("config_code", "100")
+ .attr("config_value", "enabled")
+ .attr("event_code", "5138")
+ .attr("sampling_weight", "-1")
+ .build();
+
+ let err = AbPropConfig::try_from_node(&node).unwrap_err();
+ assert!(err.to_string().contains("both config_code and event_code"));
+ }
+
+ #[test]
+ fn test_ab_prop_config_rejects_missing_discriminator() {
+ let node = NodeBuilder::new("prop")
+ .attr("config_value", "enabled")
+ .build();
+
+ let err = AbPropConfig::try_from_node(&node).unwrap_err();
+ assert!(err.to_string().contains("neither config_code nor event_code"));
+ }
}🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| let experiment_err = experiment | ||
| .err() | ||
| .unwrap_or_else(|| anyhow::anyhow!("unknown error")); | ||
| let sampling_err = sampling | ||
| .err() | ||
| .unwrap_or_else(|| anyhow::anyhow!("unknown error")); | ||
| Err(anyhow::anyhow!( | ||
| "prop did not match experiment or sampling config: experiment_err={}; sampling_err={}", | ||
| experiment_err, | ||
| sampling_err | ||
| )) | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🛠️ Refactor suggestion | 🟠 Major
Preserve caller order in the SQLite batch path.
eq_any(&ids)returns matching rows in database order and only once per stored ID, while the defaultSignalStore::load_prekeys_batchimplementation iterates the input slice and can return repeated IDs in request order. That makes this new API behave differently by backend.Proposed patch
async fn load_prekeys_batch(&self, ids: &[u32]) -> Result<Vec<(u32, Vec<u8>)>> { if ids.is_empty() { return Ok(Vec::new()); } let pool = self.pool.clone(); let device_id = self.device_id; - let ids: Vec<i32> = ids.iter().map(|&id| id as i32).collect(); + let requested_ids: Vec<u32> = ids.to_vec(); + let ids: Vec<i32> = requested_ids.iter().map(|&id| id as i32).collect(); self.with_semaphore(move || -> Result<Vec<(u32, Vec<u8>)>> { let mut conn = pool .get() .map_err(|e| StoreError::Connection(e.to_string()))?; let rows: Vec<(i32, Vec<u8>)> = prekeys::table @@ .filter(prekeys::device_id.eq(device_id)) .load(&mut conn) .map_err(|e| StoreError::Database(e.to_string()))?; - Ok(rows.into_iter().map(|(id, key)| (id as u32, key)).collect()) + let row_map: std::collections::HashMap<u32, Vec<u8>> = + rows.into_iter().map(|(id, key)| (id as u32, key)).collect(); + Ok(requested_ids + .into_iter() + .filter_map(|id| row_map.get(&id).cloned().map(|key| (id, key))) + .collect()) }) .await }📝 Committable suggestion
🤖 Prompt for AI Agents