⚡ perf: optimize notebook keyword replacement speed - #561
Conversation
…ed BNGNotebook.write() to use whole-file string replacement\ninstead of nested line-by-line dictionary iterations. This significantly\nimproves generation speed (3-4x in benchmarks) while retaining identical\nsequential replacement semantics and lowering memory overhead by\navoiding list allocation for lines. Co-authored-by: akutuva21 <44119804+akutuva21@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. |
💡 What: Refactored
BNGNotebook.write()inbionetgen/core/notebook.pyto replace keywords across the entire file string using a singlef.read()and.replace()loop, rather than looping over individual lines withf.readlines().🎯 Why: The previous nested loop (
O(lines * keys)) in Python was very slow and allocated a list of strings for the entire file. By leveraging Python's highly optimized, native C-levelstr.replaceon the entire file payload, we drastically reduce execution time and memory overhead while maintaining strict sequential replacement semantics. A regex implementation was tested but ultimately discarded to ensure complex/overlapping keys evaluate identically to the original logic.📊 Measured Improvement:
Benchmarking a 100,000-line template with 1,000 keys:
Benchmarking a 100,000-line template with 100 keys:
PR created automatically by Jules for task 15965814901288633720 started by @akutuva21