Skip to content
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

Improve performance for creating crdt.TreeNode #939

Merged
merged 5 commits into from
Jul 24, 2024
Merged

Improve performance for creating crdt.TreeNode #939

merged 5 commits into from
Jul 24, 2024

Conversation

m4ushold
Copy link
Contributor

@m4ushold m4ushold commented Jul 23, 2024

What this PR does / why we need it:

The time complexity of creating crdt.TreeNode is O(N^2), potentially causing performance bottlenecks. It's optimized to O(n).

While this may not be a significant issue currently, there is a risk that as the number of tree nodes in the protobuf increases, operations will scale quadratically, potentially causing performance bottlenecks.

Which issue(s) this PR fixes:

Addresses #930

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


Additional documentation:


Checklist:

  • Added relevant tests or not required
  • Didn't break anything

Summary by CodeRabbit

Summary by CodeRabbit

  • Improved Performance

    • Enhanced efficiency in managing parent-child relationships among tree node instances, particularly for larger datasets.
    • Introduced benchmarking tests to assess performance for tree editing and conversion operations, offering insights into efficiency.
  • Code Readability

    • Simplified control flow in the management of parent-child relationships, improving overall code comprehension.

The time complexity of creating crdt.TreeNode is O(N^2), potentially causing
performance bottlenecks. It's optimized to O(n).
@CLAassistant
Copy link

CLAassistant commented Jul 23, 2024

CLA assistant check
All committers have signed the CLA.

Copy link

coderabbitai bot commented Jul 23, 2024

Walkthrough

The recent changes enhance the FromTreeNodes function in api/converter/from_pb.go, streamlining the management of parent-child relationships among TreeNode instances. By introducing a depthTable, the new implementation allows direct access to parent nodes, significantly boosting efficiency and readability, especially for larger datasets. Additionally, new benchmarking tests have been added to assess the performance of tree editing operations across various sizes.

Changes

File Change Summary
api/converter/from_pb.go Optimized FromTreeNodes function by replacing nested loops with a depthTable for direct parent node access, improving time complexity and overall performance.
test/bench/tree_editing_bench_test.go Introduced benchmarking tests for tree editing operations with BenchmarkTreeEditing and BenchmarkTreeConverting, assessing performance across various tree sizes.

Poem

In the forest deep, where tree nodes grow,
A rabbit found a way to make them flow.
With a map of depth, oh what a sight,
No more loops to slow down the night!
Hopping through code, so swift and bright,
Efficiency gained, oh what a delight! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b494fa2 and 0466649.

Files selected for processing (1)
  • api/converter/from_pb.go (1 hunks)
Additional comments not posted (3)
api/converter/from_pb.go (3)

586-587: Initialize depthTable map with root node.

The depthTable map is initialized to store the most recently processed node at each depth, starting with the root node.


589-589: Use depthTable for direct parent node access.

The depthTable map is used to directly access the parent node based on its depth, eliminating the need for nested loops.


594-594: Update depthTable with the current node.

After processing each node, the current node is added to the depthTable, ensuring that the parent can be quickly accessed in subsequent iterations.

Copy link
Member

@hackerwins hackerwins 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 your contribution.

It would be good to add a benchmark test to measure Tree Performance. How about adding tree_editing_bench_test.go and adding the test code you wrote before?

In order to check the performance of the tree, a tree_editing_bench_test file
was added and a tree converting test was implemented.
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0466649 and 4800147.

Files selected for processing (1)
  • test/bench/tree_editing_bench_test.go (1 hunks)
Additional context used
GitHub Check: bench
test/bench/tree_editing_bench_test.go

[failure] 53-53:
undefined: api

Additional comments not posted (5)
test/bench/tree_editing_bench_test.go (5)

1-20: Header and package declaration are correct.

The build constraints and licensing information are standard and correctly declared.


21-27: Import statements are correct.

The imported packages are necessary and correctly used in the file.


29-31: BenchmarkTreeEditing function is correct.

The function correctly sets up the benchmark by calling BenchmarkTreeConverting.


33-50: BenchmarkTreeConverting function is correct.

The function correctly sets up and runs multiple benchmarks with different vertex counts.


68-74: TreeConverting function is correct.

The function correctly sets up and runs the benchmark for converting protobuf tree nodes to crdt.TreeNode.

test/bench/tree_editing_bench_test.go Show resolved Hide resolved
Omitted api package is added
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4800147 and f312305.

Files selected for processing (1)
  • test/bench/tree_editing_bench_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • test/bench/tree_editing_bench_test.go

Copy link
Member

@hackerwins hackerwins left a comment

Choose a reason for hiding this comment

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

LGTM. 👍

test/bench/tree_editing_bench_test.go Outdated Show resolved Hide resolved
test/bench/tree_editing_bench_test.go Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f312305 and 516bed9.

Files selected for processing (1)
  • test/bench/tree_editing_bench_test.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • test/bench/tree_editing_bench_test.go

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 516bed9 and 4cdcc3c.

Files selected for processing (1)
  • test/bench/tree_editing_bench_test.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • test/bench/tree_editing_bench_test.go

@hackerwins hackerwins merged commit 85a1a3a into yorkie-team:main Jul 24, 2024
4 checks passed
hackerwins added a commit that referenced this pull request Jul 24, 2024
The time complexity of creating crdt.TreeNode is O(N^2), potentially
causing performance bottlenecks. It's optimized to O(n).

While this may not be a significant issue currently, there is a risk
that as the number of tree nodes in the protobuf increases, operations
will scale quadratically, potentially causing performance bottlenecks.

---------

Co-authored-by: JiHwan Yim <raararaara@gmail.com>
Co-authored-by: Youngteac Hong <susukang98@gmail.com>
@hackerwins
Copy link
Member

I've cleaned up the benchmark code a little more. Please refer to the commit below.
379ee54

Before:
Screenshot 2024-07-24 at 10 35 45 AM

After:
Screenshot 2024-07-24 at 10 35 13 AM

raararaara added a commit that referenced this pull request Oct 7, 2024
The time complexity of creating crdt.TreeNode is O(N^2), potentially
causing performance bottlenecks. It's optimized to O(n).

While this may not be a significant issue currently, there is a risk
that as the number of tree nodes in the protobuf increases, operations
will scale quadratically, potentially causing performance bottlenecks.

---------

Co-authored-by: JiHwan Yim <raararaara@gmail.com>
Co-authored-by: Youngteac Hong <susukang98@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Development

Successfully merging this pull request may close these issues.

3 participants