Skip to content

Comments

[IMPROVED] Single subject in FilterSubjects field#7856

Merged
neilalexander merged 1 commit intomainfrom
maurice/single-consumer-filter
Feb 20, 2026
Merged

[IMPROVED] Single subject in FilterSubjects field#7856
neilalexander merged 1 commit intomainfrom
maurice/single-consumer-filter

Conversation

@MauriceVanVeen
Copy link
Member

We would populate o.filters even if FilterSubjects has a single entry, which would make us use LoadNextMsgMulti versus LoadNextMsg. o.updateConfig would already correctly populate o.subjf and o.filters in the same way.

Resolves #7852

Signed-off-by: Maurice van Veen github@mauricevanveen.com

@MauriceVanVeen MauriceVanVeen requested a review from a team as a code owner February 19, 2026 16:46
Copy link
Member

@neilalexander neilalexander left a comment

Choose a reason for hiding this comment

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

Would it better to gate on o.filters.Count() at the callsite of LoadNextMsg/LoadNextMsgMulti instead? That way we get the same optimisation for both SubjectFilter = "foo" and SubjectFilters = []string{"foo"}.

@MauriceVanVeen
Copy link
Member Author

Would it better to gate on o.filters.Count() at the callsite of LoadNextMsg/LoadNextMsgMulti instead? That way we get the same optimisation for both SubjectFilter = "foo" and SubjectFilters = []string{"foo"}.

In essence setting o.filters to a sublist is what makes us use LoadNextMsgMulti, as it should only be populated if there are multiple filters. It was a bug that we did do this properly in o.updateConfig but not when creating the consumer initially.

All callsites of LoadNextMsg/Multi (o.getNextMsg and o.checkStateForInterestStream), already use nil-checks on o.filters and fallback to o.subjf. So I'm thinking the current approach is already both consistent with o.updateConfig and effective at not using LoadNextMsgMulti when there's only a single subject in FilterSubjects.

// If we have multiple filter subjects, create a sublist which we will use
// in calling store.LoadNextMsgMulti.
if len(o.cfg.FilterSubjects) > 0 {
if len(o.subjf) == 1 {
Copy link
Member

Choose a reason for hiding this comment

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

Should this be <= 1?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, also changed the else if to just an else.

Signed-off-by: Maurice van Veen <github@mauricevanveen.com>
@MauriceVanVeen MauriceVanVeen force-pushed the maurice/single-consumer-filter branch from aa945f1 to 263ef9b Compare February 20, 2026 10:37
Copy link
Member

@neilalexander neilalexander left a comment

Choose a reason for hiding this comment

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

LGTM!

@neilalexander neilalexander merged commit 66e9bbc into main Feb 20, 2026
89 of 93 checks passed
@neilalexander neilalexander deleted the maurice/single-consumer-filter branch February 20, 2026 11:41
wallyqs pushed a commit to wallyqs/nats-server that referenced this pull request Feb 20, 2026
Review of the performance fix that prevents single-entry FilterSubjects
from populating o.filters, which would incorrectly route to the slower
LoadNextMsgMulti code path instead of LoadNextMsg.

https://claude.ai/code/session_016j2QjHeW4iRrcc6obkni4A
neilalexander added a commit that referenced this pull request Feb 20, 2026
Includes the following:

- #7839
- #7843
- #7824
- #7826
- #7845
- #7844
- #7840
- #7827
- #7846
- #7848
- #7849
- #7855
- #7850
- #7857
- #7856

Signed-off-by: Neil Twigg <neil@nats.io>
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.

JetStream consumer subject filtering causes performance degradation even when filter matches all subjects

2 participants