From 30943121fa4388ebab0381e9ad06dab340e86039 Mon Sep 17 00:00:00 2001 From: Andreas Stefl Date: Mon, 29 Apr 2024 16:33:55 +0200 Subject: [PATCH] perf: Cache particle hypothesis (#3151) It's not possible to make these compile time constants if we want to share the constants with `ParticleData` but we can make them runtime constants. It looks like the `findMass` / `findCharge` calls got more expensive after https://github.com/acts-project/acts/pull/2908 but I don't see a reason to optimize them after keeping caching the hypothesis. --- .../Acts/EventData/ParticleHypothesis.hpp | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/Core/include/Acts/EventData/ParticleHypothesis.hpp b/Core/include/Acts/EventData/ParticleHypothesis.hpp index e2865e41d13..8fb7fe9ec03 100644 --- a/Core/include/Acts/EventData/ParticleHypothesis.hpp +++ b/Core/include/Acts/EventData/ParticleHypothesis.hpp @@ -36,23 +36,30 @@ class SinglyChargedParticleHypothesis : GenericParticleHypothesis(other) {} static SinglyChargedParticleHypothesis muon() { - return SinglyChargedParticleHypothesis(PdgParticle::eMuon); + static const SinglyChargedParticleHypothesis cache(PdgParticle::eMuon); + return cache; } static SinglyChargedParticleHypothesis pion() { - return SinglyChargedParticleHypothesis(PdgParticle::ePionPlus); + static const SinglyChargedParticleHypothesis cache(PdgParticle::ePionPlus); + return cache; } static SinglyChargedParticleHypothesis electron() { - return SinglyChargedParticleHypothesis(PdgParticle::eElectron); + static const SinglyChargedParticleHypothesis cache(PdgParticle::eElectron); + return cache; } static SinglyChargedParticleHypothesis kaon() { - return SinglyChargedParticleHypothesis(PdgParticle::eKaonPlus); + static const SinglyChargedParticleHypothesis cache(PdgParticle::eKaonPlus); + return cache; } static SinglyChargedParticleHypothesis proton() { - return SinglyChargedParticleHypothesis(PdgParticle::eProton); + static const SinglyChargedParticleHypothesis cache(PdgParticle::eProton); + return cache; } static SinglyChargedParticleHypothesis chargedGeantino() { - return SinglyChargedParticleHypothesis(PdgParticle::eInvalid, 0); + static const SinglyChargedParticleHypothesis cache(PdgParticle::eInvalid, + 0); + return cache; } }; @@ -72,14 +79,17 @@ class NeutralParticleHypothesis : public GenericParticleHypothesis { : GenericParticleHypothesis(other) {} static NeutralParticleHypothesis photon() { - return NeutralParticleHypothesis(PdgParticle::eGamma); + static const NeutralParticleHypothesis cache(PdgParticle::eGamma); + return cache; } static NeutralParticleHypothesis pion0() { - return NeutralParticleHypothesis(PdgParticle::ePionZero); + static const NeutralParticleHypothesis cache(PdgParticle::ePionZero); + return cache; } static NeutralParticleHypothesis geantino() { - return NeutralParticleHypothesis(PdgParticle::eInvalid, 0); + static const NeutralParticleHypothesis cache(PdgParticle::eInvalid, 0); + return cache; } }; @@ -122,7 +132,8 @@ class NonNeutralChargedParticleHypothesis } static NonNeutralChargedParticleHypothesis chargedGeantino() { - return chargedGeantino(Acts::UnitConstants::e); + static const auto cache = chargedGeantino(Acts::UnitConstants::e); + return cache; } static NonNeutralChargedParticleHypothesis chargedGeantino(float absQ) { return NonNeutralChargedParticleHypothesis(PdgParticle::eInvalid, 0, absQ); @@ -175,7 +186,8 @@ class ParticleHypothesis : public GenericParticleHypothesis { return NeutralParticleHypothesis::geantino(); } static ParticleHypothesis chargedGeantino() { - return chargedGeantino(Acts::UnitConstants::e); + static const auto cache = chargedGeantino(Acts::UnitConstants::e); + return cache; } static ParticleHypothesis chargedGeantino(float absQ) { return ParticleHypothesis(PdgParticle::eInvalid, 0, absQ);