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

In-memory thread cache #19

Open
novacrazy opened this issue Jul 1, 2023 · 1 comment
Open

In-memory thread cache #19

novacrazy opened this issue Jul 1, 2023 · 1 comment
Labels
enhancement New feature or request performance

Comments

@novacrazy
Copy link
Member

novacrazy commented Jul 1, 2023

Due to the performance limitations of recursive queries, it would be very useful in the future to create an in-memory thread structure to cache the last/top N messages to a certain depth, for query-less rapid access. Treat it like an LRU cache based on room/thread accesses, so the most in-demand rooms/threads can avoid a large chunk of work. For the purpose of this, an entire room is a top-level thread. This has the added benefit of the listing of posts in a user-board room also being cached.

This is partially inspired by https://slack.engineering/real-time-messaging/

However, I want to extend this to user-board rooms and posts, with the full tree structure. As of writing this I'm unsure what the tree should look like, as it might need to support varying traversal conditions and filtering.

type MsgId = Snowflake;
type RoomOrParentId = Snowflake;

// 16 bytes
struct CachedMessage {
    msg_id: MsgId,
    parent_id: RoomOrParentId,
}

struct Cache {
    threads: scc::HashCache<RoomOrParentId, Tree<CachedMessage>>,
    messages: scc::HashMap<MsgId, Message>,
}

perhaps key by (RoomOrParentId, SortMethod) where SortMethod could be Top/New/etc., so those get their own sorted trees.

Furthermore, we'll need to track some kind of threshold for cache insertion, such that we aren't constantly thrashing it with small posts. The cache should only be used when the time spent doing repeated work becomes significant for a single post.

@novacrazy novacrazy added enhancement New feature or request performance labels Jul 1, 2023
@novacrazy
Copy link
Member Author

Forgot to mention that per-message access is so Messages can be updated without invalidating the entire tree. Things such as reactions and voting will cause frequent updates that don't change the layout of the tree. Perhaps the exception to that would be if it changes the sort order of siblings in the tree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request performance
Projects
None yet
Development

No branches or pull requests

1 participant