Repository navigation
fix: memoize field :if/:unless conditions - #608
Merged
Merged
Conversation
Contributor
|
Thanks for pushing this up @Hamdy! The improvements look promising. I left a few comments, but outside of that, I’d lean towards splitting out the additional benchmarks from the fix itself (given that it represents only a small fraction of the code here). We’ve had previous discussions around reworking the benchmark tooling previously, so I’d be happy to work with you on that separately from this change! |
Signed-off-by: hamdy <hamdy.abdelbadeea@procore.com> Co-authored-by: Cursor <cursoragent@cursor.com>
Hamdy
force-pushed
the
hamdy/fix-memoization
branch
from
October 23, 2026 09:09
2a24d75 to
885a48c
Compare
2 of 3 tasks
3 tasks done
Draft
3 tasks done
lessthanjacob
approved these changes
Oct 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist:
Summary
Each field's
:if/:unlesslookup was meant to be computed once, but it ran again for every object in every render.Field#if_callableand#unless_callablecache their result with@ivar ||= callable_from(...). When a field has no condition, which is the common case,callable_fromreturnsfalse.||=never cachesfalse, so both lookups ran again for every field of every object, and each one readBlueprinter.configuration.The fix swaps
||=for adefined?guard, so afalseresult is cached too. It's two methods inlib/blueprinter/field.rband adds no allocations. The result is about 40% less time per rendered object.Benchmarks
I measured
mainagainstmainplus this fix, using the benchmark suite from #614 (split out of this PR per review). Both ran on the same machine with Ruby 3.3.9, alternating over 5 rounds each. The numbers are medians.Iterations per second (higher is better)
Renders per second (higher is better)
Microseconds per object (lower is better)
Big-O render time, ms (lower is better)
Allocations per render (lower is better)
Field#callable_fromcalls per render (lower is better)The last table explains the speedup. The lookup now runs once per field per render (6 fields ×
:if/:unless= 12) instead of once per field per object.Behaviour change: please review
If a field has no
:if/:unlessof its own, it now captures the globalconfig.if/config.unlesson its first render. A global condition set after a blueprint has already rendered is ignored, where before it took effect. Setting it before the first render works exactly as before. Thesnapshots the global condition on first usespec pins this.Tests
spec/units/field_spec.rbadds coverage forField#skip?: field-level procs, symbols, global config, and precedence. Two examples fail onmainand guard the regression:does not read global configuration again after the first callandsnapshots the global condition on first use.