Skip to content

Commit

Permalink
any: perf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Nov 8, 2024
1 parent b2110f0 commit 1c8b909
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,5 @@ TODO:
* sigh_mixin: change cb signature from reg/entt to storage/entt (breaking for the good)
* don't pass reactive storage by default to callback
* runtime types support for meta for types that aren't backed by C++ types
* bypass vtable in any (improvements available for move/get, add more policy types)
* dtor, traits and custom should be part of meta descriptor maybe (?)
* allow attaching const values of non-Type type to meta types
29 changes: 15 additions & 14 deletions src/entt/core/any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,11 @@ class basic_any {
}
break;
case operation::move:
ENTT_ASSERT(value.owner(), "Unexpected mode");

ENTT_ASSERT(value.mode == any_policy::embedded, "Unexpected policy type");
if constexpr(in_situ<Type>) {
return ::new(&static_cast<basic_any *>(const_cast<void *>(other))->storage) Type{std::move(*const_cast<Type *>(elem))};
} else {
return (static_cast<basic_any *>(const_cast<void *>(other))->instance = std::exchange(const_cast<basic_any &>(value).instance, nullptr));
}
break;
case operation::transfer:
if constexpr(std::is_move_assignable_v<Type>) {
*const_cast<Type *>(elem) = std::move(*static_cast<Type *>(const_cast<void *>(other)));
Expand Down Expand Up @@ -114,7 +112,11 @@ class basic_any {
return (elem == other) ? other : nullptr;
}
case operation::get:
return elem;
ENTT_ASSERT(value.mode == any_policy::embedded, "Unexpected policy type");
if constexpr(in_situ<Type>) {
return elem;
}
break;
}

return nullptr;
Expand Down Expand Up @@ -216,10 +218,10 @@ class basic_any {
info{other.info},
vtable{other.vtable},
mode{other.mode} {
if(other.mode == any_policy::ref || other.mode == any_policy::cref) {
instance = std::exchange(other.instance, nullptr);
} else if(other.vtable) {
if(other.mode == any_policy::embedded) {
other.vtable(operation::move, other, this);
} else if(other.mode != any_policy::empty) {
instance = std::exchange(other.instance, nullptr);
}
}

Expand Down Expand Up @@ -257,10 +259,10 @@ class basic_any {

reset();

if(other.mode == any_policy::ref || other.mode == any_policy::cref) {
instance = std::exchange(other.instance, nullptr);
} else if(other.vtable) {
if(other.mode == any_policy::embedded) {
other.vtable(operation::move, other, this);
} else if(other.mode != any_policy::empty) {
instance = std::exchange(other.instance, nullptr);
}

info = other.info;
Expand Down Expand Up @@ -295,7 +297,7 @@ class basic_any {
* @return An opaque pointer the contained instance, if any.
*/
[[nodiscard]] const void *data() const noexcept {
return vtable ? vtable(operation::get, *this, nullptr) : nullptr;
return (mode == any_policy::embedded) ? vtable(operation::get, *this, nullptr) : instance;
}

/**
Expand Down Expand Up @@ -368,8 +370,7 @@ class basic_any {
vtable(operation::destroy, *this, nullptr);
}

// unnecessary but it helps to detect nasty bugs
ENTT_ASSERT((instance = nullptr) == nullptr, "");
instance = nullptr;
info = &type_id<void>();
vtable = nullptr;
mode = any_policy::empty;
Expand Down

0 comments on commit 1c8b909

Please sign in to comment.