Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize typed coordinate conversion functions for faster Windows unsafe_get_submap_at performance #75376

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/cata_inline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#ifndef CATA_SRC_CATA_INLINE_H
#define CATA_SRC_CATA_INLINE_H

#ifndef CATA_FORCEINLINE
#ifdef _MSC_VER
#define CATA_FORCEINLINE __forceinline
#else
#define CATA_FORCEINLINE inline __attribute__((always_inline))
#endif
#endif

#endif // CATA_SRC_CATA_INLINE_H
594 changes: 371 additions & 223 deletions src/coordinates.h

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion src/coords_fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#ifndef CATA_SRC_COORDS_FWD_H
#define CATA_SRC_COORDS_FWD_H

#include <type_traits>

struct point;
struct tripoint;

Expand Down Expand Up @@ -32,8 +34,15 @@ constexpr scale omt = scale::overmap_terrain;
constexpr scale seg = scale::segment;
constexpr scale om = scale::overmap;

template<typename Point, origin Origin, scale Scale>
class coord_point_ob;

template<typename Point, origin Origin, scale Scale>
class coord_point_ib;

template<typename Point, origin Origin, scale Scale, bool InBounds = false>
class coord_point;
using coord_point =
std::conditional_t<InBounds, coord_point_ib<Point, Origin, Scale>, coord_point_ob<Point, Origin, Scale>>;

} // namespace coords

Expand Down
4 changes: 2 additions & 2 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1805,7 +1805,7 @@ furn_id map::furn( const tripoint &p ) const
return furn( tripoint_bub_ms( p ) );
}

furn_id map::furn( const tripoint_bub_ms &p ) const
furn_id map::furn( const tripoint_bub_ms p ) const
{
if( !inbounds( p ) ) {
return furn_str_id::NULL_ID();
Expand Down Expand Up @@ -2000,7 +2000,7 @@ std::string map::furnname( const tripoint_bub_ms &p )
* retained for high performance comparisons, save/load, and gradual transition
* to string terrain.id
*/
ter_id map::ter( const tripoint &p ) const
ter_id map::ter( const tripoint p ) const
{
if( !inbounds( p ) ) {
return ter_str_id::NULL_ID().id();
Expand Down
16 changes: 6 additions & 10 deletions src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ class map
}
// TODO: fix point types (remove the first overload)
furn_id furn( const tripoint &p ) const;
furn_id furn( const tripoint_bub_ms &p ) const;
furn_id furn( tripoint_bub_ms p ) const;
// TODO: Get rid of untyped overload.
furn_id furn( const point_bub_ms &p ) const {
return furn( tripoint_bub_ms( p, abs_sub.z() ) );
Expand Down Expand Up @@ -1010,7 +1010,7 @@ class map

// Terrain
// TODO: fix point types (remove the first overload)
ter_id ter( const tripoint &p ) const;
ter_id ter( tripoint p ) const;
ter_id ter( const tripoint_bub_ms &p ) const;
// TODO: Get rid of untyped overload.
ter_id ter( const point &p ) const {
Expand Down Expand Up @@ -2399,11 +2399,9 @@ class map
offset_p.y = p.y % SEEY;
return unsafe_get_submap_at( p );
}
submap *unsafe_get_submap_at( const tripoint_bub_ms &p, point_sm_ms &offset_p ) {
submap *unsafe_get_submap_at( const tripoint_bub_ms p, point_sm_ms &offset_p ) {
tripoint_bub_sm sm;
point_sm_ms_ib l;
std::tie( sm, l ) = project_remain<coords::sm>( p );
offset_p = point_sm_ms( l );
std::tie( sm, offset_p ) = project_remain<coords::sm>( p );
return unsafe_get_submap_at( p );
}
// TODO: fix point types (remove the first overload)
Expand All @@ -2413,11 +2411,9 @@ class map
return unsafe_get_submap_at( p );
}
const submap *unsafe_get_submap_at(
const tripoint_bub_ms &p, point_sm_ms &offset_p ) const {
const tripoint_bub_ms p, point_sm_ms &offset_p ) const {
tripoint_bub_sm sm;
point_sm_ms_ib l;
std::tie( sm, l ) = project_remain<coords::sm>( p );
offset_p = point_sm_ms( l );
std::tie( sm, offset_p ) = project_remain<coords::sm>( p );
return unsafe_get_submap_at( p );
}
// TODO: Get rid of untyped overload
Expand Down
8 changes: 4 additions & 4 deletions src/mdarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ namespace cata
template<typename Point>
struct mdarray_default_size_impl;

template<typename Point, coords::scale Scale, bool InBounds>
struct mdarray_default_size_impl<coords::coord_point<Point, coords::origin::reality_bubble, Scale, InBounds>> {
template<template<class, coords::origin, coords::scale> class coord_point, typename Point, coords::scale Scale>
struct mdarray_default_size_impl<coord_point<Point, coords::origin::reality_bubble, Scale>> {
static constexpr size_t value = MAPSIZE_X / map_squares_per( Scale );
static_assert( MAPSIZE_X % map_squares_per( Scale ) == 0, "Scale must be smaller than map" );
};

template<typename Point, coords::origin Origin, coords::scale Scale, bool InBounds>
struct mdarray_default_size_impl<coords::coord_point<Point, Origin, Scale, InBounds>> {
template<template<class, coords::origin, coords::scale> class coord_point, typename Point, coords::origin Origin, coords::scale Scale>
struct mdarray_default_size_impl<coord_point<Point, Origin, Scale>> {
static constexpr coords::scale outer_scale = coords::scale_from_origin( Origin );
static constexpr size_t value = map_squares_per( outer_scale ) / map_squares_per( Scale );
static_assert( value > 0, "Scale must be smaller origin" );
Expand Down
2 changes: 1 addition & 1 deletion src/omdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ std::string name( type dir );
point rotate( const point &p, type dir );
tripoint rotate( const tripoint &p, type dir );
template<typename Point, coords::scale Scale>
auto rotate( const coords::coord_point<Point, coords::origin::relative, Scale> &p, type dir )
auto rotate( const coords::coord_point_ob<Point, coords::origin::relative, Scale> &p, type dir )
-> coords::coord_point<Point, coords::origin::relative, Scale>
{
return coords::coord_point<Point, coords::origin::relative, Scale> { rotate( p.raw(), dir ) };
Expand Down
13 changes: 6 additions & 7 deletions tests/coordinate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ static_assert( tripoint_abs_omt::dimension == 3 );

// Out of bounds coords can be implicitly constructed from inbounds ones. This is used
// to ensure a return type is NOT an inbounds one, before it can be converted.
template <typename Point, coords::origin Origin, coords::scale Scale, bool InBounds>
coords::coord_point<Point, Origin, Scale, InBounds> assert_not_ib( const
coords::coord_point<Point, Origin, Scale, InBounds> &p )
{
static_assert( !InBounds );
return p;
}
#define assert_not_ib(p) \
(([](auto&& _p) { \
using _t = decltype(_p); \
static_assert(!std::decay_t<_t>::is_inbounds); \
return std::forward<_t>(_p); } \
)(p))

TEST_CASE( "coordinate_strings", "[point][coords][nogame]" )
{
Expand Down
Loading