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

feat(gno): enhance reactions retrievals #266

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

MikaelVallenet
Copy link
Member

⚠️ Description is AI generated and could contains some inconsistencies

PR: Optimize Reaction System Performance

Problem Statement

Our current implementation of the reaction system suffers from several performance issues:

  1. Inefficient Data Storage: We store user reactions as string arrays, requiring O(n) operations to check if a user has voted.
  2. Redundant String Parsing: Each lookup requires parsing the composite keys.
  3. Excessive Memory Usage: Storing full user lists for each reaction consumes unnecessary memory.
  4. Scalability Concerns: As user engagement grows, these operations become increasingly expensive.

Solution

This PR refactors the reaction system to leverage two distinct AVL trees more efficiently:

  1. reactions tree: Stores aggregate reaction counts per post/icon combination

    • Key format: localPostId:icon
    • Value: Simple integer count
  2. userReactions tree: Tracks individual user reactions

    • Key format: localPostId:icon:userAddress
    • Value: Empty struct (serving as a set marker)

Implementation Details

The refactoring focuses on two key functions:

  1. ReactPost: Now handles toggling reactions by manipulating both trees atomically:

    • Adds/removes individual user reaction markers
    • Increments/decrements aggregate counts
    • Removes count entries that reach zero
  2. GetPostReactions: Optimized to:

    • Efficiently iterate through reactions for a post
    • Directly access reaction counts
    • Perform O(1) lookups to check if a user has voted

Performance Benefits

  • Faster Lookups: O(1) operations to check if a user has reacted (vs previous O(n))
  • Reduced Memory: Storing integer counts instead of user arrays
  • No String Splitting: Direct key access instead of parsing composite keys
  • Better Scaling: Performance remains consistent regardless of reaction count

Tradeoffs

  • Increased Implementation Complexity: Managing two trees requires careful synchronization
  • Storage Overhead: Some duplication in keys across both trees
  • Atomic Operations: Updates must modify both trees to maintain consistency

Copy link

vercel bot commented Mar 14, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
zenao ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 14, 2025 4:04pm

@MikaelVallenet MikaelVallenet self-assigned this Mar 14, 2025
@MikaelVallenet MikaelVallenet added the enhancement New feature or request label Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant