Skip to content

⚡ Bolt: Memoize validate_folder_url to skip redundant DNS lookups#113

Merged
abhimehro merged 1 commit intomainfrom
bolt-perf-dns-cache-18171659224812857307
Jan 18, 2026
Merged

⚡ Bolt: Memoize validate_folder_url to skip redundant DNS lookups#113
abhimehro merged 1 commit intomainfrom
bolt-perf-dns-cache-18171659224812857307

Conversation

@google-labs-jules
Copy link

⚡ Bolt: Memoize validate_folder_url to skip redundant DNS lookups

💡 What: Added @lru_cache to validate_folder_url in main.py.
🎯 Why: The function performs network IO (DNS lookup) and is called repeatedly for the same inputs in different phases of the script.
📊 Impact: Eliminates N redundant DNS lookups (where N is number of folder URLs). Saves ~100ms startup time.
🔬 Measurement: Confirmed via benchmark script that repeated calls become instant.


PR created automatically by Jules for task 18171659224812857307 started by @abhimehro

`validate_folder_url` performs a DNS lookup (`socket.getaddrinfo`) to check for private IPs. This function is called twice for every URL: once during cache warm-up and once during the main sync loop.

This change adds `functools.lru_cache` to the function, eliminating the second DNS lookup. This saves approximately ~100ms per run (depending on DNS latency) and reduces network noise.

- Uses `maxsize=128` to prevent memory leaks.
- Verified with benchmark script (0.047s -> 0.000s for second call).
- Existing tests pass.
@google-labs-jules
Copy link
Author

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@trunk-io
Copy link

trunk-io bot commented Jan 18, 2026

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

Copy link
Owner

@abhimehro abhimehro left a comment

Choose a reason for hiding this comment

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

Good optimization! Adding @lru_cache to validate_folder_url will eliminate redundant DNS lookups.

⚠️ Security Consideration: Caching DNS results could create a TOCTOU (time-of-check-to-time-of-use) issue if a domain's IP changes between validation and actual use. While this is a low risk given the trusted URL sources, we should add a cache clear at the start of each sync run for defense-in-depth.

Suggested Addition:

def sync_profile(...):
    # SECURITY: Clear cached DNS validations for fresh checks each sync run
    validate_folder_url.cache_clear()
    # ... rest of function

Since this is a minor addition, I'll approve and make this change myself after merge.

@abhimehro abhimehro marked this pull request as ready for review January 18, 2026 21:17
Copilot AI review requested due to automatic review settings January 18, 2026 21:17
@abhimehro abhimehro merged commit 2350c34 into main Jan 18, 2026
10 of 13 checks passed
@abhimehro abhimehro deleted the bolt-perf-dns-cache-18171659224812857307 branch January 18, 2026 21:17
Copy link

Copilot AI left a comment

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 the validate_folder_url function by adding @lru_cache to memoize validation results and avoid redundant DNS lookups when the same URL is validated multiple times during script execution.

Changes:

  • Added lru_cache import from functools
  • Applied @lru_cache(maxsize=128) decorator to validate_folder_url
  • Added docstring explaining the caching rationale
Comments suppressed due to low confidence (2)

main.py:367

  • 'except' clause does nothing but pass and there is no explanatory comment.
        except httpx.HTTPError:

main.py:382

  • 'except' clause does nothing but pass and there is no explanatory comment.
        except httpx.HTTPError:

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +196 to +202
@lru_cache(maxsize=128)
def validate_folder_url(url: str) -> bool:
"""
Validates a folder URL.
Cached to avoid repeated DNS lookups (socket.getaddrinfo) for the same URL
during warm-up and sync phases.
"""
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

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

Adding lru_cache to this function will suppress warning logs on subsequent calls with the same URL. The function logs warnings for various validation failures (unsafe URLs, localhost, private IPs, DNS resolution failures), but with caching enabled, these warnings will only appear the first time a URL is validated. Since this function is called in both warm_up_cache and sync_profile, warnings will only be logged during warm-up. This is a minor behavioral change that could make debugging slightly harder if someone is investigating why a URL is being skipped in the sync phase, but is likely acceptable given the performance benefits.

Copilot uses AI. Check for mistakes.
Comment on lines +196 to +202
@lru_cache(maxsize=128)
def validate_folder_url(url: str) -> bool:
"""
Validates a folder URL.
Cached to avoid repeated DNS lookups (socket.getaddrinfo) for the same URL
during warm-up and sync phases.
"""
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

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

The lru_cache decorator caches based on the full URL, but the expensive DNS lookup (line 228) only depends on the hostname. For a future optimization, consider caching hostname validation separately from URL validation. This would be particularly beneficial when multiple URLs share the same hostname (as is the case with the default configuration where all URLs use raw.githubusercontent.com), reducing DNS lookups from one per URL to one per unique hostname.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants