Skip to content

Commit 8bfe1a0

Browse files
committed
Fix alias dedup bug and remove Box<dyn Iterator> heap allocation
- Make HashSet mutable in alias/spoiler_alias merging so duplicates within `other` are properly caught during the loop - Replace Box<dyn Iterator> with slice reference + chain for spoiler alias iteration, avoiding unnecessary heap allocation on hot path https://claude.ai/code/session_01NRsURdxvKrse2dQTBW7LZ4
1 parent 3157f27 commit 8bfe1a0

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

yomitan-dict-builder/src/dict_builder.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,19 +296,19 @@ impl DictBuilder {
296296

297297
// Aliases: union (deduplicated)
298298
if !other.aliases.is_empty() {
299-
let existing: HashSet<String> = base.aliases.iter().cloned().collect();
299+
let mut existing: HashSet<String> = base.aliases.iter().cloned().collect();
300300
for alias in &other.aliases {
301-
if !alias.is_empty() && !existing.contains(alias) {
301+
if !alias.is_empty() && existing.insert(alias.clone()) {
302302
base.aliases.push(alias.clone());
303303
}
304304
}
305305
}
306306

307307
// Spoiler aliases: union (deduplicated)
308308
if !other.spoiler_aliases.is_empty() {
309-
let existing: HashSet<String> = base.spoiler_aliases.iter().cloned().collect();
309+
let mut existing: HashSet<String> = base.spoiler_aliases.iter().cloned().collect();
310310
for alias in &other.spoiler_aliases {
311-
if !alias.is_empty() && !existing.contains(alias) {
311+
if !alias.is_empty() && existing.insert(alias.clone()) {
312312
base.spoiler_aliases.push(alias.clone());
313313
}
314314
}
@@ -686,13 +686,13 @@ impl DictBuilder {
686686

687687
// --- Alias entries ---
688688
// Include spoiler aliases when the user has enabled spoilers
689-
let all_aliases: Box<dyn Iterator<Item = &String>> = if self.settings.show_spoilers {
690-
Box::new(char.aliases.iter().chain(char.spoiler_aliases.iter()))
689+
let spoiler_aliases: &[String] = if self.settings.show_spoilers {
690+
&char.spoiler_aliases
691691
} else {
692-
Box::new(char.aliases.iter())
692+
&[]
693693
};
694694

695-
for alias in all_aliases {
695+
for alias in char.aliases.iter().chain(spoiler_aliases.iter()) {
696696
if alias.is_empty() {
697697
continue;
698698
}

0 commit comments

Comments
 (0)