-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[QueryCache] Use RamUsageEstimator instead of defaulting to 1024 bytes for non-accountable query size #15124
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
base: main
Are you sure you want to change the base?
Changes from all commits
2dd3b8b
c60d477
dd86863
2fe6e55
436b95e
9e60d4f
f42f927
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,10 @@ public BooleanQuery build() { | |
private final List<BooleanClause> clauses; // used for toString() and getClauses() | ||
// WARNING: Do not let clauseSets escape from this class as it breaks immutability: | ||
private final Map<Occur, Collection<Query>> clauseSets; // used for equals/hashCode | ||
private final List<Query> mustClauseQueries; | ||
private final List<Query> mustNotClauseQueries; | ||
private final List<Query> filterClauseQueries; | ||
private final List<Query> shouldClauseQueries; | ||
|
||
private BooleanQuery(int minimumNumberShouldMatch, BooleanClause[] clauses) { | ||
this.minimumNumberShouldMatch = minimumNumberShouldMatch; | ||
|
@@ -137,9 +141,36 @@ private BooleanQuery(int minimumNumberShouldMatch, BooleanClause[] clauses) { | |
// but not for FILTER and MUST_NOT | ||
clauseSets.put(Occur.FILTER, new HashSet<>()); | ||
clauseSets.put(Occur.MUST_NOT, new HashSet<>()); | ||
// We store the queries per clauses in a list beforehand. As otherwise during repeated visit() | ||
// calls(like in QueryCache), we need to iterate over clauseSets.keySet() which allocates | ||
// HashMap$HashSetIterator.<init> | ||
// for every call causing some performance impact | ||
|
||
List<Query> mustClauseQueries = new ArrayList<>(); | ||
List<Query> mustNotClauseQueries = new ArrayList<>(); | ||
List<Query> filterClauseQueries = new ArrayList<>(); | ||
List<Query> shouldClauseQueries = new ArrayList<>(); | ||
for (BooleanClause clause : clauses) { | ||
switch (clause.occur()) { | ||
case MUST: | ||
mustClauseQueries.add(clause.query()); | ||
break; | ||
case FILTER: | ||
filterClauseQueries.add(clause.query()); | ||
break; | ||
case SHOULD: | ||
shouldClauseQueries.add(clause.query()); | ||
break; | ||
case MUST_NOT: | ||
mustNotClauseQueries.add(clause.query()); | ||
break; | ||
} | ||
clauseSets.get(clause.occur()).add(clause.query()); | ||
} | ||
this.mustClauseQueries = List.copyOf(mustClauseQueries); | ||
this.mustNotClauseQueries = List.copyOf(mustNotClauseQueries); | ||
this.filterClauseQueries = List.copyOf(filterClauseQueries); | ||
this.shouldClauseQueries = List.copyOf(shouldClauseQueries); | ||
} | ||
|
||
/** Gets the minimum number of the optional BooleanClauses which must be satisfied. */ | ||
|
@@ -649,15 +680,23 @@ public Query rewrite(IndexSearcher indexSearcher) throws IOException { | |
@Override | ||
public void visit(QueryVisitor visitor) { | ||
QueryVisitor sub = visitor.getSubVisitor(Occur.MUST, this); | ||
for (BooleanClause.Occur occur : clauseSets.keySet()) { | ||
for (Occur occur : Occur.cachedValues()) { | ||
Comment on lines
-652
to
+683
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the cost is truly creating the iterator, maybe we can switch to something that still uses the key set, like It does seem weird to iterate the keys only to immediately use them to rehash and get the values again. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So even doing With this change, the 100% write ops/s dropped to 2M compared to the change as part of this PR (which was 3M)
In my case, because I am frequently calling ![]() |
||
if (clauseSets.get(occur).size() > 0) { | ||
if (occur == Occur.MUST) { | ||
for (Query q : clauseSets.get(occur)) { | ||
for (Query q : mustClauseQueries) { | ||
q.visit(sub); | ||
} | ||
} else { | ||
QueryVisitor v = sub.getSubVisitor(occur, this); | ||
for (Query q : clauseSets.get(occur)) { | ||
List<Query> queryList; | ||
queryList = | ||
switch (occur) { | ||
case FILTER -> filterClauseQueries; | ||
case SHOULD -> shouldClauseQueries; | ||
case MUST_NOT -> mustNotClauseQueries; | ||
case MUST -> mustClauseQueries; | ||
}; | ||
for (Query q : queryList) { | ||
q.visit(v); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for digging into this performance impact.
How much does this gain us in the "normal" case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure on this. Might have to run some existing benchmark to get an idea.