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

Rearrange conditions to short-circuit before database queries #11197

Merged
merged 1 commit into from
Sep 5, 2024

Conversation

aduth
Copy link
Member

@aduth aduth commented Sep 4, 2024

🛠 Summary of changes

Rearranges a handful of && conditions to move trivial-cost checks to earlier in the condition, leveraging boolean short-circuiting to avoid more costly checks.

These were discovered by searching && in app/**/*.rb files and manually reviewing each instance.

📜 Testing Plan

Verify build passes.

It's not expected this should have a meaningful impact, but there may be scenarios where a condition was already written with short-circuiting in mind, where the latter part of a condition relies on a dependency established in an earlier part. Those have been considered as part of these changes, but a secondary review is appreciated.

changelog: Internal, Performance, Optimize code conditions to avoid unnecessary database queries
Copy link
Member

@n1zyy n1zyy left a comment

Choose a reason for hiding this comment

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

👏 Nice. I think it's easy to get bogged down in changing logic and not consider the impact of database queries.

A couple thoughts...

  1. Have we looked into using bullet to try to find N+1 queries and such? Not directly related, just on the topic of "I didn't realize we were doing that bad database thing..."
  2. What about building out a little style guide / best practices doc as we go? This might be a neat thing to note. We had this when I worked on Caseflow, for example.

Both are just ideas for potential followups.

@zachmargolis
Copy link
Contributor

  1. Have we looked into using bullet to try to find N+1 queries and such? Not directly related, just on the topic of "I didn't realize we were doing that bad database thing..."

We do use Bullet, it's just disabled by default because of performance implications

gem 'bullet', '~> 7.0'

@aduth
Copy link
Member Author

aduth commented Sep 5, 2024

We do use Bullet, it's just disabled by default because of performance implications

Not sure what you mean by "disabled by default", but it's caught issues for me in the past (recent example).

@aduth
Copy link
Member Author

aduth commented Sep 5, 2024

2. What about building out a little style guide / best practices doc as we go? This might be a neat thing to note. We had this when I worked on Caseflow, for example.

I like this idea, though I could also see a counter-argument that these sorts of optimizations aren't really specific to our project, and more just best practices for any programming language that has boolean short-circuiting.

We could maybe have a "Optimizations" section in the "Back-end Architecture" doc, or maybe a separate performance-specific document?

@aduth aduth merged commit 5b4a479 into main Sep 5, 2024
2 checks passed
@aduth aduth deleted the aduth-short-circuit-queries branch September 5, 2024 12:40
@n1zyy
Copy link
Member

n1zyy commented Sep 5, 2024

We do use Bullet, it's just disabled by default because of performance implications

gem 'bullet', '~> 7.0'

D'oh! I took a look at the Gemfile but didn't make it down to the :development, :test group before concluding it wasn't there. 🤦

I like this idea, though I could also see a counter-argument that these sorts of optimizations aren't really specific to our project, and more just best practices for any programming language that has boolean short-circuiting.

It is definitely not unique to the project. My thinking was to just document best practices as a reminder. That said...

We could maybe have a "Optimizations" section in the "Back-end Architecture" doc, or maybe a separate performance-specific document?

I consistently forget this document exists. 😬 So I'm realizing that my "we should document this as a best practice!" thought might really mean "we should put this in a document that I rarely look at," and maybe that's not worthwhile.

@aduth
Copy link
Member Author

aduth commented Sep 5, 2024

We could maybe have a "Optimizations" section in the "Back-end Architecture" doc, or maybe a separate performance-specific document?

I consistently forget this document exists. 😬 So I'm realizing that my "we should document this as a best practice!" thought might really mean "we should put this in a document that I rarely look at," and maybe that's not worthwhile.

True, though I think it can still be a nice thing to point to as a reference when reviewing a pull request if the reviewer is aware of the document. For example, I frequently point people to documentation they may not know exists, which hopefully indirectly increases awareness of said documentation.

I think discoverability of this document is a problem in its own right, but I'm not really sure how much more we can highlight beyond what we've done to make it one of the few top links in the top-level README 🤷

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

Successfully merging this pull request may close these issues.

3 participants