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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,6 @@
## 2024-11-20 - Optimize multiple tuple generation from a single collection
**Learning:** `build_rule_metadata` derives exactly two collections, `owasp` and `cwe`, from the same references. Replacing its two generator traversals with one explicit loop reduces element visits from about 2N to N. Both versions remain O(N), so this is a constant-factor optimization rather than an asymptotic complexity improvement.
**Action:** Combine repeated traversal when fixed derived collections share one source, while preserving ordering and classification semantics. Benchmark the production hot path before claiming a material wall-clock improvement.
## 2026-10-06 - O(N) Loop Consolidation Optimization
**Learning:** Combining multiple list comprehensions and sum() traversals into a single `for` loop significantly improves performance by reducing the number of traversals over the same dataset. For large datasets, this single-pass optimization provides a measurable constant-factor improvement.
**Action:** When calculating multiple aggregations from the same iterable, prefer a single `for` loop over multiple list comprehensions and sum() calls to iterate through the data only once.
66 changes: 41 additions & 25 deletions appguardrail_core/org_intelligence.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,35 +122,51 @@ def build_org_inventory(
active_repository_target: int = 20,
) -> OrgInventory:
"""Build a stable organization inventory from GitHub repo JSON."""
repo_list = list(repos)
nonforks = [repo for repo in repo_list if not _truthy(repo.get("isFork"))]
forks = [repo for repo in repo_list if _truthy(repo.get("isFork"))]
primary_languages = Counter(_primary_language(repo) for repo in repo_list)
default_branches = Counter(_default_branch(repo) for repo in repo_list)
unsupported = sorted(
{
_primary_language(repo)
for repo in nonforks
if _primary_language(repo) not in SUPPORTED_PRIMARY_LANGUAGES
and _primary_language(repo) != "Unknown"
}
)
supported_nonforks = sum(
1 for repo in nonforks if _primary_language(repo) in SUPPORTED_PRIMARY_LANGUAGES
)
# Bolt: O(N) single-pass optimization over repositories
# Combined multiple list comprehensions and sum() traversals into one O(N) loop
total_repositories = 0
nonfork_repositories = 0
fork_repositories = 0
private_repositories = 0
supported_nonfork_repositories = 0

primary_languages: Counter[str] = Counter()
default_branches: Counter[str] = Counter()
unsupported_set: set[str] = set()

for repo in repos:
total_repositories += 1
is_fork = _truthy(repo.get("isFork"))

if is_fork:
fork_repositories += 1
else:
nonfork_repositories += 1

if _truthy(repo.get("isPrivate")):
private_repositories += 1

lang = _primary_language(repo)
primary_languages[lang] += 1
default_branches[_default_branch(repo)] += 1

if not is_fork:
if lang in SUPPORTED_PRIMARY_LANGUAGES:
supported_nonfork_repositories += 1
elif lang != "Unknown":
unsupported_set.add(lang)

return OrgInventory(
total_repositories=len(repo_list),
nonfork_repositories=len(nonforks),
fork_repositories=len(forks),
private_repositories=sum(
1 for repo in repo_list if _truthy(repo.get("isPrivate"))
),
supported_nonfork_repositories=supported_nonforks,
unsupported_nonfork_languages=tuple(unsupported),
total_repositories=total_repositories,
nonfork_repositories=nonfork_repositories,
fork_repositories=fork_repositories,
private_repositories=private_repositories,
supported_nonfork_repositories=supported_nonfork_repositories,
unsupported_nonfork_languages=tuple(sorted(unsupported_set)),
primary_language_counts=_sorted_counts(primary_languages),
default_branch_counts=_sorted_counts(default_branches),
active_repository_target=active_repository_target,
active_repository_target_met=len(nonforks) >= active_repository_target,
active_repository_target_met=nonfork_repositories >= active_repository_target,
)


Expand Down
Loading