address review: gate paste link index, keep first preset name, name-only pattern refs

This commit is contained in:
Dylan M. Taylor committed 2026-11-04 01:32:14 +00:00
1 parent fe65d6067f
commit dc3fdbf3c0
3 files changed
+32 -6

No files matched your search

+4 -1
View File
@@ -466,7 +466,10 @@ fn paste_layers(s: &mut Session, layers: &Document, dx: i32, dy: i32) -> Result<
// The destination's records by block key, collected once: a copy keeps duplicate records,
// and checking each against every destination record was quadratic in their number.
let mut present: std::collections::HashMap<[u8; 4], std::collections::HashSet<&[u8]>> = std::collections::HashMap::new();
for (_, dk, dd) in d.doc.metadata.psd_global_blocks.iter().filter(|(_, k, _)| matches!(k, b"lnk2" | b"lnk3" | b"lnkD" | b"lnkE")) {
// Only when the clipboard carries linked blocks: an ordinary paste would hash every destination file for nothing.
let is_lnk = |k: &[u8; 4]| matches!(k, b"lnk2" | b"lnk3" | b"lnkD" | b"lnkE");
let clipboard_links = copies.metadata.psd_global_blocks.iter().any(|(_, k, _)| is_lnk(k));
for (_, dk, dd) in d.doc.metadata.psd_global_blocks.iter().filter(|(_, k, _)| clipboard_links && is_lnk(k)) {
present.entry(*dk).or_default().extend(photocraft_io::linked::records(dd));
}
for (signature, key, data) in copies.metadata.psd_global_blocks.iter().filter(|(_, k, _)| matches!(k, b"lnk2" | b"lnk3" | b"lnkD" | b"lnkE")) {
+9 -3
View File
@@ -263,7 +263,11 @@ fn export_import(s: &mut Session, p: &Value) -> Result<Value> {
// Merge by name through an index built once: a file of many distinct names must not
// rescan the growing library for each (quadratic, on the command thread).
if want("brushes") {
let mut by_name: std::collections::HashMap<String, usize> = s.tools.presets.iter().enumerate().map(|(i, x)| (x.name.clone(), i)).collect();
// The first entry of a name wins, as the scan this replaced found it.
let mut by_name: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
for (i, x) in s.tools.presets.iter().enumerate() {
by_name.entry(x.name.clone()).or_insert(i);
}
for mut b in file.brushes {
// Nested folders as the commands bound them (a hand-edited file can't build an
// absurd tree).
@@ -281,8 +285,10 @@ fn export_import(s: &mut Session, p: &Value) -> Result<Value> {
s.brush_presets_changed();
}
if want("customShapes") {
let mut by_name: std::collections::HashMap<String, usize> =
s.edit_state.custom_shapes.iter().enumerate().map(|(i, x)| (x.name.clone(), i)).collect();
let mut by_name: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
for (i, x) in s.edit_state.custom_shapes.iter().enumerate() {
by_name.entry(x.name.clone()).or_insert(i);
}
for c in file.custom_shapes {
match by_name.get(&c.name) {
Some(&i) => s.edit_state.custom_shapes[i] = c,
+19 -2
View File
@@ -48,12 +48,13 @@ pub struct AslImport {
/// Patterns an effect list refers to: `(name, id)` in first-use order, without repeats.
pub fn referenced_patterns(effects: &[Effect]) -> Vec<(String, String)> {
let mut out: Vec<(String, String)> = Vec::new();
// Seen ids and (for references without an id) names: a style of many pattern strokes must
// not rescan the list per reference.
// Seen ids and the names of every reference kept (a reference without an id is a repeat of any
// kept one with its name): a style of many pattern strokes must not rescan the list per reference.
let (mut ids, mut names): (HashSet<String>, HashSet<String>) = (HashSet::new(), HashSet::new());
let mut push = |name: &str, id: &str| {
let new = if id.is_empty() { names.insert(name.to_string()) } else { ids.insert(id.to_string()) };
if new {
names.insert(name.to_string());
out.push((name.to_string(), id.to_string()));
}
};
@@ -285,6 +286,22 @@ mod tests {
]
}
/// A name-only reference after a reference with that name and an id is a repeat of it.
#[test]
fn name_only_reference_repeats_a_named_one() {
let overlay = |id: &str| Effect::PatternOverlay {
common: FxCommon { enabled: true, blend: BlendMode::Normal, opacity: 1.0 },
name: "N".into(),
id: id.into(),
scale: 1.0,
angle: 0.0,
link: true,
phase: (0.0, 0.0),
};
assert_eq!(referenced_patterns(&[overlay("X"), overlay("")]), vec![("N".to_string(), "X".to_string())]);
assert_eq!(referenced_patterns(&[overlay(""), overlay("X")]).len(), 2);
}
#[test]
fn round_trip_keeps_effects_blending_and_patterns() {
let pats = vec![pattern("pat-1", SampleType::U8), pattern("pat-2", SampleType::U16)];