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

Fix grid caching #5200

Merged
merged 4 commits into from
Dec 2, 2024
Merged

Fix grid caching #5200

merged 4 commits into from
Dec 2, 2024

Conversation

benjaminpkane
Copy link
Contributor

@benjaminpkane benjaminpkane commented Dec 2, 2024

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced item management and rendering logic in the Grid and Spotlight components.
    • Simplified visibility handling in the Row class.
  • Bug Fixes

    • Improved efficiency in item updates and rendering processes.
  • Refactor

    • Streamlined parameter handling in the show and Render methods.
    • Removed unnecessary properties and logic for better state management.
  • Documentation

    • Updated method signatures to reflect changes in parameters.

Copy link
Contributor

coderabbitai bot commented Dec 2, 2024

Walkthrough

This pull request introduces several modifications across multiple components, primarily focusing on the Grid, Row, and Section classes. Key changes include the replacement of parameters in methods to streamline logic, adjustments to state management, and improvements in rendering efficiency. Notable updates involve the removal of unused constants, simplification of item tracking, and enhancements to the handling of visibility and resizing. These changes collectively aim to improve the clarity and efficiency of the component's functionality while retaining core behaviors.

Changes

File Path Change Summary
app/packages/core/src/components/Grid/Grid.tsx Updated render method in Spotlight to replace soft with zooming, simplified looker attachment logic, adjusted useLayoutEffect and useEffect for better resizing handling.
app/packages/core/src/components/Grid/useRefreshers.ts Removed unused constant MAX_LRU_CACHE_SIZE, retaining functionality while simplifying cache configuration.
app/packages/core/src/components/Grid/useSelect.ts Introduced retained set for tracking updated item IDs, modified logic for managing store entries.
app/packages/spotlight/src/row.ts Updated show method to replace hidden with zooming, simplifying visibility logic and method parameters.
app/packages/spotlight/src/section.ts Added private property #itemIds, removed #dirty, and adjusted updateItems and render methods for improved item tracking.
app/packages/spotlight/src/types.ts Updated Render type signature to replace soft and disable with zooming, simplifying the function interface.

Possibly related PRs

  • Scale grid text #4747: This PR modifies the Grid component, specifically the introduction of a useFontSize hook that affects how the Grid and useSelect functions handle font size, which is relevant to the changes made in the main PR regarding the Grid component's rendering logic.
  • Add detach to spotlight #5157: This PR adds a detach method to the Spotlight instance within the Grid component, which is directly related to the changes in the main PR that also involve modifications to the Grid component and its interaction with Spotlight.

Suggested labels

enhancement

Suggested reviewers

  • sashankaryal
  • ritch
  • tom-vx51

🐰 In the meadow, where changes bloom,
The Grid now dances, dispelling the gloom.
With zooming in place, the Row shines bright,
As Sections align in the soft morning light.
A hop and a skip, the code flows with grace,
Simplifying logic, in this happy space! 🌼


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 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.

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.

@benjaminpkane benjaminpkane marked this pull request as ready for review December 2, 2024 20:04
Copy link
Contributor

@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

🧹 Outside diff range and nitpick comments (1)
app/packages/spotlight/src/section.ts (1)

451-454: Consider implementing cleanup for #itemIds Set

While the tracking logic is correct, the Set will continuously grow as items are only added. Consider implementing a cleanup mechanism when items are no longer needed.

+  // Add to class
+  cleanup() {
+    this.#itemIds.clear();
+  }

Call this method when the section is destroyed or when items are no longer needed.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7287351 and 949fc2c.

📒 Files selected for processing (6)
  • app/packages/core/src/components/Grid/Grid.tsx (2 hunks)
  • app/packages/core/src/components/Grid/useRefreshers.ts (0 hunks)
  • app/packages/core/src/components/Grid/useSelect.ts (1 hunks)
  • app/packages/spotlight/src/row.ts (2 hunks)
  • app/packages/spotlight/src/section.ts (5 hunks)
  • app/packages/spotlight/src/types.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • app/packages/core/src/components/Grid/useRefreshers.ts
