When working with data structures, especially ropes and collections, avoid algorithms that result in quadratic time complexity when linear alternatives exist. Common patterns to watch for include repeated indexing operations and nested loops over related data sets.
When working with data structures, especially ropes and collections, avoid algorithms that result in quadratic time complexity when linear alternatives exist. Common patterns to watch for include repeated indexing operations and nested loops over related data sets.
Key anti-patterns:
(0..=cursor_char).rev().find(|&i| !is_word(text.char(i)))
is O(W*log(N))code_actions_on_save.iter().any(|a| match &x.kind { ... })
can be O(M*N)Better approaches:
text.chars_at(cursor_char).take_while(is_word).count()
is O(log(N)+W)let actions_set: HashSet<_> = code_actions_on_save.iter().collect()
makes lookups O(1)partition_point
instead of binary_search_by_key
when you don’t need the exact matchExample transformation:
// Inefficient O(W*log(N)) - repeated rope indexing
let start = (0..=cursor_char)
.rev()
.find(|&i| !is_word(text.char(i)))
.map(|i| i + 1)
.unwrap_or(0);
// Efficient O(log(N)+W) - iterator-based
let start = cursor_char - text.chars_at(cursor_char)
.reversed()
.take_while(|&c| is_word(c))
.count();
Always consider the complexity of your algorithm, especially when dealing with text processing, search operations, or nested data access patterns.
Enter the URL of a public GitHub repository