perf(get): reduce request entry allocations (#6029)

Co-authored-by: heihutu <heihutu@gmail.com>
This commit is contained in:
houseme
2026-08-13 03:07:59 +08:00
committed by GitHub
parent 398d2d87c8
commit 73bd5d9d95
3 changed files with 49 additions and 22 deletions
+20 -3
View File
@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::borrow::Cow;
use std::path::Component;
use std::path::Path;
use std::path::PathBuf;
@@ -44,14 +45,19 @@ pub fn has_suffix(s: &str, suffix: &str) -> bool {
/// If the object name ends with a slash, it is considered a directory object.
/// The trailing slash is removed and `GLOBAL_DIR_SUFFIX` is appended.
/// If it does not end with a slash, the name is returned as is.
pub fn encode_dir_object(object: &str) -> String {
pub fn encode_dir_object_ref(object: &str) -> Cow<'_, str> {
if has_suffix(object, SLASH_SEPARATOR) {
format!("{}{}", object.trim_end_matches(SLASH_SEPARATOR), GLOBAL_DIR_SUFFIX)
Cow::Owned(format!("{}{}", object.trim_end_matches(SLASH_SEPARATOR), GLOBAL_DIR_SUFFIX))
} else {
object.to_string()
Cow::Borrowed(object)
}
}
/// Owned compatibility wrapper for callers that retain or mutate the encoded name.
pub fn encode_dir_object(object: &str) -> String {
encode_dir_object_ref(object).into_owned()
}
/// Checks if the given object name represents a directory object.
///
/// Returns true if the object name ends with `GLOBAL_DIR_SUFFIX`.
@@ -602,6 +608,17 @@ mod tests {
use super::*;
use proptest::prelude::*;
#[test]
fn encode_dir_object_ref_borrows_objects_and_encodes_directories() {
let object = "prefix/object";
let encoded = encode_dir_object_ref(object);
assert!(matches!(encoded, Cow::Borrowed(value) if value == object));
let encoded = encode_dir_object_ref("prefix/directory/");
assert!(matches!(encoded, Cow::Owned(ref value) if value == "prefix/directory__XLDIR__"));
assert_eq!(encode_dir_object("prefix/directory/"), encoded);
}
#[test]
fn test_trim_etag() {
// Test with quoted ETag