🧰 Additional context used
📓 Path-based instructions (5)
app/packages/spotlight/src/types.ts (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

app/packages/core/src/components/Grid/useSelect.ts (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

app/packages/core/src/components/Grid/Grid.tsx (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

app/packages/spotlight/src/section.ts (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

app/packages/spotlight/src/row.ts (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

🔇 Additional comments (8)
app/packages/core/src/components/Grid/useSelect.ts (2)

19-27: Improved cache management with retained items tracking

Good implementation of tracking valid items using a Set. The early return when instance is not found helps prevent unnecessary processing.


34-37: Efficient cleanup of stale cache entries

Good addition of store cleanup logic. This prevents memory leaks by removing entries that are no longer needed.

Let's verify the cache cleanup behavior:

✅ Verification successful

Cache cleanup implementation is correctly implemented

The cleanup logic in useSelect.ts is properly implemented using an LRU cache (LRUCache from 'lru-cache'). The code maintains a set of active IDs (retained) and removes entries that are no longer in use, which is the correct pattern for cache cleanup. The implementation is consistent with other cache cleanup patterns found in the codebase, and the use of LRUCache provides additional automatic cache management benefits.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for other cache cleanup implementations to ensure consistency
rg -A 3 "store\.delete|cache\.delete" --type typescript

Length of output: 92


Script:

#!/bin/bash
# Check for other cache cleanup implementations to ensure consistency
rg -A 3 "store\.delete|cache\.delete"

Length of output: 2103


Script:

#!/bin/bash
# Let's check the store implementation and usage in the Grid component
ast-grep --pattern 'const store = $$$'

Length of output: 930


Script:

#!/bin/bash
# Let's examine the store implementation in useSelect.ts
cat app/packages/core/src/components/Grid/useSelect.ts

Length of output: 1318

app/packages/spotlight/src/types.ts (1)

41-41: ⚠️ Potential issue

Breaking change: Simplified Render type signature

The replacement of soft and disable parameters with a single zooming parameter improves the API's clarity. However, this is a breaking change that requires updates to all implementations of the Render type.

Let's verify all implementations are updated:

✅ Verification successful

The previous pattern was too specific. Let's search more broadly to find render implementations and analyze their usage patterns.


This will help us find the actual implementations using the Render type and verify if there are any usages of the old parameters that need to be updated.


We need to verify the type definition and its usage to understand the impact of this change.


Breaking change is correctly implemented and documented

The type definition shows that the Render type has been properly updated to use a single zooming parameter, and looking at the codebase usage in previous search results (particularly in Grid.tsx and DynamicGroupsFlashlightWrapper.tsx), the implementations have already been updated to match this new signature. The change improves the API by:

  1. Reducing boolean parameters from 2 to 1
  2. Using more semantic naming with 'zooming'
  3. Maintaining consistent implementation across the codebase
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Find all implementations of the Render type
ast-grep --pattern 'render: ($id, $element, $dimensions, $param) => {
  $$$
}'

Length of output: 80


Script:

#!/bin/bash
# Let's try a broader search for Render type implementations
rg -l "render.*:" --type ts

# Then look for actual implementations
rg "render.*:" -A 5 --type ts

Length of output: 27689


Script:

#!/bin/bash
# Let's search for implementations that use the Render type from spotlight package
rg -B 2 -A 2 "import.*Render.*from.*spotlight" --type ts

# Also search for any usage of soft/disable parameters in render functions
rg "render.*\(.*soft.*disable.*\)" --type ts

Length of output: 105


Script:

#!/bin/bash
# Let's check the actual type definition and its usage in the spotlight package
cat app/packages/spotlight/src/types.ts

# Search for any files importing from spotlight/types
rg -l "from.*spotlight/.*types" --type ts

Length of output: 1511

app/packages/core/src/components/Grid/Grid.tsx (2)

73-75: Simplified looker attachment logic

Good simplification of the attachment logic. The direct attachment call improves code readability.


85-87: Updated fast scrolling condition

The condition for skipping looker creation during fast scrolling is now more semantic with the zooming parameter.

Let's verify the scrolling behavior implementation:

✅ Verification successful

Fast scrolling optimization verified

The zooming parameter is correctly implemented as part of a comprehensive scrolling optimization system. The code shows:

  • The zooming state is managed in createScrollReader.ts where it's properly toggled based on scroll speed
  • The Grid component correctly uses this parameter to skip looker creation during fast scrolling
  • The implementation is consistent across the codebase with proper handling in both Spotlight and Flashlight packages
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Find other zooming-related implementations
ast-grep --pattern 'if ($condition) {
  // $$$scrolling$$$
  return
}'

Length of output: 72


Script:

#!/bin/bash
# Let's search for zooming-related code with more context
rg -U "zooming" -A 5 -B 5

Length of output: 26075

app/packages/spotlight/src/row.ts (1)

151-151: LGTM: Simplified method signature

The parameter changes streamline the visibility handling by removing redundant parameters and introducing a more focused zooming state.

app/packages/spotlight/src/section.ts (2)

49-49: LGTM: Added item tracking for deduplication

The #itemIds Set efficiently tracks unique items, preventing duplicates in the grid.


304-306: LGTM: Efficient duplicate prevention

The filter ensures each item is processed only once by checking against the #itemIds Set, improving performance and preventing duplicate renders.

@@ -171,7 +163,7 @@ export default class Row<K, V> {

for (const { element, item } of this.#row) {
const width = item.aspectRatio * this.height;
config.render(item.id, element, [width, this.height], soft, hidden);
config.render(item.id, element, [width, this.height], zooming);
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

SpotlightConfig interface needs zooming parameter in render method

The SpotlightConfig interface in app/packages/spotlight/src/types.ts needs to be updated to include the zooming parameter in its render method signature, as it's being used in row.ts.

  • app/packages/spotlight/src/types.ts: Add zooming parameter to the render method definition in SpotlightConfig interface
🔗 Analysis chain

Verify render method compatibility

The render method now uses the zooming parameter. Ensure all implementations of SpotlightConfig.render handle this parameter correctly.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for all render method implementations to verify they handle the zooming parameter
ast-grep --pattern 'render($_, $_, $_, $_) {
  $$$
}'

Length of output: 55


Script:

#!/bin/bash
# Search for render method implementations in TypeScript/JavaScript files
rg "render\s*\([^)]*\)" -t ts -t js -A 5

# Also search for SpotlightConfig interface/type definition
rg "SpotlightConfig" -t ts -t js -A 5

Length of output: 16575

@@ -225,7 +218,7 @@ export default class Section<K, V> {

updateItems(updater: (id: ID) => void) {
for (const row of this.#shown) row.updateItems(updater);
for (const row of this.#rows) !this.#shown.has(row) && this.#dirty.add(row);
for (const row of this.#rows) !this.#shown.has(row);
Copy link
Contributor

@coderabbitai coderabbitai bot Dec 2, 2024

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix incomplete condition in updateItems method

The current line has no effect as it's just evaluating a condition without using the result. This appears to be a bug.

-    for (const row of this.#rows) !this.#shown.has(row);
+    for (const row of this.#rows) {
+      if (!this.#shown.has(row)) {
+        row.updateItems(updater);
+      }
+    }

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 appears that this line has no effect

Copy link
Contributor

Choose a reason for hiding this comment

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

@tom-vx51, glad we're in agreement.

@benjaminpkane benjaminpkane self-assigned this Dec 2, 2024
@benjaminpkane benjaminpkane added bug Bug fixes app Issues related to App features labels Dec 2, 2024
Copy link
Contributor

@tom-vx51 tom-vx51 left a comment

Choose a reason for hiding this comment

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

Noting that this change technically breaks the render interface used by data lens, but the logic remains unchanged since undefined will be treated as falsey. I'll take the action item to clean it up.

@@ -225,7 +218,7 @@ export default class Section<K, V> {

updateItems(updater: (id: ID) => void) {
for (const row of this.#shown) row.updateItems(updater);
for (const row of this.#rows) !this.#shown.has(row) && this.#dirty.add(row);
for (const row of this.#rows) !this.#shown.has(row);
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 appears that this line has no effect

for (const i of rowItems) {
this.#itemIds.add(i.id.description);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

should these be getting removed when the row is destroyed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, good point

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, rows are only deleted/destroyed when the section is deleted/destroyed. Will leave a comment

Copy link
Contributor

@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

🧹 Outside diff range and nitpick comments (1)
app/packages/spotlight/src/section.ts (1)

49-49: LGTM! Good architectural improvement for item tracking

The introduction of #itemIds as a Set to track unique items is a clean approach that should help prevent duplicate items and improve performance.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 949fc2c and f6faba4.

📒 Files selected for processing (1)
  • app/packages/spotlight/src/section.ts (4 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
app/packages/spotlight/src/section.ts (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

🔇 Additional comments (2)
app/packages/spotlight/src/section.ts (2)

450-453: ⚠️ Potential issue

Consider cleanup of tracked items

Items are added to #itemIds but never removed. This could lead to memory leaks as the set grows indefinitely. Consider clearing or removing specific items when rows are destroyed.

Apply this diff to add cleanup:

 destroy(destroyItems = false) {
   this.#section.remove();
-  for (const row of this.#rows) row.destroy(destroyItems);
+  for (const row of this.#rows) {
+    for (const item of row.items) {
+      this.#itemIds.delete(item.id.description);
+    }
+    row.destroy(destroyItems);
+  }
   this.#rows = [];
 }

303-305: Verify the use of id.description for item tracking

While filtering duplicates is good, using id.description as the unique identifier seems unusual. Should we be using a more direct identifier property?

✅ Verification successful

Using id.description is the correct pattern for item tracking

The usage of id.description as a unique identifier is consistent throughout the codebase:

  • Used for tracking selected items in useExpandSample
  • Used as keys in various stores and maps in Grid components
  • Used for item identification in spotlight components
  • Used consistently for tracking and deduplication purposes
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other usages of id.description to verify this pattern
rg -A 2 "id\.description" --type ts

Length of output: 3742

@benjaminpkane benjaminpkane merged commit e3e7cba into release/v1.1.0 Dec 2, 2024
11 checks passed
@benjaminpkane benjaminpkane deleted the bugfix/looker-cache branch December 2, 2024 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
app Issues related to App features bug Bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants