Skip to content

perf(core): reduce redundant grapheme conversions in common_prefix/suffix#7

Merged
lupodevelop merged 1 commit into
lupodevelop:mainfrom
lemorage:refine
Jan 2, 2026
Merged

perf(core): reduce redundant grapheme conversions in common_prefix/suffix#7
lupodevelop merged 1 commit into
lupodevelop:mainfrom
lemorage:refine

Conversation

@lemorage

@lemorage lemorage commented Jan 1, 2026

Copy link
Copy Markdown
Contributor

This PR tries to optimize the find_common_prefix helper function so to benefit the 2 callers.

Before:

fn find_common_prefix(reference, others: List(String), acc) {
  case reference {
    [char, ..rest_ref] -> {
      let all_have_char = list.all(others, fn(s) {
        case string.to_graphemes(s) { ... }  // ← O(n) conversion
      })
      let new_others = list.map(others, fn(s) {
        s |> string.to_graphemes |> list.drop(1) |> string.concat  // ← O(n²)
      })
      ...
    }
  }
}

Since this helper is recursively called in the caller, and we keep doing string↔grapheme conversions back and force, this is inefficient. My idea is that, we can just pass others as List(List(String)), so we don't need to convert it back and force, we just need to do one initialization in the caller part -> let others_chars = list.map(rest, string.to_graphemes).

@lupodevelop lupodevelop merged commit 4d0ccc4 into lupodevelop:main Jan 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants