diff options
| author | lukyan <[email protected]> | 2025-07-31 11:18:15 +0300 |
|---|---|---|
| committer | lukyan <[email protected]> | 2025-07-31 12:26:35 +0300 |
| commit | be28619189eb153f4454ec29cfb94e0359a1838c (patch) | |
| tree | 6957e78714bbff0a2959d9d75042855ad8735a18 | |
| parent | 306629c233bd285a5abe409aa3cb79130fe5a4ff (diff) | |
YT-25634: Create periodic yielder on stack
commit_hash:691a7ada6e79715563396f59a185d774cd4b0997
| -rw-r--r-- | yt/yt/client/table_client/adapters.cpp | 6 | ||||
| -rw-r--r-- | yt/yt/core/concurrency/periodic_yielder.cpp | 15 | ||||
| -rw-r--r-- | yt/yt/core/concurrency/periodic_yielder.h | 13 |
3 files changed, 21 insertions, 13 deletions
diff --git a/yt/yt/client/table_client/adapters.cpp b/yt/yt/client/table_client/adapters.cpp index 47547c76239..a16ac651c90 100644 --- a/yt/yt/client/table_client/adapters.cpp +++ b/yt/yt/client/table_client/adapters.cpp @@ -157,7 +157,7 @@ void PipeReaderToWriter( const IUnversionedRowsetWriterPtr& writer, const TPipeReaderToWriterOptions& options) { - TPeriodicYielder yielder(TDuration::Seconds(1)); + auto yielder = CreatePeriodicYielder(TDuration::Seconds(1)); TRowBatchReadOptions readOptions{ .MaxRowsPerRead = options.BufferRowCount, @@ -223,7 +223,7 @@ void PipeReaderToWriterByBatches( TDuration pipeDelay) { try { - TPeriodicYielder yielder(TDuration::Seconds(1)); + auto yielder = CreatePeriodicYielder(TDuration::Seconds(1)); for (bool isFirstBatch = true; auto batch = reader->Read(options); isFirstBatch = false) { yielder.TryYield(); @@ -274,7 +274,7 @@ void PipeInputToOutput( struct TWriteBufferTag { }; TBlob buffer(GetRefCountedTypeCookie<TWriteBufferTag>(), bufferBlockSize, /*initializeStorage*/ false); - TPeriodicYielder yielder(TDuration::Seconds(1)); + auto yielder = CreatePeriodicYielder(TDuration::Seconds(1)); while (true) { yielder.TryYield(); diff --git a/yt/yt/core/concurrency/periodic_yielder.cpp b/yt/yt/core/concurrency/periodic_yielder.cpp index 1e841e73ffa..330685b9028 100644 --- a/yt/yt/core/concurrency/periodic_yielder.cpp +++ b/yt/yt/core/concurrency/periodic_yielder.cpp @@ -8,19 +8,19 @@ using namespace NProfiling; //////////////////////////////////////////////////////////////////////////////// -TPeriodicYielder::TPeriodicYielder(TDuration period) +TPeriodicYielderGuard::TPeriodicYielderGuard(std::optional<TDuration> period) : TContextSwitchGuard( [this] () noexcept { Stop(); }, [this] () noexcept { Restart(); }) - , CpuPeriod_(DurationToCpuDuration(period)) + , CpuPeriod_(period ? std::optional(DurationToCpuDuration(*period)) : std::nullopt) { } -bool TPeriodicYielder::NeedYield() const +bool TPeriodicYielderGuard::NeedYield() const { - return GetElapsedCpuTime() > CpuPeriod_; + return CpuPeriod_ && *CpuPeriod_ < GetElapsedCpuTime(); } -bool TPeriodicYielder::TryYield() const +bool TPeriodicYielderGuard::TryYield() const { if (NeedYield()) { Yield(); @@ -30,6 +30,11 @@ bool TPeriodicYielder::TryYield() const return false; } +TPeriodicYielderGuard CreatePeriodicYielder(std::optional<TDuration> period) +{ + return {period}; +} + //////////////////////////////////////////////////////////////////////////////// } // namespace NYT::NConcurrency diff --git a/yt/yt/core/concurrency/periodic_yielder.h b/yt/yt/core/concurrency/periodic_yielder.h index d04e019faea..67f9368bd4e 100644 --- a/yt/yt/core/concurrency/periodic_yielder.h +++ b/yt/yt/core/concurrency/periodic_yielder.h @@ -8,23 +8,26 @@ namespace NYT::NConcurrency { //////////////////////////////////////////////////////////////////////////////// - -class TPeriodicYielder +class TPeriodicYielderGuard : public NProfiling::TWallTimer , private TContextSwitchGuard { public: - TPeriodicYielder(TDuration period = TDuration::MilliSeconds(30)); - bool NeedYield() const; //! Returns true, if we have released the thread and got back to execution. bool TryYield() const; private: - TCpuDuration CpuPeriod_; + std::optional<TCpuDuration> CpuPeriod_; + + TPeriodicYielderGuard(std::optional<TDuration> period = std::nullopt); + + friend TPeriodicYielderGuard CreatePeriodicYielder(std::optional<TDuration> period); }; +TPeriodicYielderGuard CreatePeriodicYielder(std::optional<TDuration> period = std::nullopt); + //////////////////////////////////////////////////////////////////////////////// } // namespace NYT::NConcurrency |
