⚡ Bolt: [performance improvement]#54
Conversation
- Applied `@functools.lru_cache(maxsize=None)` to `get_variants` in `printify_manager.py`, `merch_factory.py`, and `create_merch.py`. - Changed return types to immutable `tuple` to ensure cached data thread safety. - Documented findings in `.jules/bolt.md`. Co-authored-by: merg357 <221854052+merg357@users.noreply.github.com>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
This PR reduces repeated Printify API calls when fetching blueprint/provider variants by memoizing get_variants() results and returning immutable tuples to prevent accidental downstream mutation of cached data.
Changes:
- Added
@functools.lru_cache(maxsize=None)toget_variants()in multiple scripts to avoid repeated identical/variants.jsonrequests. - Changed
get_variants()to returntupleinstead oflistfor immutability safety with caching. - Added a short “Bolt” learning note documenting the memoization pattern.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| printify_manager.py | Memoizes variant ID fetches and returns tuples to reduce repeated catalog calls during product creation. |
| merch_factory.py | Memoizes variant ID fetches (tuple return) to speed up bulk creation loops. |
| create_merch.py | Memoizes variant ID fetches (tuple return) to reduce repeated requests across blueprint iterations. |
| .jules/bolt.md | Documents the rationale and recommended memoization pattern for external API calls. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @functools.lru_cache(maxsize=None) | ||
| def get_variants(blueprint_id: int, provider_id: int) -> tuple: | ||
| url = f"{BASE_URL}/catalog/blueprints/{blueprint_id}/print_providers/{provider_id}/variants.json" | ||
| resp = requests.get(url, headers=HEADERS) |
| return tuple([v["id"] for v in resp.json().get("variants", [])[:4]]) | ||
|
|
||
| def create_product(title: str, blueprint_id: int, provider_id: int, | ||
| variant_ids: list, image_id: str) -> dict: |
| @functools.lru_cache(maxsize=None) | ||
| def get_variants(blueprint_id: int, print_provider_id: int = 29) -> tuple: | ||
| url = f"{BASE_URL}/catalog/blueprints/{blueprint_id}/print_providers/{print_provider_id}/variants.json" | ||
| resp = requests.get(url, headers=get_headers()) |
| @functools.lru_cache(maxsize=None) | ||
| def get_variants(blueprint_id: int, provider_id: int) -> tuple: | ||
| url = f"{BASE_URL}/catalog/blueprints/{blueprint_id}/print_providers/{provider_id}/variants.json" | ||
| resp = requests.get(url, headers=HEADERS) |
| return tuple([v["id"] for v in resp.json().get("variants", [])[:4]]) | ||
|
|
||
| def create_product(title: str, blueprint_id: int, provider_id: int, | ||
| variant_ids: list, image_id: str) -> dict: |
| @functools.lru_cache(maxsize=None) | ||
| def get_variants(blueprint_id: int, print_provider_id: int = 29) -> tuple: | ||
| url = f"{BASE_URL}/catalog/blueprints/{blueprint_id}/print_providers/{print_provider_id}/variants.json" | ||
| resp = requests.get(url, headers=get_headers()) |
💡 What: Added
functools.lru_cache(maxsize=None)to theget_variantsfunctions across the codebase and casted the returned sequence to an immutabletuple.🎯 Why: In scripts like
create_merch_v2.pyand loops inmerch_factory.py, identical API queries are made continuously to fetch the available blueprint variants. This creates a large N+1 network bottleneck, slowing down bulk creation. Using an LRU cache avoids duplicate queries, and returning immutable tuples prevents the cached result from accidentally mutating downstream while maintaining drop-in compatibility withjson.dumps().📊 Impact: Reduces duplicate network requests directly to Printify's API when creating multiple products matching identical blueprints (e.g., creating 8 Bella Canvas Tees). Expected to save ~300-800ms per identical blueprint loop hit.
🔬 Measurement: Measure the time saved per batch operation (e.g., using
time python3 merch_factory.py). The second iteration on the same blueprint variant will hit cache, bypassing the typical API response latency entirely.PR created automatically by Jules for task 2125679433527546181 started by @merg357