Skip to content

Commit 97bd137

Browse files
authored
perf(rust-guard): remove avoidable allocations in hot path helpers
1 parent 1b6489b commit 97bd137

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

guards/github-guard/rust-guard/src/labels/tool_rules.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,17 +745,18 @@ fn check_file_secrecy(
745745
ctx: &PolicyContext,
746746
) -> Vec<String> {
747747
let path_lower = path.to_lowercase();
748-
let segments: Vec<&str> = path_lower.split('/').collect();
749748

750749
// Check for sensitive file extensions/names
751750
for pattern in SENSITIVE_FILE_PATTERNS {
752-
if path_lower.ends_with(pattern) || segments.iter().any(|seg| seg.starts_with(*pattern)) {
751+
if path_lower.ends_with(pattern)
752+
|| path_lower.split('/').any(|seg| seg.starts_with(*pattern))
753+
{
753754
return policy_private_scope_label(owner, repo, repo_id, ctx);
754755
}
755756
}
756757

757758
// Get filename
758-
let filename = segments.last().copied().unwrap_or(path_lower.as_str());
759+
let filename = path_lower.rsplit('/').next().unwrap_or(&path_lower);
759760

760761
// Check for sensitive keywords in filename
761762
for keyword in SENSITIVE_FILE_KEYWORDS {

guards/github-guard/rust-guard/src/lib.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use labels::{
2121
use serde::{Deserialize, Serialize};
2222
use serde_json::Value;
2323
use std::alloc::{alloc as std_alloc, dealloc as std_dealloc, Layout};
24+
use std::borrow::Cow;
2425
use std::slice;
2526
use std::sync::Mutex;
2627

@@ -257,9 +258,13 @@ struct LabelResponseOutput {
257258
items: Vec<LabeledItem>,
258259
}
259260

260-
fn infer_scope_for_baseline(tool_name: &str, tool_args: &Value, repo_id: &str) -> String {
261+
fn infer_scope_for_baseline<'a>(
262+
tool_name: &str,
263+
tool_args: &Value,
264+
repo_id: &'a str,
265+
) -> Cow<'a, str> {
261266
if !repo_id.is_empty() {
262-
return repo_id.to_string();
267+
return Cow::Borrowed(repo_id);
263268
}
264269

265270
match tool_name {
@@ -268,16 +273,16 @@ fn infer_scope_for_baseline(tool_name: &str, tool_args: &Value, repo_id: &str) -
268273
| "manage_notification_subscription"
269274
| "manage_repository_notification_subscription"
270275
| "create_repository"
271-
| "fork_repository" => scope_names::GITHUB.to_string(),
276+
| "fork_repository" => Cow::Borrowed(scope_names::GITHUB),
272277
"search_code" | "search_issues" | "search_pull_requests" => {
273278
let query = tool_args
274279
.get("query")
275280
.and_then(|v| v.as_str())
276281
.unwrap_or("");
277282
let (_, _, repo_from_query) = extract_repo_info_from_search_query(query);
278-
repo_from_query
283+
Cow::Owned(repo_from_query)
279284
}
280-
_ => String::new(),
285+
_ => Cow::Borrowed(""),
281286
}
282287
}
283288

@@ -1096,9 +1101,18 @@ mod tests {
10961101
let tool_args = json!({"query": "repo:lpcox/github-guard README"});
10971102

10981103
let inferred = infer_scope_for_baseline("search_code", &tool_args, "");
1104+
assert!(matches!(inferred, Cow::Owned(_)));
10991105
assert_eq!(inferred, "lpcox/github-guard");
11001106
}
11011107

1108+
#[test]
1109+
fn infer_scope_for_baseline_borrows_repo_id_when_present() {
1110+
let tool_args = json!({});
1111+
let inferred = infer_scope_for_baseline("get_file_contents", &tool_args, "octocat/hello-world");
1112+
1113+
assert!(matches!(inferred, Cow::Borrowed("octocat/hello-world")));
1114+
}
1115+
11021116
#[test]
11031117
fn search_code_baseline_preserves_scoped_integrity() {
11041118
let ctx = PolicyContext {

0 commit comments

Comments
 (0)