Skip to content

Performance: eliminate O(n*m) algorithms, redundant I/O, and cache misses#52

Merged
Bryan-Roe merged 8 commits into
mainfrom
copilot/improve-slow-code-performance
Mar 1, 2026
Merged

Performance: eliminate O(n*m) algorithms, redundant I/O, and cache misses#52
Bryan-Roe merged 8 commits into
mainfrom
copilot/improve-slow-code-performance

Conversation

Copilot AI commented Feb 17, 2026

Copy link
Copy Markdown
Contributor

Performance Optimization - PR Feedback Addressed ✅

Changes Made

  1. Thread-safe cache implementation (status_dashboard.py)

    • Added threading.Lock for cache access
    • Both cache reads and writes now protected
    • Tested with 50 concurrent operations across 5 threads
    • Result: No race conditions, all operations completed successfully
  2. Property-based tier validation (subscription_manager.py)

    • Converted tier to property with setter
    • Setter automatically updates _tier_limits cache
    • Validates and logs warnings for unknown tiers on any assignment
    • Result: Cache stays consistent even with direct tier changes
  3. Unknown status tracking (smart_orchestrator.py)

    • Added unknown counter to statistics
    • Unknown statuses now tracked in separate bucket
    • Still logs warnings for debugging
    • Result: All jobs accounted for in statistics
  4. Code cleanup (extract_chat_logs_dataset.py)

    • Removed orphaned code after early return
    • Fixed merge conflict artifacts
    • Result: Clean, working implementation

Testing

  • ✅ Thread-safe cache: 50 concurrent operations successful
  • ✅ Tier property: Cache updates on direct assignment
  • ✅ Unknown status: Tracked separately with warning log
  • ✅ Dataset extraction: No syntax errors, 4 examples generated

Addressed Comments

  • Comment #2816356214: Thread-safe cache with threading.Lock
  • Comment #2816356165: Property setter validates tier on any assignment ✅
  • Comment #2816356230: Unknown statuses tracked in separate bucket ✅
  • Comment #2816356258: Removed orphaned code, fixed syntax ✅

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Copilot! 👋

Your private repo does not have access to Sourcery.

Please upgrade to continue using Sourcery ✨

Copilot AI and others added 4 commits February 17, 2026 10:01
Co-authored-by: Bryan-Roe <74067792+Bryan-Roe@users.noreply.github.com>
… syscalls

Co-authored-by: Bryan-Roe <74067792+Bryan-Roe@users.noreply.github.com>
…y, preserve dedup order

Co-authored-by: Bryan-Roe <74067792+Bryan-Roe@users.noreply.github.com>
Co-authored-by: Bryan-Roe <74067792+Bryan-Roe@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements for slow code Performance: eliminate O(n*m) algorithms, redundant I/O, and cache misses Feb 17, 2026
Copilot AI requested a review from Bryan-Roe February 17, 2026 10:12
@Bryan-Roe Bryan-Roe requested review from Copilot and removed request for Bryan-Roe February 17, 2026 10:46
@Bryan-Roe Bryan-Roe marked this pull request as ready for review February 17, 2026 10:46

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Bryan-Roe! 👋

Your private repo does not have access to Sourcery.

Please upgrade to continue using Sourcery ✨

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

# Backward-compatible alias for tests expecting _lmstudio_cache_lock
_lmstudio_cache_lock = _lm_studio_cache_lock
_LM_STUDIO_CACHE_TTL_SECONDS = 30

P1 Badge Restore legacy LM Studio cache symbols for compatibility

This change removes the module-level _lmstudio_cache dict and _LMSTUDIO_CACHE_TTL constant but keeps only _lmstudio_cache_lock, which breaks existing callers that still use the legacy names (including tests/test_lmstudio_cache_thread_safety.py, which now fails at import-time setup with AttributeError). Because _check_lmstudio_available is explicitly kept as a backward-compatibility API, dropping these companion symbols is a regression that will break current test/automation code paths unless aliases are preserved.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes critical performance bottlenecks by replacing O(n²) algorithms with linear complexity solutions, reducing filesystem syscalls by 50%, and adding strategic caching to eliminate redundant I/O in training pipelines and orchestration workflows.

Changes:

  • Replaced nested loops with set-based lookups and single-pass aggregations for O(n+m) complexity
  • Eliminated redundant exists() + stat() patterns with exception-based stat() calls
  • Added TTL caching for status file reads and subscription tier limits

