perf(etcd): refactor some code to improve performance#12011
perf(etcd): refactor some code to improve performance#12011xuruidong wants to merge 4 commits intoapache:masterfrom
Conversation
Signed-off-by: xuruidong <xuruidong@gmail.com>
|
@xuruidong Just approved to run the CI |
please run the CI again @juzhiyuan |
Hi @xuruidong, I couldn't find the Re-run button. Can you confirm the failed test cases?
|
|
I don't think the CI failure is related to the code changes. |
|
Hi @xuruidong, can you add some descriptive information for PR? |
|
Hi @xuruidong, please add a PR description so that others can review it better ~ |
Hi @Baoyuantop , the PR description has been added, please help review it. Thanks. |
|
Please fix the failed ci |
Hi @Baoyuantop ,please trigger the ci |
|
Hi @Baoyuantop , the ci is not stable, the failed test should be rerun |
|
Hi @xuruidong, there are still bad CIs that need to be fixed. |
|
Hi @xuruidong, any updates? |
Hi @Baoyuantop ,please trigger the ci |
|
This pull request has been marked as stale due to 60 days of inactivity. It will be closed in 4 weeks if no further activity occurs. If you think that's incorrect or this pull request should instead be reviewed, please simply write any comment. Even if closed, you can still revive the PR at any time or discuss it on the dev@apisix.apache.org list. Thank you for your contributions. |
|
Hi @xuruidong, are you still working on this pull request? There are some failed tests that need to be addressed. |
Hi @Baoyuantop ,please trigger the CI |
|
This pull request has been marked as stale due to 60 days of inactivity. It will be closed in 4 weeks if no further activity occurs. If you think that's incorrect or this pull request should instead be reviewed, please simply write any comment. Even if closed, you can still revive the PR at any time or discuss it on the dev@apisix.apache.org list. Thank you for your contributions. |
|
This pull request has been marked as stale due to 60 days of inactivity. It will be closed in 4 weeks if no further activity occurs. If you think that's incorrect or this pull request should instead be reviewed, please simply write any comment. Even if closed, you can still revive the PR at any time or discuss it on the dev@apisix.apache.org list. Thank you for your contributions. |
There was a problem hiding this comment.
Pull request overview
This pull request optimizes the array compaction logic in the etcd configuration synchronization code to improve memory usage and performance. The optimization targets the cleanup routine that runs after 100+ deletions to remove stale entries from the values array.
Changes:
- Added
table.removeas a localized functionremove_tabfollowing codebase conventions - Refactored the array compaction algorithm to perform in-place compaction instead of copy-clear-rebuild
- Combined
values_hashrebuilding with array compaction into a single pass
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| local pre = 1 | ||
| local cur = 1 |
There was a problem hiding this comment.
The variable names pre and cur are unclear and could be more descriptive. Consider renaming to write_idx and read_idx respectively to better convey their purpose in the compaction algorithm. write_idx represents the next position to write a valid element, while read_idx tracks the current position being read from the original array.
| local pre = 1 | ||
| local cur = 1 | ||
| table.clear(self.values_hash) | ||
| log.info("clear stale data in `values_hash` for key: ", key) | ||
| for _, val in ipairs(self.values) do | ||
| if val then | ||
| table.insert(self.values, val) | ||
| self.values[pre] = val | ||
| key = short_key(self, val.key) | ||
| self.values_hash[key] = pre | ||
| pre = pre + 1 | ||
| end | ||
| end | ||
|
|
||
| table.clear(self.values_hash) | ||
| log.info("clear stale data in `values_hash` for key: ", key) | ||
| cur = cur + 1 | ||
| end | ||
|
|
||
| for i = 1, #self.values do | ||
| key = short_key(self, self.values[i].key) | ||
| self.values_hash[key] = i | ||
| for i = cur - 1, pre, -1 do | ||
| remove_tab(self.values, i) | ||
| end |
There was a problem hiding this comment.
The refactored array compaction logic (lines 811-828) lacks test coverage. This is a critical code path that triggers after 100 deletions to clean up the values array. Consider adding a test that creates multiple items, deletes them to increment sync_times beyond 100, and verifies the array is correctly compacted with no data corruption.
| local cur = 1 | ||
| table.clear(self.values_hash) | ||
| log.info("clear stale data in `values_hash` for key: ", key) | ||
| for _, val in ipairs(self.values) do | ||
| if val then | ||
| table.insert(self.values, val) | ||
| self.values[pre] = val | ||
| key = short_key(self, val.key) | ||
| self.values_hash[key] = pre | ||
| pre = pre + 1 | ||
| end | ||
| end | ||
|
|
||
| table.clear(self.values_hash) | ||
| log.info("clear stale data in `values_hash` for key: ", key) | ||
| cur = cur + 1 | ||
| end | ||
|
|
||
| for i = 1, #self.values do | ||
| key = short_key(self, self.values[i].key) | ||
| self.values_hash[key] = i | ||
| for i = cur - 1, pre, -1 do |
There was a problem hiding this comment.
The cur variable is only used to track the iteration count and could be replaced with #self.values captured before the loop. For example: local original_len = #self.values before line 815, then use original_len instead of cur - 1 in line 826. This would eliminate the need for incrementing cur on every iteration and make the code slightly clearer.
| key = short_key(self, val.key) | ||
| self.values_hash[key] = pre |
There was a problem hiding this comment.
The variable key shadows the outer loop variable declared at line 726. While this doesn't cause incorrect behavior since key is reassigned at the start of each outer loop iteration, it's poor practice and reduces code clarity. Consider using a different variable name such as item_key or short_k for the inner usage.
| key = short_key(self, val.key) | |
| self.values_hash[key] = pre | |
| local item_key = short_key(self, val.key) | |
| self.values_hash[item_key] = pre |

Description
Fixes # (issue)
Before:
The original code performed a cleanup of
self.valuesand rebuiltself.values_hashby:self.valuesnilvaluesvalues_hashin a separate stepProblems:
Now:
nilvalues while keeping the same tablevalues_hashat the same timeWhy better?
Before:
The original code performed a cleanup of
self.valuesand rebuiltself.values_hashby:self.valuesnilvaluesvalues_hashin a separate stepProblems:
Now:
nilvalues while keeping the same tablevalues_hashat the same timeWhy better?
Checklist