Skip to content

Commit

Permalink
meta: avoid invoking .data() more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Jul 5, 2023
1 parent d00a4c4 commit 6257cc8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ TODO (high prio):
* further optimize exclusion lists in multi type views (no existence check)
* doc: bump entities
* deprecate/drop snapshot orphans function, make it a general purpose one
* avoid invoking ::data twice if possible ie within meta_associative_container::begin()

WIP:
* get rid of observers, storage based views made them pointless - document alternatives
Expand Down
35 changes: 17 additions & 18 deletions src/entt/meta/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ struct basic_meta_sequence_container_traits {
}
}

[[nodiscard]] static iterator iter(const meta_ctx &ctx, void *container, const void *as_const, const bool as_end) {
if(auto *const cont = static_cast<Type *>(container); cont) {
return iterator{ctx, as_end ? cont->end() : cont->begin()};
[[nodiscard]] static iterator iter(const meta_ctx &ctx, const void *container, const bool as_const, const bool as_end) {
if(as_const) {
const auto it = as_end ? static_cast<const Type *>(container)->end() : static_cast<const Type *>(container)->begin();
return iterator{ctx, it};
} else {
const auto it = as_end ? static_cast<Type *>(const_cast<void *>(container))->end() : static_cast<Type *>(const_cast<void *>(container))->begin();
return iterator{ctx, it};
}

auto *const cont = static_cast<const Type *>(as_const);
return iterator{ctx, as_end ? cont->end() : cont->begin()};
}

[[nodiscard]] static iterator insert_or_erase([[maybe_unused]] const meta_ctx &ctx, [[maybe_unused]] void *container, [[maybe_unused]] const any &handle, [[maybe_unused]] meta_any &value) {
Expand Down Expand Up @@ -110,13 +111,14 @@ struct basic_meta_associative_container_traits {
return true;
}

[[nodiscard]] static iterator iter(const meta_ctx &ctx, void *container, const void *as_const, const bool as_end) {
if(auto *const cont = static_cast<Type *>(container); cont) {
return iterator{ctx, std::bool_constant<key_only>{}, as_end ? cont->end() : cont->begin()};
[[nodiscard]] static iterator iter(const meta_ctx &ctx, const void *container, const bool as_const, const bool as_end) {
if(as_const) {
const auto it = as_end ? static_cast<const Type *>(container)->end() : static_cast<const Type *>(container)->begin();
return iterator{ctx, std::bool_constant<key_only>{}, it};
} else {
const auto it = as_end ? static_cast<Type *>(const_cast<void *>(container))->end() : static_cast<Type *>(const_cast<void *>(container))->begin();
return iterator{ctx, std::bool_constant<key_only>{}, it};
}

auto *const cont = static_cast<const Type *>(as_const);
return iterator{ctx, std::bool_constant<key_only>{}, as_end ? cont->end() : cont->begin()};
}

[[nodiscard]] static size_type insert_or_erase(void *container, meta_any &key, meta_any &value) {
Expand All @@ -135,13 +137,10 @@ struct basic_meta_associative_container_traits {
return 0u;
}

[[nodiscard]] static iterator find(const meta_ctx &ctx, void *container, const void *as_const, meta_any &key) {
[[nodiscard]] static iterator find(const meta_ctx &ctx, const void *container, const bool as_const, meta_any &key) {
if(key.allow_cast<const typename Type::key_type &>()) {
if(auto *const cont = static_cast<Type *>(container); cont) {
return iterator{ctx, std::bool_constant<key_only>{}, cont->find(key.cast<const typename Type::key_type &>())};
}

return iterator{ctx, std::bool_constant<key_only>{}, static_cast<const Type *>(as_const)->find(key.cast<const typename Type::key_type &>())};
return as_const ? iterator{ctx, std::bool_constant<key_only>{}, static_cast<const Type *>(container)->find(key.cast<const typename Type::key_type &>())}
: iterator{ctx, std::bool_constant<key_only>{}, static_cast<Type *>(const_cast<void *>(container))->find(key.cast<const typename Type::key_type &>())};
}

return iterator{};
Expand Down
16 changes: 8 additions & 8 deletions src/entt/meta/meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class meta_sequence_container {
internal::meta_type_node (*value_type_node)(const internal::meta_context &){};
size_type (*size_fn)(const void *) noexcept {};
bool (*resize_fn)(void *, size_type){};
iterator (*iter_fn)(const meta_ctx &, void *, const void *, const bool){};
iterator (*iter_fn)(const meta_ctx &, const void *, const bool, const bool){};
iterator (*insert_or_erase_fn)(const meta_ctx &, void *, const any &, meta_any &){};
any storage{};
};
Expand Down Expand Up @@ -143,9 +143,9 @@ class meta_associative_container {
internal::meta_type_node (*value_type_node)(const internal::meta_context &){};
size_type (*size_fn)(const void *) noexcept {};
bool (*clear_fn)(void *){};
iterator (*iter_fn)(const meta_ctx &, void *, const void *, const bool){};
iterator (*iter_fn)(const meta_ctx &, const void *, const bool, const bool){};
size_type (*insert_or_erase_fn)(void *, meta_any &, meta_any &){};
iterator (*find_fn)(const meta_ctx &, void *, const void *, meta_any &){};
iterator (*find_fn)(const meta_ctx &, const void *, const bool, meta_any &){};
any storage{};
};

Expand Down Expand Up @@ -1867,15 +1867,15 @@ inline bool meta_sequence_container::clear() {
* @return An iterator to the first element of the container.
*/
[[nodiscard]] inline meta_sequence_container::iterator meta_sequence_container::begin() {
return iter_fn(*ctx, storage.data(), std::as_const(storage).data(), false);
return iter_fn(*ctx, std::as_const(storage).data(), (storage.policy() == any_policy::cref), false);
}

/**
* @brief Returns an iterator that is past the last element of a container.
* @return An iterator that is past the last element of the container.
*/
[[nodiscard]] inline meta_sequence_container::iterator meta_sequence_container::end() {
return iter_fn(*ctx, storage.data(), std::as_const(storage).data(), true);
return iter_fn(*ctx, std::as_const(storage).data(), (storage.policy() == any_policy::cref), true);
}

/**
Expand Down Expand Up @@ -1958,12 +1958,12 @@ inline bool meta_associative_container::clear() {

/*! @copydoc meta_sequence_container::begin */
[[nodiscard]] inline meta_associative_container::iterator meta_associative_container::begin() {
return iter_fn(*ctx, storage.data(), std::as_const(storage).data(), false);
return iter_fn(*ctx, std::as_const(storage).data(), (storage.policy() == any_policy::cref), false);
}

/*! @copydoc meta_sequence_container::end */
[[nodiscard]] inline meta_associative_container::iterator meta_associative_container::end() {
return iter_fn(*ctx, storage.data(), std::as_const(storage).data(), true);
return iter_fn(*ctx, std::as_const(storage).data(), (storage.policy() == any_policy::cref), true);
}

/**
Expand Down Expand Up @@ -2001,7 +2001,7 @@ inline meta_associative_container::size_type meta_associative_container::erase(m
* @return An iterator to the element with the given key, if any.
*/
[[nodiscard]] inline meta_associative_container::iterator meta_associative_container::find(meta_any key) {
return find_fn(*ctx, storage.data(), std::as_const(storage).data(), key);
return find_fn(*ctx, std::as_const(storage).data(), (storage.policy() == any_policy::cref), key);
}

/**
Expand Down

0 comments on commit 6257cc8

Please sign in to comment.