Skip to content

Commit

Permalink
makes implementations trivially destructible
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Apr 24, 2024
1 parent 9b8aac5 commit ef0dfeb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 5 additions & 0 deletions doc/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -2271,9 +2271,14 @@ acts as an iterator (and is therefore not thread safe).
The CPU detection, which runs the first time parsing is attempted and switches to the fastest
parser for your CPU, is transparent and thread-safe.
Our runtime dispatching is based on global objects that are instantiated at the beginning of the
main thread and may be discarded at the end of the main thread. If you have multiple threads running
and some threads use the library while the main thread is cleaning up ressources, you may encounter
issues. If you expect such problems, you may consider using [std::quick_exit](https://en.cppreference.com/w/cpp/utility/program/quick_exit).
In a threaded environment, stack space is often limited. Running code like simdjson in debug mode may require hundreds of kilobytes of stack memory. Thus stack overflows are a possibility. We recommend you turn on optimization when working in an environment where stack space is limited. If you must run your code in debug mode, we recommend you configure your system to have more stack space. We discourage you from running production code based on a debug build.
Standard compliance
--------------------
Expand Down
11 changes: 6 additions & 5 deletions include/simdjson/implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class implementation {
*
* @return the name of the implementation, e.g. "haswell", "westmere", "arm64".
*/
virtual const std::string &name() const { return _name; }
virtual std::string name() const { return std::string(_name); }

/**
* The description of this implementation.
Expand All @@ -63,7 +63,7 @@ class implementation {
*
* @return the description of the implementation, e.g. "Intel/AMD AVX2", "Intel/AMD SSE4.2", "ARM NEON".
*/
virtual const std::string &description() const { return _description; }
virtual std::string description() const { return std::string(_description); }

/**
* The instruction sets this implementation is compiled against
Expand Down Expand Up @@ -139,18 +139,19 @@ class implementation {
_required_instruction_sets(required_instruction_sets)
{
}
virtual ~implementation()=default;
protected:
~implementation() = default;

private:
/**
* The name of this implementation.
*/
const std::string _name;
std::string_view _name;

/**
* The description of this implementation.
*/
const std::string _description;
std::string_view _description;

/**
* Instruction sets required for this implementation.
Expand Down
8 changes: 6 additions & 2 deletions src/implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ namespace internal {
*/
class detect_best_supported_implementation_on_first_use final : public implementation {
public:
const std::string &name() const noexcept final { return set_best()->name(); }
const std::string &description() const noexcept final { return set_best()->description(); }
std::string name() const noexcept final { return set_best()->name(); }
std::string description() const noexcept final { return set_best()->description(); }
uint32_t required_instruction_sets() const noexcept final { return set_best()->required_instruction_sets(); }
simdjson_warn_unused error_code create_dom_parser_implementation(
size_t capacity,
Expand All @@ -190,6 +190,8 @@ class detect_best_supported_implementation_on_first_use final : public implement
const implementation *set_best() const noexcept;
};

static_assert(std::is_trivially_destructible<detect_best_supported_implementation_on_first_use>::value, "detect_best_supported_implementation_on_first_use should be trivially destructible");

static const std::initializer_list<const implementation *>& get_available_implementation_pointers() {
static const std::initializer_list<const implementation *> available_implementation_pointers {
#if SIMDJSON_IMPLEMENTATION_ICELAKE
Expand Down Expand Up @@ -246,6 +248,8 @@ class unsupported_implementation final : public implementation {
unsupported_implementation() : implementation("unsupported", "Unsupported CPU (no detected SIMD instructions)", 0) {}
};

static_assert(std::is_trivially_destructible<unsupported_implementation>::value, "unsupported_singleton should be trivially destructible");

const unsupported_implementation* get_unsupported_singleton() {
static const unsupported_implementation unsupported_singleton{};
return &unsupported_singleton;
Expand Down

0 comments on commit ef0dfeb

Please sign in to comment.