From ab7d7fd443abbdedf8932373a707a9d4778141a8 Mon Sep 17 00:00:00 2001 From: liuzhenlong Date: Sun, 12 Jul 2026 16:32:53 +0800 Subject: [PATCH] refactor: declare functions noexcept if they won't emit exceptions. --- .../references/custom-code-style.md | 22 +++++++++++++++++++ .../core/framework/model/model_input_params.h | 4 ++-- xllm/core/layers/npu/buffer/atb_workspace.h | 4 ++-- xllm/core/platform/stream.h | 2 +- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.agents/skills/code-review/references/custom-code-style.md b/.agents/skills/code-review/references/custom-code-style.md index 23ca1ca78f..35ac5ff116 100644 --- a/.agents/skills/code-review/references/custom-code-style.md +++ b/.agents/skills/code-review/references/custom-code-style.md @@ -157,6 +157,28 @@ ModelOutput forward(torch::Tensor tokens, ...) override; virtual ModelOutput forward(torch::Tensor tokens, ...); ``` +- **Declare functions `noexcept` if they won't emit exceptions.** This is part of the function's interface contract, like `const`. Pay special attention to move constructors, move assignment operators, and `swap`: `std::vector` reallocation moves elements only when move is `noexcept`. Do not use C++98 exception specifications (`throw()`). Do not mark `noexcept` on functions that may throw or call APIs that can throw. + +```cpp +// Good – move is noexcept; vector reallocation can move instead of copy +MyResource(MyResource&& other) noexcept; +MyResource& operator=(MyResource&& other) noexcept; + +void swap(MyResource& lhs, MyResource& rhs) noexcept; +``` + +```cpp +// Bad – move not noexcept; std::vector may fall back to copy +MyResource(MyResource&& other); +MyResource& operator=(MyResource&& other); + +// Bad – deprecated C++98 exception specification +void reset() throw(); + +// Bad – noexcept on a function that may throw +void parse_file(const std::string& path) noexcept; +``` + - **Structs must not have member functions**. If you need methods, use a `class`. Structs are for plain data aggregation only. --- diff --git a/xllm/core/framework/model/model_input_params.h b/xllm/core/framework/model/model_input_params.h index fd6848f10b..a5f7a68c84 100644 --- a/xllm/core/framework/model/model_input_params.h +++ b/xllm/core/framework/model/model_input_params.h @@ -661,7 +661,7 @@ struct BlockTransferInfo { memcpy(hash_key, other.hash_key, XXH3_128BITS_HASH_VALUE_LEN); } - BlockTransferInfo(BlockTransferInfo&& other) + BlockTransferInfo(BlockTransferInfo&& other) noexcept : src_block_id(other.src_block_id), dst_block_id(other.dst_block_id), transfer_type(other.transfer_type) { @@ -679,7 +679,7 @@ struct BlockTransferInfo { return *this; } - BlockTransferInfo& operator=(BlockTransferInfo&& other) { + BlockTransferInfo& operator=(BlockTransferInfo&& other) noexcept { src_block_id = other.src_block_id; dst_block_id = other.dst_block_id; transfer_type = other.transfer_type; diff --git a/xllm/core/layers/npu/buffer/atb_workspace.h b/xllm/core/layers/npu/buffer/atb_workspace.h index 704a480eed..88f887bf1a 100644 --- a/xllm/core/layers/npu/buffer/atb_workspace.h +++ b/xllm/core/layers/npu/buffer/atb_workspace.h @@ -36,9 +36,9 @@ class AtbWorkspace { AtbWorkspace& operator=(const AtbWorkspace&) = delete; - AtbWorkspace(AtbWorkspace&&) = default; + AtbWorkspace(AtbWorkspace&&) noexcept = default; - AtbWorkspace& operator=(AtbWorkspace&&) = default; + AtbWorkspace& operator=(AtbWorkspace&&) noexcept = default; void* get_workspace_buffer(uint64_t bufferSize); diff --git a/xllm/core/platform/stream.h b/xllm/core/platform/stream.h index 11355a2289..054b4fb4e3 100644 --- a/xllm/core/platform/stream.h +++ b/xllm/core/platform/stream.h @@ -63,7 +63,7 @@ class Stream { Stream(const Stream&) = delete; Stream& operator=(const Stream&) = delete; - Stream(Stream&&) = default; + Stream(Stream&&) noexcept = default; Stream& operator=(Stream&&) = default; explicit Stream(PlatformStream stream, const int32_t timeout = -1);