Skip to content

Adding thousands of views to a client is extremely slow #1286

Open
@abrahammurciano

Description

@abrahammurciano

Summary

Registering thousands of persistent views on startup takes O(n^2) time because ViewStore.__verify_integrity gets called each time one is added, and that is O(n).

Reproduction Steps

My bot is a polling bot. On startup I load all the polls from the database, then register a persistent view for each poll, so the bot can continue to respond to interactions on old polls.

Minimal Reproducible Code

class Paul(InteractionBot):
    # ...
    async def __load_polls(self) -> None:
        async for poll in Poll.fetch_polls():
            self.add_view(PollView(self, poll))

Expected Results

I expect Client.add_view to run in O(1) time, causing my bot to load in linear time.

Actual Results

My bot has grown to have about 22K polls. Now it takes about 30 minutes to start up, though I notice that at the beginning it loads polls quite fast, but as it progresses it gets slower and slower.

This is because Client.add_view runs in O(n) time, since each time it calls ViewStore.__verify_integrity, which iterates over all previously added views.

Intents

None

System Information

- Python v3.13.0-final
- disnake v2.10.1-final
    - disnake importlib.metadata: v2.10.1
- aiohttp v3.11.12
- system info: Linux 5.15.0-88-generic #98-Ubuntu SMP Mon Oct 2 15:18:56 UTC 2023 x86_64

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

Additional Context

I think a possible solution would be to have a method Client.bulk_add_views which would propagate to a new method ViewStore.add_views which would only call ViewStore.__verify_integrity once.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions