Skip to content

Commit

Permalink
Make IsWall easier for the compiler to inline
Browse files Browse the repository at this point in the history
This appears to be a very hot function.
Makes it fully inlineable (even in Debug mode).
  • Loading branch information
glebm committed Sep 18, 2023
1 parent 058a0d1 commit e5c426c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Source/engine/render/scrollrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ void DrawFloor(const Surface &out, Point tilePosition, Point targetBufferPositio
}
}

bool IsWall(Point position)
[[nodiscard]] DVL_ALWAYS_INLINE bool IsWall(Point position)
{
return TileHasAny(dPiece[position.x][position.y], TileProperties::Solid) || dSpecial[position.x][position.y] != 0;
}
Expand Down
7 changes: 1 addition & 6 deletions Source/levels/gendung.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ std::unique_ptr<uint16_t[]> pSetPiece;
OptionalOwnedClxSpriteList pSpecialCels;
std::unique_ptr<MegaTile[]> pMegaTiles;
std::unique_ptr<std::byte[]> pDungeonCels;
std::array<TileProperties, MAXTILES> SOLData;
TileProperties SOLData[MAXTILES];
WorldTilePosition dminPosition;
WorldTilePosition dmaxPosition;
dungeon_type leveltype;
Expand Down Expand Up @@ -417,11 +417,6 @@ void CreateDungeon(uint32_t rseed, lvl_entry entry)
Make_SetPC(SetPiece);
}

bool TileHasAny(int tileId, TileProperties property)
{
return HasAnyOf(SOLData[tileId], property);
}

void LoadLevelSOLData()
{
switch (leveltype) {
Expand Down
8 changes: 6 additions & 2 deletions Source/levels/gendung.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ extern std::unique_ptr<std::byte[]> pDungeonCels;
/**
* List tile properties
*/
extern DVL_API_FOR_TEST std::array<TileProperties, MAXTILES> SOLData;
extern DVL_API_FOR_TEST TileProperties SOLData[MAXTILES];
/** Specifies the minimum X,Y-coordinates of the map. */
extern WorldTilePosition dminPosition;
/** Specifies the maximum X,Y-coordinates of the map. */
Expand Down Expand Up @@ -334,7 +334,11 @@ struct Miniset {
}
};

bool TileHasAny(int tileId, TileProperties property);
[[nodiscard]] DVL_ALWAYS_INLINE bool TileHasAny(int tileId, TileProperties property)
{
return HasAnyOf(SOLData[tileId], property);
}

void LoadLevelSOLData();
void SetDungeonMicros();
void DRLG_InitTrans();
Expand Down
28 changes: 15 additions & 13 deletions Source/utils/enum_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <cstddef>
#include <type_traits>

#include "utils/attributes.h"

namespace devilution {

template <typename T>
Expand All @@ -27,31 +29,31 @@ class enum_values {
{
}

const T operator*() const
[[nodiscard]] DVL_ALWAYS_INLINE const T operator*() const
{
return static_cast<T>(m_value);
}

void operator++()
DVL_ALWAYS_INLINE void operator++()
{
m_value++;
}

bool operator!=(Iterator rhs) const
[[nodiscard]] DVL_ALWAYS_INLINE bool operator!=(Iterator rhs) const
{
return m_value != rhs.m_value;
}
};
};

template <typename T>
typename enum_values<T>::Iterator begin(enum_values<T>)
[[nodiscard]] DVL_ALWAYS_INLINE typename enum_values<T>::Iterator begin(enum_values<T>)
{
return typename enum_values<T>::Iterator(static_cast<typename std::underlying_type<T>::type>(T::FIRST));
}

template <typename T>
typename enum_values<T>::Iterator end(enum_values<T>)
[[nodiscard]] DVL_ALWAYS_INLINE typename enum_values<T>::Iterator end(enum_values<T>)
{
return typename enum_values<T>::Iterator(static_cast<typename std::underlying_type<T>::type>(T::LAST) + 1);
}
Expand All @@ -66,54 +68,54 @@ struct is_flags_enum : std::false_type {
};

template <typename EnumType, std::enable_if_t<std::is_enum<EnumType>::value && is_flags_enum<EnumType>::value, bool> = true>
constexpr EnumType operator|(EnumType lhs, EnumType rhs)
[[nodiscard]] DVL_ALWAYS_INLINE constexpr EnumType operator|(EnumType lhs, EnumType rhs)
{
using T = std::underlying_type_t<EnumType>;
return static_cast<EnumType>(static_cast<T>(lhs) | static_cast<T>(rhs));
}

template <typename EnumType, std::enable_if_t<std::is_enum<EnumType>::value && is_flags_enum<EnumType>::value, bool> = true>
constexpr EnumType operator|=(EnumType &lhs, EnumType rhs)
DVL_ALWAYS_INLINE constexpr EnumType operator|=(EnumType &lhs, EnumType rhs)
{
lhs = lhs | rhs;
return lhs;
}

template <typename EnumType, std::enable_if_t<std::is_enum<EnumType>::value && is_flags_enum<EnumType>::value, bool> = true>
constexpr EnumType operator&(EnumType lhs, EnumType rhs)
[[nodiscard]] DVL_ALWAYS_INLINE constexpr EnumType operator&(EnumType lhs, EnumType rhs)
{
using T = std::underlying_type_t<EnumType>;
return static_cast<EnumType>(static_cast<T>(lhs) & static_cast<T>(rhs));
}

template <typename EnumType, std::enable_if_t<std::is_enum<EnumType>::value && is_flags_enum<EnumType>::value, bool> = true>
constexpr EnumType operator&=(EnumType &lhs, EnumType rhs)
DVL_ALWAYS_INLINE constexpr EnumType operator&=(EnumType &lhs, EnumType rhs)
{
lhs = lhs & rhs;
return lhs;
}

template <typename EnumType, std::enable_if_t<std::is_enum<EnumType>::value && is_flags_enum<EnumType>::value, bool> = true>
constexpr EnumType operator~(EnumType value)
[[nodiscard]] DVL_ALWAYS_INLINE constexpr EnumType operator~(EnumType value)
{
using T = std::underlying_type_t<EnumType>;
return static_cast<EnumType>(~static_cast<T>(value));
}

template <typename EnumType, std::enable_if_t<std::is_enum<EnumType>::value && is_flags_enum<EnumType>::value, bool> = true>
constexpr bool HasAnyOf(EnumType lhs, EnumType test)
[[nodiscard]] DVL_ALWAYS_INLINE constexpr bool HasAnyOf(EnumType lhs, EnumType test)
{
return (lhs & test) != static_cast<EnumType>(0); // Some flags enums may not use a None value outside this check so we don't require an EnumType::None definition here.
}

template <typename EnumType, std::enable_if_t<std::is_enum<EnumType>::value && is_flags_enum<EnumType>::value, bool> = true>
constexpr bool HasAllOf(EnumType lhs, EnumType test)
[[nodiscard]] DVL_ALWAYS_INLINE constexpr bool HasAllOf(EnumType lhs, EnumType test)
{
return (lhs & test) == test;
}

template <typename EnumType, std::enable_if_t<std::is_enum<EnumType>::value && is_flags_enum<EnumType>::value, bool> = true>
constexpr bool HasNoneOf(EnumType lhs, EnumType test)
[[nodiscard]] DVL_ALWAYS_INLINE constexpr bool HasNoneOf(EnumType lhs, EnumType test)
{
return !HasAnyOf(lhs, test);
}
Expand Down

0 comments on commit e5c426c

Please sign in to comment.