-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Bolt: Optimize percent_decode to avoid allocation on unencoded strings #424
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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,16 +5,25 @@ use super::helpers::{ | |||||||||||||
| use crate::interpreter::environment::Environment; | ||||||||||||||
| use crate::interpreter::error::RuntimeError; | ||||||||||||||
| use crate::interpreter::value::Value; | ||||||||||||||
| use std::borrow::Cow; | ||||||||||||||
| use std::cell::RefCell; | ||||||||||||||
| use std::rc::Rc; | ||||||||||||||
| use std::sync::Arc; | ||||||||||||||
|
|
||||||||||||||
| /// Decode percent-encoded URL string | ||||||||||||||
| /// Converts '+' to space and decodes %HH hex sequences | ||||||||||||||
| /// Invalid sequences are left as-is | ||||||||||||||
| fn percent_decode(s: &str) -> String { | ||||||||||||||
| let mut result = Vec::new(); | ||||||||||||||
| fn percent_decode(s: &str) -> Cow<'_, str> { | ||||||||||||||
| let bytes = s.as_bytes(); | ||||||||||||||
|
|
||||||||||||||
| // Optimization: avoid string allocation and decoding overhead if the string | ||||||||||||||
| // doesn't contain any encoded characters ('%' or '+'). | ||||||||||||||
| // We scan bytes in a single pass to be efficient. | ||||||||||||||
| if !bytes.iter().any(|&b| b == b'%' || b == b'+') { | ||||||||||||||
| return Cow::Borrowed(s); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| let mut result = Vec::with_capacity(bytes.len()); | ||||||||||||||
| let mut i = 0; | ||||||||||||||
|
|
||||||||||||||
| while i < bytes.len() { | ||||||||||||||
|
|
@@ -43,8 +52,10 @@ fn percent_decode(s: &str) -> String { | |||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Convert bytes to String, replacing invalid UTF-8 with replacement character | ||||||||||||||
| String::from_utf8(result) | ||||||||||||||
| .unwrap_or_else(|e| String::from_utf8_lossy(&e.into_bytes()).into_owned()) | ||||||||||||||
| Cow::Owned( | ||||||||||||||
| String::from_utf8(result) | ||||||||||||||
| .unwrap_or_else(|e| String::from_utf8_lossy(&e.into_bytes()).into_owned()), | ||||||||||||||
| ) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /// Parse key-value pairs with URL decoding | ||||||||||||||
|
|
@@ -73,13 +84,13 @@ fn parse_key_value_pairs( | |||||||||||||
| let key = if trim_parts { key.trim() } else { key }; | ||||||||||||||
| let value = if trim_parts { value.trim() } else { value }; | ||||||||||||||
|
|
||||||||||||||
| let decoded_key = percent_decode(key); | ||||||||||||||
| let decoded_key = percent_decode(key).into_owned(); | ||||||||||||||
| let decoded_value = percent_decode(value); | ||||||||||||||
| params.insert(decoded_key, Value::Text(Arc::from(decoded_value))); | ||||||||||||||
| params.insert(decoded_key, Value::Text(Arc::from(decoded_value.as_ref()))); | ||||||||||||||
|
||||||||||||||
| params.insert(decoded_key, Value::Text(Arc::from(decoded_value.as_ref()))); | |
| let value_arc: Arc<str> = match decoded_value { | |
| Cow::Borrowed(s) => Arc::from(s), | |
| Cow::Owned(s) => Arc::from(s), | |
| }; | |
| params.insert(decoded_key, Value::Text(value_arc)); |
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 changes the return type/behavioral path of URL decoding (borrowed fast-path vs owned decoding) but there are no Rust tests covering
parse_query_string/parse_key_value_pairsbehavior (including '+' handling and invalid % sequences). Since other text stdlib features are covered by integration tests, adding coverage here would help prevent regressions when optimizing this hot path.