From a437373ff7f6a6f2070d7703738aad6634dd5338 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 4 May 2026 16:32:14 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Replace=20O(N^2)=20list=20p?= =?UTF-8?q?opping=20with=20O(N)=20index=20tracking=20in=20entropy=5Futils.?= =?UTF-8?q?py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces `list.pop(0)` within nested loops with an O(1) index increment variable `entropy_idx` to iterate over elements sequentially. This improves execution time inside `calculate_logits_entropy` and `speculate_calculate_logits_entropy` from O(N^2) to O(N). --- .jules/bolt.md | 3 +++ fastdeploy/model_executor/entropy_utils.py | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000000..d2508be30ba --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-04 - Inefficient list.pop(0) operations +**Learning:** Found multiple usages of `list.pop(0)` in the codebase, particularly in `fastdeploy/model_executor/entropy_utils.py` . `pop(0)` on a list is an O(N) operation because it requires shifting all subsequent elements. For queues or iterating over elements sequentially, using an index or `collections.deque` is much faster. +**Action:** Replace O(N) `pop(0)` operations with O(1) index tracking or `collections.deque` where appropriate to optimize execution performance. diff --git a/fastdeploy/model_executor/entropy_utils.py b/fastdeploy/model_executor/entropy_utils.py index 21d1b3421e9..e97a38d39a9 100644 --- a/fastdeploy/model_executor/entropy_utils.py +++ b/fastdeploy/model_executor/entropy_utils.py @@ -49,9 +49,11 @@ def calculate_logits_entropy(logits, share_inputs, temperature): entropy_tensor = get_entropy(logits) entropy = entropy_tensor.tolist() + entropy_idx = 0 for i in range(real_bsz): for _ in range(real_seq_lens[i]): - share_inputs["entropy_list"][i].append(entropy.pop(0)) + share_inputs["entropy_list"][i].append(entropy[entropy_idx]) + entropy_idx += 1 if ( share_inputs["stop_flags"][i] and share_inputs["seq_lens_decoder"][i] != 0 @@ -92,9 +94,11 @@ def speculate_calculate_logits_entropy(logits, share_inputs, temperature): entropy_tensor = get_entropy(accepted_logits) entropy = entropy_tensor.tolist() + entropy_idx = 0 for i in range(real_bsz): for _ in range(share_inputs["accept_num"][i]): - share_inputs["entropy_list"][i].append(entropy.pop(0)) + share_inputs["entropy_list"][i].append(entropy[entropy_idx]) + entropy_idx += 1 if ( share_inputs["stop_flags"][i] and share_inputs["seq_lens_decoder"][i] != 0