From 4ccf75fa47a8400b5e761dfcaebd8bb7facc5c08 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 23:14:01 +0000 Subject: [PATCH] feat(tidy): add fairmq-performance-channel-lookup-in-loop - Report GetChannel() and GetChannels() calls in an override of ConditionalRun() or in a loop in an override of Run(). Each call looks up the channel name in a map. - Skip the short form Send(msg, "name"): it is the usual style. Co-Authored-By: Claude Opus 5.5 --- docs/src/content/docs/tools/fairmq-tidy.mdx | 7 ++ fairmq/tidy/CMakeLists.txt | 2 + fairmq/tidy/ChannelLookupInLoopCheck.cxx | 91 +++++++++++++++++++ fairmq/tidy/ChannelLookupInLoopCheck.h | 39 ++++++++ fairmq/tidy/Module.cxx | 2 + ...rmq-performance-channel-lookup-in-loop.cxx | 38 ++++++++ 6 files changed, 179 insertions(+) create mode 100644 fairmq/tidy/ChannelLookupInLoopCheck.cxx create mode 100644 fairmq/tidy/ChannelLookupInLoopCheck.h create mode 100644 test/tidy/fairmq-performance-channel-lookup-in-loop.cxx diff --git a/docs/src/content/docs/tools/fairmq-tidy.mdx b/docs/src/content/docs/tools/fairmq-tidy.mdx index 3b20f7d3..53403463 100644 --- a/docs/src/content/docs/tools/fairmq-tidy.mdx +++ b/docs/src/content/docs/tools/fairmq-tidy.mdx @@ -44,6 +44,7 @@ The `fairmq-*` checks are a clang-tidy module ([Module.cxx](/source/fairmq/tidy/ | `fairmq-modernize-parts-api` | Calls of `Parts::AtRef()` and of `Parts::AddPart(Message*)` | `At()` with `->` or `*`, and `MessagePtr(p)` | | `fairmq-modernize-progoptions-accessors` | Calls of the deprecated accessors `ProgOptions::GetValue()`, `GetStringValue()` and `SetValue()` | `GetProperty()`, `GetPropertyAsString()` and `SetProperty()` | | `fairmq-modernize-run-device` | A `getDevice()` for `` that returns a raw pointer | ``, `std::unique_ptr` and `std::make_unique` | +| `fairmq-performance-channel-lookup-in-loop` | Calls of `GetChannel()` or `GetChannels()` in `ConditionalRun()` or in a loop in `Run()` | No fix | | `fairmq-readability-transfer-code` | Comparisons of the result of `Send()` or `Receive()` with `-1`, `-2` or `-3` | `static_cast(fair::mq::TransferCode::...)` | The checks report a name in a macro, but they do not fix it. Like all clang-tidy checks, they show findings in headers only if the header matches `--header-filter=`. @@ -194,6 +195,12 @@ A device leaves the state Running only when `Run()` returns. An endless loop in Use `while (!NewStatePending())` as the loop condition. +### fairmq-performance-channel-lookup-in-loop + +`GetChannel("name")` looks up the name in a map at each call. The check reports a call that runs one time for each iteration: in an override of `ConditionalRun()`, or in a loop in an override of `Run()`. Look up the channel one time, for example in `InitTask()`, and keep a reference. + +The check does not report `Send(msg, "name")` and `Receive(msg, "name")`. They also look up the channel, but they are the usual short form. + ## Build fairmq-tidy Set the CMake option `BUILD_TIDY_TOOL` (default `OFF`) when you configure FairMQ: diff --git a/fairmq/tidy/CMakeLists.txt b/fairmq/tidy/CMakeLists.txt index 984cc18e..86632553 100644 --- a/fairmq/tidy/CMakeLists.txt +++ b/fairmq/tidy/CMakeLists.txt @@ -32,6 +32,8 @@ endif() # The fairmq-* checks as a clang-tidy module. add_library(fairmq-tidy-module OBJECT + ChannelLookupInLoopCheck.cxx + ChannelLookupInLoopCheck.h ChannelsAccessCheck.cxx ChannelsAccessCheck.h DeprecatedFunctionsCheck.cxx diff --git a/fairmq/tidy/ChannelLookupInLoopCheck.cxx b/fairmq/tidy/ChannelLookupInLoopCheck.cxx new file mode 100644 index 00000000..cc018f27 --- /dev/null +++ b/fairmq/tidy/ChannelLookupInLoopCheck.cxx @@ -0,0 +1,91 @@ +/******************************************************************************** + * Copyright (C) 2026 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * * + * This software is distributed under the terms of the * + * GNU Lesser General Public Licence (LGPL) version 3, * + * copied verbatim in the file "LICENSE" * + ********************************************************************************/ + + +#include + +#include +#include +#include +#include + +namespace fair::mq::tidy { + +using namespace clang; +using namespace clang::ast_matchers; + +namespace { + +/// True if `call` runs once per iteration of a loop: it is in the condition, +/// the increment or the body of a loop, and not in a lambda in between. +bool inLoop(Expr const& call, ASTContext& ctx) +{ + DynTypedNode node = DynTypedNode::create(call); + while (true) { + auto const parents = ctx.getParents(node); + if (parents.size() != 1) { + return false; + } + auto const& parent = parents[0]; + auto const* stmt = node.get(); + if (parent.get() || parent.get()) { + return false; + } else if (parent.get() || parent.get()) { + return true; + } else if (auto const* s = parent.get()) { + if (stmt != s->getInit()) { + return true; + } + } else if (auto const* s = parent.get()) { + if (stmt == s->getBody()) { + return true; + } + } + node = parent; + } +} + +} // namespace + +void ChannelLookupInLoopCheck::registerMatchers(MatchFinder* finder) +{ + auto const device = cxxRecordDecl(isSameOrDerivedFrom("::fair::mq::Device")); + auto const lookup = cxxMemberCallExpr( + callee(cxxMethodDecl(hasAnyName("GetChannel", "GetChannels"), ofClass(hasName("::fair::mq::Device")))), + unless(isInTemplateInstantiation())); + + finder->addMatcher( + cxxMemberCallExpr(lookup, + forFunction(cxxMethodDecl(hasName("ConditionalRun"), isOverride(), ofClass(device)))) + .bind("conditionalRun"), + this); + finder->addMatcher( + cxxMemberCallExpr(lookup, + forFunction(cxxMethodDecl(hasName("Run"), parameterCountIs(0), isOverride(), ofClass(device)))) + .bind("run"), + this); +} + +void ChannelLookupInLoopCheck::check(MatchFinder::MatchResult const& result) +{ + auto const* call = result.Nodes.getNodeAs("conditionalRun"); + char const* where = "'ConditionalRun()'"; + if (!call) { + call = result.Nodes.getNodeAs("run"); + where = "a loop in 'Run()'"; + if (!inLoop(*call, *result.Context)) { + return; + } + } + diag(call->getExprLoc(), + "'%0()' looks up the channel by name in each iteration of %1; look it up once, e.g. in " + "'InitTask()', and keep a reference") + << call->getMethodDecl()->getName() << where; +} + +} // namespace fair::mq::tidy diff --git a/fairmq/tidy/ChannelLookupInLoopCheck.h b/fairmq/tidy/ChannelLookupInLoopCheck.h new file mode 100644 index 00000000..41368238 --- /dev/null +++ b/fairmq/tidy/ChannelLookupInLoopCheck.h @@ -0,0 +1,39 @@ +/******************************************************************************** + * Copyright (C) 2026 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * * + * This software is distributed under the terms of the * + * GNU Lesser General Public Licence (LGPL) version 3, * + * copied verbatim in the file "LICENSE" * + ********************************************************************************/ + + +#ifndef FAIR_MQ_TIDY_CHANNELLOOKUPINLOOPCHECK_H +#define FAIR_MQ_TIDY_CHANNELLOOKUPINLOOPCHECK_H + +#include + +namespace fair::mq::tidy { + +/// Finds calls of `GetChannel()` and `GetChannels()` that run once per +/// iteration of the device loop: in an override of `ConditionalRun()`, or in +/// a loop in an override of `Run()`. Each call looks up the channel name in a +/// map. Look up the channel once, e.g. in `InitTask()`, and keep a reference. +/// +/// The check does not report `Send(msg, "name")` and `Receive(msg, "name")`, +/// which also look up the channel, because they are the usual short form. +class ChannelLookupInLoopCheck : public clang::tidy::ClangTidyCheck +{ + public: + using ClangTidyCheck::ClangTidyCheck; + + bool isLanguageVersionSupported(clang::LangOptions const& opts) const override + { + return opts.CPlusPlus17; + } + void registerMatchers(clang::ast_matchers::MatchFinder* finder) override; + void check(clang::ast_matchers::MatchFinder::MatchResult const& result) override; +}; + +} // namespace fair::mq::tidy + +#endif /* FAIR_MQ_TIDY_CHANNELLOOKUPINLOOPCHECK_H */ diff --git a/fairmq/tidy/Module.cxx b/fairmq/tidy/Module.cxx index b3e18b85..fb881d95 100644 --- a/fairmq/tidy/Module.cxx +++ b/fairmq/tidy/Module.cxx @@ -6,6 +6,7 @@ * copied verbatim in the file "LICENSE" * ********************************************************************************/ +#include #include #include #include @@ -42,6 +43,7 @@ class Module : public clang::tidy::ClangTidyModule factories.registerCheck("fairmq-modernize-parts-api"); factories.registerCheck("fairmq-modernize-progoptions-accessors"); factories.registerCheck("fairmq-modernize-run-device"); + factories.registerCheck("fairmq-performance-channel-lookup-in-loop"); factories.registerCheck("fairmq-readability-transfer-code"); } }; diff --git a/test/tidy/fairmq-performance-channel-lookup-in-loop.cxx b/test/tidy/fairmq-performance-channel-lookup-in-loop.cxx new file mode 100644 index 00000000..150f1f97 --- /dev/null +++ b/test/tidy/fairmq-performance-channel-lookup-in-loop.cxx @@ -0,0 +1,38 @@ +#include + +struct Sampler : fair::mq::Device +{ + bool ConditionalRun() override + { + // CHECK-MESSAGES: :[[@LINE+1]]:20: warning: 'GetChannel()' looks up the channel by name in each iteration of 'ConditionalRun()'; look it up once, e.g. in 'InitTask()', and keep a reference [fairmq-performance-channel-lookup-in-loop] + auto& ch = GetChannel("data"); + auto msg(ch.NewMessage()); + return ch.Send(msg) >= 0; + } +}; + +struct Looper : fair::mq::Device +{ + void Run() override + { + auto& out = GetChannel("data"); + while (!NewStatePending()) { + // CHECK-MESSAGES: :[[@LINE+1]]:24: warning: 'GetChannel()' looks up the channel by name in each iteration of a loop in 'Run()'; look it up once, e.g. in 'InitTask()', and keep a reference [fairmq-performance-channel-lookup-in-loop] + auto& in = GetChannel("in", 0); + auto msg(in.NewMessage()); + // CHECK-MESSAGES: :[[@LINE+1]]:17: warning: 'GetChannels()' looks up the channel by name in each iteration of a loop in 'Run()'; look it up once, e.g. in 'InitTask()', and keep a reference [fairmq-performance-channel-lookup-in-loop] + if (GetChannels().count("monitor") == 0) { + out.Send(msg); + } + } + // Not reported: once per loop, or in a lambda. + for (auto& sub : GetChannels().at("data")) { + static_cast(sub); + } + for (auto& ch = GetChannel("data"); !NewStatePending();) { + auto msg(ch.NewMessage()); + auto send = [&] { return GetChannel("data").Send(msg); }; + send(); + } + } +};