Skip to content

perf: Optimize BTree search using binary search #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 8, 2024

Conversation

ie-Yoshisaur
Copy link
Contributor

Description

This pull request optimizes the search function in the BTree implementation by replacing the linear search with a binary search algorithm. This change significantly improves the search performance, especially for large trees, without altering the existing functionality.

The optimization is achieved by modifying the search method in the BTree struct to use the binary_search method on the keys vector instead of a while loop. This change reduces the time complexity of searching within a node from O(n) to O(log n), where n is the number of keys in the node.

Type of change

  • New feature (non-breaking change which adds functionality)

Implementation Details

The main changes are in the search method of the BTree struct:

  1. Replaced the while loop with binary_search method on the keys vector.

Benchmark Results

Environment: MacOS 14.5, Apple M1 Pro, 32 GB RAM
Dataset: 1,000,000 random integers for insertion, 1,000,000 searches for the key 500,000

Before optimization:

  • Insertion: 3.002587333s
  • Search: 2.334683584s

After optimization:

  • Insertion: 2.998482583s
  • Search: 288.659458ms

Note: Insertion time remains largely unchanged, as expected.

Checklist:

  • I ran the following commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I checked CONTRIBUTING.md and my code follows its guidelines.

All existing tests pass with the new implementation. No new tests were added as the existing ones adequately cover the modified functionality.

This commit optimizes the search function in the BTree implementation
by replacing the linear search with a binary search algorithm. This
change significantly improves the search performance, especially for
large trees.

Implementation details:
- Modified the `search` method in the `BTree` struct
- Replaced the while loop with `binary_search` method on the `keys` vector

Complexity analysis:
- Previous implementation: O(n) per node, where n is the number of keys
- New implementation: O(log n) per node

Benchmark results:
- Environment: MacOS 14.5, Apple M1 Pro, 32 GB RAM
- Dataset: 1,000,000 random integers for insertion
- Search: 1,000,000 searches for the key 500,000
- Before:
  - Insertion: 3.002587333s
  - Search: 2.334683584s
- After:
  - Insertion: 2.998482583s
  - Search: 288.659458ms

Note: Insertion time remains largely unchanged, as expected. All
existing tests pass with the new implementation.

Benchmark code is not included in this commit.
@codecov-commenter
Copy link

codecov-commenter commented Jul 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.08%. Comparing base (65ca19d) to head (b772765).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #767   +/-   ##
=======================================
  Coverage   95.07%   95.08%           
=======================================
  Files         309      309           
  Lines       22781    22788    +7     
=======================================
+ Hits        21660    21667    +7     
  Misses       1121     1121           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@vil02 vil02 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting that. I added few tests: b772765.

By the way: would you like to spend some more time with this class? Code like BTree::new(0) or BTree::new(1) should not work in my opinion. Feel free to open a new PR fixing that.

@vil02 vil02 merged commit f08e936 into TheAlgorithms:master Aug 8, 2024
4 checks passed
@ie-Yoshisaur
Copy link
Contributor Author

By the way: would you like to spend some more time with this class?

Of course! I’ll work on fixing that issue and submit a PR soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants