Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@
## 2024-07-30 - Avoid chained string replace when checking character sets
**Learning:** Using chained `.replace(a, "").replace(b, "")` to check if a string consists entirely of specific characters requires intermediate string allocations for every call. In benchmarks, using `.strip("ab")` is ~30% faster and avoids multiple allocations in the hot path.
**Action:** When checking if a string is solely composed of specific characters, use `.strip(chars)` instead of chained `.replace()` calls to improve performance.

## 2024-07-31 - Avoid unnecessary string allocations when checking for visible text
**Learning:** Using `bool(text.strip())` to check if a string contains non-whitespace characters allocates a new string unnecessarily. This adds measurable overhead in hot parsing loops.
**Action:** Use early truthiness and `.isspace()` checks (e.g., `bool(text) and not text.isspace()`) instead of `.strip()` to determine if a string contains visible text without allocating a new string.
4 changes: 2 additions & 2 deletions src/newsdom_api/equivalence.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def _article_has_headline(article: dict[str, Any]) -> bool:
return headline_present

headline = article.get("headline")
# ⚡ Bolt: Early truthiness return to avoid allocating a stripped string when it is empty
return isinstance(headline, str) and bool(headline) and bool(headline.strip())
# ⚡ Bolt: Early truthiness return to avoid allocating a stripped string when it is empty, and use .isspace() instead of .strip() to avoid unnecessary string allocations
return isinstance(headline, str) and bool(headline) and not headline.isspace()


def _process_articles(metrics: dict[str, Any], articles: list[Any]) -> None:
Expand Down
Loading