Skip to content

Performance: extract_field_name in proc-macro is O(n²) due to unnecessary iterator clone #27

Description

@ZialeHub

Description

In pulsync-derive/src/lib.rs, extract_field_name checks for the "self." prefix by cloning the entire remaining character iterator and collecting it into a String on every { encountered:

  // line 126 — allocates a new String of all remaining chars on every '{'
  if chars.clone().collect::<String>().as_str().starts_with("self.") {

For a format string with K fields and average remaining length L, this is O(K × L) allocations — quadratic in the number of characters in the worst case.

Affected file

  • pulsync-derive/src/lib.rs — line 126

Fix

std::str::Chars exposes the remaining string slice via .as_str(), which avoids any allocation:

  // O(5), zero allocation
  if chars.as_str().starts_with("self.") {
      for _ in 0..5 { chars.next(); }
  }

This is a one-line change that makes the method O(n) overall.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions