Skip to content

Commit

Permalink
Better example, use std::byte instead of unsigned char
Browse files Browse the repository at this point in the history
  • Loading branch information
omilyutin-tt committed Feb 19, 2025
1 parent 1aefa1e commit 1391409
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
25 changes: 20 additions & 5 deletions contributing/BestPractices.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,17 +376,32 @@ To ensure safe and predictable program termination, static objects should meet t
**Avoid:**
```cpp
// Bad: Using a static object with a non-trivial destructor.
static std::string kData = "Non-trivial destructor! Bad!";
static const std::map<int, std::string> kDeviceConfigFiles = {
{1, "n150.yaml"},
{2, "n300.yaml"},
{8, "t3000.yaml"}
};
```

**Prefer:**
```cpp
// Option 1: Use a trivial type for static data when possible.
static constexpr std::string_view kData = "Trivial destructor! Good!";
constexpr std::string_view kData = "Trivial destructor! Good!";

constexpr uint32_t kMaxNumberOfCommandQueues = 2;

// Using array of trivially destructible types is OK.
constexpr std::array<int, 3> kDeviceIds = {1, 2, 8};

// Option 2: If dynamic initialization is required, use function-local statics with `Indestructible`.
const auto& get_data() {
static Indestructible<std::string> kData("Disabled destructor! Good!");
return kData.get();
const auto& get_device_configs() {
static tt::stl::Indestructible<std::map<int, std::string_view>> configs{
std::map<int, std::string_view>{
{1, "n150.yaml"},
{2, "n300.yaml"},
{8, "t3000.yaml"}
}
};
return configs.get();
}
```
5 changes: 3 additions & 2 deletions tt_metal/tt_stl/indestructible.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <cstddef>
#include <new>
#include <utility>

Expand Down Expand Up @@ -43,8 +44,8 @@ class Indestructible {
~Indestructible() = default;

private:
// A buffer of unsigned char with alignment of T and size of T
alignas(T) unsigned char storage_[sizeof(T)];
// A buffer of std::byte with alignment of T and size of T
alignas(T) std::byte storage_[sizeof(T)];
};

} // namespace tt::stl

0 comments on commit 1391409

Please sign in to comment.