ENH: Refactor the ThrottledMessenger class to use an internal thread as a timer#1528
Open
imikejackson wants to merge 5 commits intoBlueQuartzSoftware:developfrom
Open
Conversation
cde78a6 to
6556787
Compare
…erred formatting Replace the spdlog-based messaging system with a new design that eliminates the expensive std::chrono::steady_clock::now() call from the hot path. The new system uses a singleton MessageDispatcher with a background timer thread that periodically flushes pending messages, reducing hot-path cost from ~50-200ns to ~10-25ns per call. Key changes: - Add MessageDispatcher singleton with background timer thread (100ms tick) - Add ThrottledChannel<Args...> with mutex+tuple storage for general case - Add ThrottledChannel<usize> atomic specialization for common progress case - Add ProgressHelper/ProgressWorker for multi-threaded progress accumulation - Add callable_traits for automatic formatter argument type deduction - Remove spdlog dependency (MessageHandlerSink, ThrottleSink, ThreadPool) - Remove old Messenger, ProgressMessenger, ProgressMessageHelper classes - Migrate all 37 caller files across simplnx, OrientationAnalysis plugins - Add 10 new unit tests for MessageHelper covering all new functionality Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Remove unused TimeUtilities.hpp include from RequireMinimumSizeFeatures.cpp - Remove unused iostream includes from AlignSectionsFeatureCentroid.cpp and AlignSectionsMisorientation.cpp - Add docs/SendingFilterUpdates.md explaining the new messaging API - Update TODO.md marking all phases complete Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add Code_Review/SUMMARY.md with comprehensive change summary - Update messaging_classes.dot to show ThrottledChannel<usize> atomic specialization, MessageDispatcher::Impl pimpl, callable_traits hierarchy, and CalculatePercentComplete utility - Regenerate PNG and SVG diagrams Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace hand-rolled thread-safe messaging patterns with the new ProgressHelper/ProgressWorker and ThrottledMessenger APIs across multiple algorithm classes: - ResampleImageGeom: Use ProgressHelper for multi-threaded progress - InterpolateValuesToUnstructuredGrid: Use ProgressHelper (SimplnxReview) - Remove FilterProgressCallback from ImageRotationUtilities (had static mutexes, race condition on counter, inverted throttling logic) - RotateImageGeometry workers: Use local ThrottledMessenger<string> - ApplyTransformationToNodeGeometry: Use ProgressWorker - Rename WriteStlFile::sendThreadSafeProgressMessage to sendThreadSafeErrorResult to reflect actual purpose Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace placeholder examples with actual codebase patterns from ResampleImageGeom, FillBadData, RotateImageGeometry, and others. Add choosing-the-right-pattern table, ParallelTaskAlgorithm and ParallelDataAlgorithm integration examples, mutable ProgressWorker pattern, and migration guides for all three legacy patterns. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
6556787 to
535900a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Threaded Throttled Messenger - Change Summary
Problem
The existing
ThrottledMessenger::sendThrottledMessage()calledstd::chrono::steady_clock::now()on every invocation in tight voxel loops,adding 50-200ns of pure overhead per call. For a 10M voxel loop, this meant
0.5-2.0 seconds of wasted time on clock syscalls alone. Additionally, the spdlog
async backend introduced unnecessary complexity with redundant throttle layers.
Solution
A singleton background timer thread (
MessageDispatcher) now handles all clockchecking and string formatting. The filter's hot path (
sendMessage()) onlystores raw argument values -- no clock check, no
fmt::format()call.Performance Improvement
usize)Architecture
Key Classes
MessageHelperIFilter::MessageHandlerThrottledMessenger<Args...>sendMessage()stores args cheaplyThrottledChannel<Args...>ThrottledChannel<usize>ProgressHelperProgressWorkerincrementProgress()ProgressChannelstd::atomic<usize>progress counterMessageDispatcherIThrottledChannelcallable_traits<T>Files Changed
Core Implementation (2 files, rewritten)
src/simplnx/Utilities/MessageHelper.hpp-- All public API classessrc/simplnx/Utilities/MessageHelper.cpp-- MessageDispatcher singleton + pimplNew Files (2 files)
test/MessageHelperTest.cpp-- 10 Catch2 unit testsdocs/SendingFilterUpdates.md-- Developer documentationBuild System (3 files)
CMakeLists.txt-- Removedfind_package(spdlog)and link dependencyvcpkg.json-- Removed spdlog from dependenciestest/CMakeLists.txt-- AddedMessageHelperTest.cppMigrated Callers (37 files)
Core Utilities (7 files)
src/simplnx/Utilities/SegmentFeatures.hppsrc/simplnx/Utilities/SegmentFeatures.cppsrc/simplnx/Utilities/SampleSurfaceMesh.hppsrc/simplnx/Utilities/SampleSurfaceMesh.cppsrc/simplnx/Utilities/OStreamUtilities.cppsrc/simplnx/Utilities/AlignSections.hppsrc/simplnx/Utilities/AlignSections.cppSimplnxCore Filter Algorithms (17 files)
Filters/Algorithms/FillBadData.cppFilters/Algorithms/ErodeDilateBadData.cppFilters/Algorithms/RequireMinimumSizeFeatures.cppFilters/Algorithms/ComputeNeighborhoods.hppFilters/Algorithms/ComputeNeighborhoods.cppFilters/Algorithms/ComputeArrayStatistics.cppFilters/Algorithms/ComputeVertexToTriangleDistances.cppFilters/Algorithms/CalculateTriangleGroupCurvatures.hppFilters/Algorithms/CalculateTriangleGroupCurvatures.cppFilters/Algorithms/RemoveFlaggedFeatures.cppFilters/Algorithms/DBSCAN.cppFilters/Algorithms/ReadStlFile.cppFilters/Algorithms/FeatureFaceCurvature.cppFilters/Algorithms/ComputeNeighborListStatistics.cppFilters/Algorithms/ComputeArrayHistogramByFeature.cppFilters/Algorithms/AlignSectionsFeatureCentroid.cppFilters/Algorithms/WriteLAMMPSFile.cppSimplnxCore Filter Files (2 files)
Filters/IterativeClosestPointFilter.cppFilters/ComputeFeatureNeighborsFilter.cppOrientationAnalysis Plugin (6 files)
Filters/Algorithms/ComputeGBCD.cppFilters/Algorithms/RotateEulerRefFrame.cppFilters/Algorithms/NeighborOrientationCorrelation.cppFilters/Algorithms/BadDataNeighborOrientationCheck.cppFilters/Algorithms/ComputeKernelAvgMisorientations.cppFilters/Algorithms/AlignSectionsMisorientation.cppSimplnxReview Plugin (3 files, separate repository)
Filters/Algorithms/GroupMicroTextureRegions.cppFilters/Algorithms/ComputeGroupingDensity.cppFilters/Algorithms/MergeColonies.cppWhat Was Removed
spdlogdependency (library, includes, vcpkg entry, CMake linkage)Messengerclass (hpp + cpp) andMessenger::ImplMessageHandlerSink(spdlog custom sink)ThrottleSink(spdlog rate-limiting sink)ThreadPoolsingleton (spdlog async thread pool)ThrottledMessageFunctorconceptProgressMessageFunctorconceptProgressMessageDatastructThrottledMessengerclassProgressMessengerclassProgressMessageHelperclassWhat Was Retained
IFilter::Message,IFilter::ProgressMessage,IFilter::MessageHandler-- unchangedCalculatePercentComplete<T>()-- retained as-isMigration Pattern
Testing
shouldCancel