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

Optimize the logic of remove and add entity. #1930

Merged
merged 4 commits into from
Feb 18, 2025

Conversation

cptbtptpbcptdtptp
Copy link
Collaborator

@cptbtptpbcptdtptp cptbtptpbcptdtptp commented Dec 27, 2023

Summary by CodeRabbit

  • New Features

    • Enhanced the Scene API to allow specifying an insertion position when adding entities, providing improved control over entity order.
  • Refactor

    • Streamlined the management of child entities to ensure consistent ordering and stability when adding or removing items from collections.

@cptbtptpbcptdtptp cptbtptpbcptdtptp added the low priority Low priority issue label Dec 27, 2023
@cptbtptpbcptdtptp cptbtptpbcptdtptp added this to the 1.2 milestone Dec 27, 2023
@cptbtptpbcptdtptp cptbtptpbcptdtptp self-assigned this Dec 27, 2023
@GuoLei1990 GuoLei1990 changed the base branch from dev/1.2 to main March 11, 2024 09:47
children.splice(index, 0, child);
for (let i = index + 1, n = childCount + 1; i < n; i++) {
children[i]._siblingIndex++;
children.length = childCount + 1;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe can mergechildren.length = childCount + 1; ?

for (let i = childCount; i > index; i--) {
const swapChild = children[i - 1];
swapChild._siblingIndex = i;
children[i] = swapChild;
Copy link
Member

Choose a reason for hiding this comment

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

Warp a small func?

for (let i = entity._siblingIndex; i < count; i++) {
const child = rootEntities[i + 1];
rootEntities[i] = child;
child._siblingIndex = i;
Copy link
Member

Choose a reason for hiding this comment

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

Same with before warp suggestion

Copy link

coderabbitai bot commented Feb 18, 2025

Walkthrough

This update introduces two new static methods in the Entity class to centralize the manipulation of child entities: one for adding and one for removing children. The addChild and the logic for entity removal now leverage these methods. Additionally, the Scene class has been refactored to delegate root entity management to the Entity class, including an updated method signature for addRootEntity and removal of an obsolete internal method. These changes streamline the code by ensuring consistent handling of children across the system.

Changes

File Change Summary
packages/core/src/Entity.ts - Added two new static methods: _removeFormChildren (to remove a child and adjust sibling indices) and _addToChildren (to insert a child at a specific index or at end).
- Modified addChild and removal logic to use these new methods.
packages/core/src/Scene.ts - Updated addRootEntity to include an index parameter and delegate child management to the new Entity methods.
- Removed the _removeFromEntityList method and updated removal logic accordingly.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Scene
    participant Entity
    Caller->>Scene: addRootEntity(index, entity)
    Scene->>Entity: _addToChildren(children, entity, index)
    Entity-->>Scene: Children array updated
    Scene-->>Caller: Confirmation of addition
Loading
sequenceDiagram
    participant Caller
    participant Scene
    participant Entity
    Caller->>Scene: removeRootEntity(entity)
    Scene->>Entity: _removeFormChildren(children, entity)
    Entity-->>Scene: Children array updated
    Scene-->>Caller: Confirmation of removal
Loading

Possibly related PRs

  • Fix entity find path #2541: Modifications in the Entity class now align with the new static methods used for managing children in the Scene class.

Poem

Hoppin' through the code with glee,
I found new methods, swift and free.
Adding and removing with a bound,
Child entities now leap around.
A rabbit cheers this coding spree! 🐇🎉

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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

codecov bot commented Feb 18, 2025

Codecov Report

Attention: Patch coverage is 68.88889% with 14 lines in your changes missing coverage. Please review.

Project coverage is 68.94%. Comparing base (8695423) to head (21747ad).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
packages/core/src/Entity.ts 70.73% 12 Missing ⚠️
packages/core/src/Scene.ts 50.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1930   +/-   ##
=======================================
  Coverage   68.93%   68.94%           
=======================================
  Files         961      961           
  Lines      100286   100271   -15     
  Branches     8655     8657    +2     
=======================================
- Hits        69131    69128    -3     
+ Misses      30895    30883   -12     
  Partials      260      260           
Flag Coverage Δ
unittests 68.94% <68.88%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

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: 2

🧹 Nitpick comments (2)
packages/core/src/Entity.ts (1)

98-116: Add test coverage for index bounds checking.

The index bounds checking logic in _addToChildren is not covered by tests. Please add test cases to verify the behavior when:

  • Index is negative
  • Index is greater than the children count
  • Index is undefined (append mode)

Would you like me to generate test cases for these scenarios?

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 105-115: packages/core/src/Entity.ts#L105-L115
Added lines #L105 - L115 were not covered by tests

packages/core/src/Scene.ts (1)

342-342: Add test coverage for root entity management.

The new index-based root entity management functionality is not covered by tests. Please add test cases to verify:

  • Adding root entities at specific indices
  • Removing root entities

Would you like me to generate test cases for these scenarios?

Also applies to: 344-344

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8695423 and 21747ad.

📒 Files selected for processing (2)
  • packages/core/src/Entity.ts (4 hunks)
  • packages/core/src/Scene.ts (2 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
packages/core/src/Entity.ts

[warning] 105-115: packages/core/src/Entity.ts#L105-L115
Added lines #L105 - L115 were not covered by tests


[warning] 622-622: packages/core/src/Entity.ts#L622
Added line #L622 was not covered by tests

packages/core/src/Scene.ts

[warning] 340-340: packages/core/src/Scene.ts#L340
Added line #L340 was not covered by tests


[warning] 344-344: packages/core/src/Scene.ts#L344
Added line #L344 was not covered by tests

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: e2e (22.x)

Comment on lines +84 to +93
static _removeFormChildren(children: Entity[], entity: Entity): void {
const count = children.length - 1;
for (let i = entity._siblingIndex; i < count; i++) {
const child = children[i + 1];
children[i] = child;
child._siblingIndex = i;
}
children.length = count;
entity._siblingIndex = -1;
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix typo in method name.

The method name has a typo: _removeFormChildren should be _removeFromChildren.

Apply this diff to fix the typo:

-  static _removeFormChildren(children: Entity[], entity: Entity): void {
+  static _removeFromChildren(children: Entity[], entity: Entity): void {

Note: This change will require updating all references to this method.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
static _removeFormChildren(children: Entity[], entity: Entity): void {
const count = children.length - 1;
for (let i = entity._siblingIndex; i < count; i++) {
const child = children[i + 1];
children[i] = child;
child._siblingIndex = i;
}
children.length = count;
entity._siblingIndex = -1;
}
static _removeFromChildren(children: Entity[], entity: Entity): void {
const count = children.length - 1;
for (let i = entity._siblingIndex; i < count; i++) {
const child = children[i + 1];
children[i] = child;
child._siblingIndex = i;
}
children.length = count;
entity._siblingIndex = -1;
}

@@ -337,11 +337,11 @@
const oldScene = entity._scene;
if (oldScene !== this) {
if (oldScene && isRoot) {
oldScene._removeFromEntityList(entity);
Entity._removeFormChildren(oldScene._rootEntities, entity);
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Update method name to match the fix in Entity class.

Once the typo in Entity._removeFormChildren is fixed, update the references here to use the corrected name.

Apply this diff to fix the references:

-      Entity._removeFormChildren(oldScene._rootEntities, child);
+      Entity._removeFromChildren(oldScene._rootEntities, child);

Also applies to: 378-378

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 340-340: packages/core/src/Scene.ts#L340
Added line #L340 was not covered by tests

@GuoLei1990 GuoLei1990 merged commit 6234a8a into galacean:main Feb 18, 2025
8 of 9 checks passed
@GuoLei1990 GuoLei1990 added the ignore for release ignore for release label Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ignore for release ignore for release low priority Low priority issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants