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

Perf: Cache inventory calculation on dashboard #4939

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

coalest
Copy link
Collaborator

@coalest coalest commented Jan 21, 2025

Doesn't resolve any issue.

Description

Noticed a number of duplicate queries in the dashboard, so here's a PR to remove them.

Type of change

  • Internal (code refactor)

How Has This Been Tested?

Test suite and some manually testing locally.

Screenshots

These are just on my local machine with the seed database so YMMV

Before:
~70 ms response time
32 queries (11 cached)
Screenshot from 2025-01-21 17-00-31

After:
~56 ms response time
27 queries (6 cached)
Screenshot from 2025-01-21 17-00-41

Copy link
Collaborator

@dorner dorner left a comment

Choose a reason for hiding this comment

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

Teeny tiny question.

end

private

attr_reader :current_organization
attr_reader :current_organization, :inventory
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need this to be readable from outside the class?

Copy link
Collaborator Author

@coalest coalest Jan 22, 2025

Choose a reason for hiding this comment

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

It isn't readable from outside the class because it's defined below the private keyword:

image

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then why do you need an attr_reader declaration at all?

Copy link
Collaborator Author

@coalest coalest Jan 24, 2025

Choose a reason for hiding this comment

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

I'm using it on line 30 of num_locations_with_inventory. It isn't necessary as I could just access the instance variable directly. I chose this route just to follow the current pattern in this class (similar to current_organization which also doesn't really need an attr_reader).

My plan is to submit another PR refactoring OrganizationStats to use a call class method instead, as well as improve it's performance in terms of queries. But I figured this PR is just about refactoring to calculate the inventory once (which I imagine could provide a meaningful performance improvement in prod in some situations), so the changes here are as minimal as possible.

But if you prefer I can totally get rid of the attr_reader for inventory and access the @inventory instance variable directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants