Skip to content

Commit 7a3958c

Browse files
koct9irobot-piglet
authored andcommitted
yt/error: optimize constructing TError from TErrorException
Here we can avoid dynamic cast and exception hazard. Signed-off-by: Konstantin Khlebnikov <khlebnikov@tracto.ai> --- Pull Request resolved: ytsaurus/ytsaurus#1070 commit_hash:e0709e1fe0f2109792b20b850e0e11a6e96f846a
1 parent 802da27 commit 7a3958c

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

library/cpp/yt/error/error-inl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@ TErrorOr<T>::TErrorOr(TErrorOr<U>&& other) noexcept
312312
}
313313
}
314314

315+
template <class T>
316+
TErrorOr<T>::TErrorOr(const TErrorException& errorEx) noexcept
317+
: TError(errorEx)
318+
{ }
319+
315320
template <class T>
316321
TErrorOr<T>::TErrorOr(const std::exception& ex)
317322
: TError(ex)

library/cpp/yt/error/error.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ TError::TErrorOr(TError&& other) noexcept
242242
: Impl_(std::move(other.Impl_))
243243
{ }
244244

245+
TError::TErrorOr(const TErrorException& errorEx) noexcept
246+
{
247+
*this = errorEx.Error();
248+
// NB: TErrorException verifies that error not IsOK at throwing end.
249+
}
250+
245251
TError::TErrorOr(const std::exception& ex)
246252
{
247253
if (auto simpleException = dynamic_cast<const TSimpleException*>(&ex)) {

library/cpp/yt/error/error.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ void FormatValue(TStringBuilderBase* builder, TErrorCode code, TStringBuf spec);
5656

5757
////////////////////////////////////////////////////////////////////////////////
5858

59+
// Forward declaration.
60+
class TErrorException;
61+
5962
template <class TValue>
6063
concept CErrorNestable = requires (TError& error, TValue&& operand)
6164
{
@@ -72,6 +75,8 @@ class [[nodiscard]] TErrorOr<void>
7275
TErrorOr(const TError& other);
7376
TErrorOr(TError&& other) noexcept;
7477

78+
TErrorOr(const TErrorException& errorEx) noexcept;
79+
7580
TErrorOr(const std::exception& ex);
7681

7782
struct TDisableFormat
@@ -365,6 +370,8 @@ class [[nodiscard]] TErrorOr
365370
TErrorOr(const TError& other);
366371
TErrorOr(TError&& other) noexcept;
367372

373+
TErrorOr(const TErrorException& errorEx) noexcept;
374+
368375
TErrorOr(const std::exception& ex);
369376

370377
template <class U>

0 commit comments

Comments
 (0)