Skip to content

Commit 6c6e428

Browse files
authored
Sort by cached key (#1292)
I learned about `sort_by_cached_key()` in #1287, so I went back and checked we use that instead of `sort_by_key()` everywhere the key lookup is not trivial or allocates.
1 parent 129a5e7 commit 6c6e428

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

crates/ark/src/modules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ mod debug {
282282
// https://github.com/posit-dev/positron/issues/11591#issuecomment-3816838107
283283
let mut entries: Vec<_> =
284284
std::fs::read_dir(directory)?.collect::<std::io::Result<Vec<_>>>()?;
285-
entries.sort_by_key(|entry| entry.path());
285+
entries.sort_by_cached_key(|entry| entry.path());
286286

287287
for entry in entries {
288288
import_file(&entry.path(), src, env)?;

crates/oak_scan/src/packages.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ fn order_by_collation(
245245
// it into the namespace, so it can't go in `files`; keep it as a standalone
246246
// script instead of dropping it. Sorted for a deterministic order.
247247
let mut leftover: Vec<(PathBuf, FileEntry)> = by_name.into_values().collect();
248-
leftover.sort_by_key(|(path, _)| basename_key(path));
248+
leftover.sort_by_cached_key(|(path, _)| basename_key(path));
249249
for (path, _) in &leftover {
250250
log::warn!(
251251
"R file `{}` is not listed in `Collate:`; treating it as a standalone \
@@ -263,7 +263,7 @@ fn order_by_collation(
263263
/// All files are loadable, in case-insensitive alphabetical order by basename.
264264
/// No leftover: without `Collate:`, R loads every R/ file.
265265
fn order_alphabetically(mut files: Vec<(PathBuf, FileEntry)>) -> (Vec<FileEntry>, Vec<FileEntry>) {
266-
files.sort_by_key(|(path, _)| basename_key(path));
266+
files.sort_by_cached_key(|(path, _)| basename_key(path));
267267
(
268268
files.into_iter().map(|(_, file)| file).collect(),
269269
Vec::new(),

0 commit comments

Comments
 (0)