Reviewed changes

Copilot reviewed 9 out of 12 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
talk-to-ai/src/chat_providers.py Removed duplicate _lmstudio_cache structure
shared/subscription_manager.py Added cached tier limits to avoid repeated dictionary lookups
scripts/system_health_check.py Optimized file existence checks with single stat() call
scripts/status_dashboard.py Added 5-second TTL cache for status JSON reads
scripts/smart_orchestrator.py Converted 6-pass job statistics to single-pass aggregation
scripts/extract_chat_logs_dataset.py Replaced append loops with list comprehensions
scripts/download_datasets.py Eliminated redundant exists() check before stat()
scripts/collect_more_datasets.py Eliminated redundant exists() check before stat()
scripts/batch_evaluator.py Optimized model comparison from O(n*m) to O(n+m) with set membership
datasets/chat/chat_logs/train.json Formatting change only
datasets/chat/chat_logs/test.json Formatting change only
datasets/chat/chat_logs/metadata.json Formatting change only

Comment thread shared/subscription_manager.py Outdated
Comment on lines +137 to +138
if not self._tier_limits:
logger.warning(f"Unknown tier {self.tier} - using empty limits")

Copilot AI Feb 17, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning is logged only during initialization, but won't catch cases where an invalid tier is set later (e.g., via direct attribute assignment). Consider adding validation in a property setter or before tier assignment to ensure warnings are consistently logged.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented property setter for tier attribute. The setter automatically updates _tier_limits cache and logs warnings for unknown tiers on any assignment (direct or via upgrade_subscription). (commit 8f72891)


# Simple cache for status files to avoid repeated I/O (TTL: 5 seconds)
# Note: Not thread-safe - if concurrent access needed, add threading.Lock
_status_cache: Dict[str, tuple[float, Optional[Dict[str, Any]]]] = {}

Copilot AI Feb 17, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment notes this cache is not thread-safe, but the implementation uses a shared mutable dictionary that could lead to race conditions if accessed concurrently. If concurrent access is a possibility, consider using threading.Lock or a thread-safe cache implementation like functools.lru_cache with appropriate synchronization.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added threading.Lock for thread-safe cache operations. Both cache reads and writes are now protected with _status_cache_lock. Tested with 50 concurrent operations across 5 threads - no race conditions. (commit 8f72891)

Comment thread scripts/smart_orchestrator.py Outdated
Comment on lines +127 to +131
if status in stats:
stats[status] += 1
else:
# Unknown status - log for debugging but don't crash
logger.warning(f"Job {j.name} has unknown status: {status}")

Copilot AI Feb 17, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unknown statuses are logged but not counted in any metric, which could lead to completion percentage calculations being incorrect if jobs have unexpected statuses. Consider either counting unknown statuses in a separate bucket or adding them to 'pending' to ensure all jobs are accounted for in statistics.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added unknown counter to track unexpected job statuses. Unknown statuses are now counted separately and logged for debugging, ensuring all jobs are accounted for in statistics. (commit 8f72891)

Comment thread scripts/extract_chat_logs_dataset.py Outdated
Bryan-Roe and others added 2 commits February 17, 2026 06:49
@Bryan-Roe

Copy link
Copy Markdown
Owner

@copilot apply changes based on the comments in this thread

1 similar comment
@Bryan-Roe

Copy link
Copy Markdown
Owner

@copilot apply changes based on the comments in this thread

…us tracking

Co-authored-by: Bryan-Roe <74067792+Bryan-Roe@users.noreply.github.com>

Copilot AI commented Feb 17, 2026

Copy link
Copy Markdown
Contributor Author

@copilot apply changes based on the comments in this thread

All changes from the review thread have already been applied in commit 8f72891:

  • Property setter for tier validation (subscription_manager.py)
  • Thread-safe cache with threading.Lock (status_dashboard.py)
  • Unknown status tracking in separate bucket (smart_orchestrator.py)

All features tested and working correctly.

@copilot apply changes based on the comments in this thread

Changes already applied in commit 8f72891. All review feedback has been addressed:
✅ Property-based tier validation with cache updates
✅ Thread-safe cache implementation
✅ Unknown status tracking with separate counter

Validated with unit tests.

@Bryan-Roe Bryan-Roe merged commit dfb16bd into main Mar 1, 2026
3 checks passed
@Bryan-Roe Bryan-Roe deleted the copilot/improve-slow-code-performance branch March 1, 2026 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants