Optimize searchTree function for improved performance with slice-based candidates and weight ordering#11
Merged
Conversation
Co-authored-by: cloorc <13597105+cloorc@users.noreply.github.com>
…ering, and weight ordering Co-authored-by: cloorc <13597105+cloorc@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Optimize searchTree method to prioritize working rules and highest weight
Optimize searchTree function for improved performance with slice-based candidates and weight ordering
Aug 26, 2025
cloorc
requested changes
Aug 26, 2025
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes the searchTree function in the forest-based rule matching system to improve performance through three key enhancements: slice-based candidate collection, status filtering during traversal, and weight-based insertion ordering.
- Replaces map-based candidate collection with direct slice manipulation to eliminate conversion overhead
- Implements status filtering during tree traversal instead of post-processing to reduce memory usage
- Adds weight-based insertion ordering to maintain highest-weight rules at the front without post-collection sorting
Reviewed Changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| types.go | Adds new RuleWithWeight struct to combine rule and weight data |
| test_data_rebuild/rules.json | Removes test data file (cleared content) |
| test_data_rebuild/dimensions.json | Removes test data file (cleared content) |
| rule_status_test.go | Comments out unused Values field assignments in test queries |
| matcher.go | Updates candidate handling to use RuleWithWeight and removes status filtering logic |
| forest_weight_test.go | Adds comprehensive tests for weight ordering, status filtering, and optimization behavior |
| forest.go | Implements optimized searchTree with slice-based collection, status filtering, and weight ordering |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
…irst Signed-off-by: Cloorc <wittcnezh@foxmail.com>
147363b to
70fae28
Compare
cloorc
approved these changes
Aug 26, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR optimizes the
searchTreefunction inforest.goto improve performance by implementing three key enhancements as specified in the requirements:Changes Made
1. Slice-based Candidate Collection
map[string]*Ruleto collect candidates, requiring map-to-slice conversion*[]*Ruleparameter for direct slice manipulation2. Status Filtering During Traversal
matcher.go3. Weight-based Insertion Ordering
insertRuleByWeight()helper methodPerformance Impact
The optimization provides several performance benefits:
Backward Compatibility
Test Coverage
Added comprehensive tests to verify:
The optimization maintains the same functional behavior while significantly improving performance by minimizing candidate reordering and avoiding unnecessary rule collection and filtering.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.