Skip to content

Commit d87dfed

Browse files
committed
Optimize the code a bit: use one loop instead of two loops
1 parent fb6d683 commit d87dfed

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/dsql/BoolNodes.cpp

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,24 +1341,11 @@ BoolExprNode* InListBoolNode::copy(thread_db* tdbb, NodeCopier& copier) const
13411341

13421342
BoolExprNode* InListBoolNode::decompose(CompilerScratch* csb)
13431343
{
1344-
// Collect list items depending on record streams
1345-
1346-
HalfStaticArray<ValueExprNode*, OPT_STATIC_ITEMS> splitItems;
1347-
1348-
for (auto item : list->items)
1349-
{
1350-
SortedStreamList streams;
1351-
item->collectStreams(streams);
1352-
1353-
if (streams.hasData())
1354-
splitItems.add(item);
1355-
}
1356-
1357-
if (splitItems.isEmpty())
1358-
return nullptr;
1359-
1360-
// Decompose expression: <arg> IN (<item1>, <item2>, <item3>, <item4> ...)
1361-
// into: <arg> IN (<item1>, <item2>, ...) OR <arg> = <item3> OR <arg> = <item4> ...
1344+
// Search for list items depending on record streams.
1345+
// If found, decompose expression:
1346+
// <arg> IN (<item1>, <item2>, <item3>, <item4> ...)
1347+
// into:
1348+
// <arg> IN (<item1>, <item2>, ...) OR <arg> = <item3> OR <arg> = <item4> ...
13621349
// where the ORed booleans are known to be stream-based (i.e. contain fields inside)
13631350
// and thus could use an index, if possible.
13641351
//
@@ -1371,24 +1358,34 @@ BoolExprNode* InListBoolNode::decompose(CompilerScratch* csb)
13711358
auto& pool = csb->csb_pool;
13721359
BoolExprNode* boolNode = nullptr;
13731360

1374-
for (const auto item : splitItems)
1361+
for (auto iter = list->items.begin(); iter != list->items.end();)
13751362
{
1376-
list->items.findAndRemove(item);
1363+
ValueExprNode* const item = *iter;
1364+
1365+
SortedStreamList streams;
1366+
item->collectStreams(streams);
1367+
1368+
if (streams.isEmpty())
1369+
{
1370+
iter++;
1371+
continue;
1372+
}
1373+
1374+
list->items.remove(iter);
13771375

1378-
const auto cmpNode =
1379-
FB_NEW_POOL(pool) ComparativeBoolNode(pool, blr_eql, arg, item);
1376+
const auto cmpNode = FB_NEW_POOL(pool) ComparativeBoolNode(pool, blr_eql, arg, item);
13801377

13811378
if (boolNode)
13821379
boolNode = FB_NEW_POOL(pool) BinaryBoolNode(pool, blr_or, boolNode, cmpNode);
13831380
else
13841381
boolNode = cmpNode;
13851382
}
13861383

1387-
if (const auto count = list->items.getCount())
1384+
if (boolNode && list->items.hasData())
13881385
{
13891386
BoolExprNode* priorNode = this;
13901387

1391-
if (count == 1)
1388+
if (list->items.getCount() == 1)
13921389
{
13931390
// Convert A IN (B) into A = B
13941391
priorNode = FB_NEW_POOL(pool) ComparativeBoolNode(pool, blr_eql, arg, list->items.front());

0 commit comments

Comments
 (0)