Reuse query objects and skip re-parsing parsed tag lists - #91
Merged
igor-alexandrov merged 1 commit intoAug 18, 2026
Merged
Conversation
Every tagged_with call built a fresh QueryBuilder and TagsQuery even though both are stateless, and GenericParser rebuilt a TagList it had itself produced. All of that work has the same result on every call, so it was pure per-query allocation overhead. QueryBuilder now exposes a shared frozen instance with the two TagsQuery strategies (all/any) prebuilt, and the parser returns TagList input as-is. Parsing an already-parsed list drops from ~0.7us to ~0.05us; relation building gets ~2-4% cheaper. Behavior is unchanged except that a hand-built TagList containing empty strings is no longer filtered — Metka itself never produces one. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This was referenced Aug 18, 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.
Why
The benchmark suite showed Metka statistically tied with acts-as-taggable-array-on on querying — both emit identical SQL against the same GIN index — but Metka's Ruby-side relation building carried avoidable per-call work: a fresh
QueryBuilderandTagsQueryon everytagged_withcall despite both being stateless, andGenericParserrebuildingTagListinput that is its own, already-parsed output.What
QueryBuilderexposes a shared frozen.instance; the twoTagsQuerystrategies (:all/:any) are prebuilt in a frozenSTRATEGIESconstant..newremains public, so direct construction (as in the unit tests) still works.tagged_withuses the shared instance.GenericParser#callreturns aTagListargument as-is instead of re-parsing it.Numbers
Micro-benchmark of relation construction (no DB round trip), before → after:
parser.call(TagList)tagged_with(...).to_sqlBehavior note
A hand-built
TagListcontaining empty strings is no longer filtered on re-parse. Metka itself never produces such a list, so no gem path or test is affected.Tests
Full suite: 116 runs, 311 assertions, 0 failures, 0 errors.
🤖 Generated with Claude Code