Skip to content

Commit

Permalink
Merge pull request #27 from psiha/feature/tlc_n_optimizations
Browse files Browse the repository at this point in the history
Feature/tlc n optimizations
  • Loading branch information
psiha authored Dec 17, 2024
2 parents 075e09a + db2b107 commit b342225
Show file tree
Hide file tree
Showing 13 changed files with 392 additions and 161 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ include( ${CMAKE_CURRENT_BINARY_DIR}/cmake/get_cpm.cmake )

# Add packages

set( boost_ver boost-1.86.0 )
set( boost_ver boost-1.87.0 )
CPMAddPackage( "gh:boostorg/static_assert#${boost_ver}" ) # Boost::core dependency
CPMAddPackage( "gh:boostorg/throw_exception#${boost_ver}" ) # Boost::core dependency
CPMAddPackage( "gh:boostorg/config#${boost_ver}" ) # Boost::core dependency
Expand Down
2 changes: 1 addition & 1 deletion include/psi/vm/align.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace align_detail
[[ using gnu: const, always_inline ]] constexpr auto is_aligned( auto const value, auto const alignment ) noexcept { return __builtin_is_aligned( value, alignment ); }
#else
[[ using gnu: const, always_inline ]] constexpr auto is_aligned( auto const value, auto const alignment ) noexcept { return value % alignment == 0; }
[[ using gnu: const, always_inline ]] constexpr auto is_aligned( auto * const ptr , auto const alignment ) noexcept { return is_aligned( reinterpret_cast<std::uintptr_t>( ptr ), alignment ); }
[[ using gnu: const, always_inline ]] constexpr auto is_aligned( auto * const ptr , auto const alignment ) noexcept { return is_aligned( std::bit_cast<std::uintptr_t>( ptr ), alignment ); }
#endif
[[ using gnu: const, always_inline ]] constexpr auto align_down( auto const value, auto const alignment ) noexcept
{
Expand Down
2 changes: 1 addition & 1 deletion include/psi/vm/containers/allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace psi::vm
{
//------------------------------------------------------------------------------

namespace detail { [[ noreturn, gnu::cold ]] inline void throw_bad_alloc() { throw std::bad_alloc(); } }
namespace detail { [[ noreturn, gnu::cold ]] void throw_bad_alloc() PSI_NOEXCEPT_EXCEPT_BADALLOC; }

class allocator_backing_mapping
{
Expand Down
18 changes: 3 additions & 15 deletions include/psi/vm/containers/b+tree.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "vector.hpp"
#include "vm_vector.hpp"

#include <psi/vm/align.hpp>
#include <psi/vm/allocation.hpp>
Expand Down Expand Up @@ -36,18 +36,6 @@ namespace psi::vm
PSI_WARNING_DISABLE_PUSH()
PSI_WARNING_MSVC_DISABLE( 5030 ) // unrecognized attribute

namespace detail
{
template <typename Header>
[[ gnu::const ]] auto header_data( std::span<std::byte> const hdr_storage ) noexcept
{
auto const data { align_up<alignof( Header )>( hdr_storage.data() ) };
auto const remaining_space{ hdr_storage.size() - unsigned( data - hdr_storage.data() ) };
BOOST_ASSERT( remaining_space >= sizeof( Header ) );
return std::pair{ reinterpret_cast<Header *>( data ), std::span{ data + sizeof( Header ), remaining_space - sizeof( Header ) } };
}
} // namespace detail

template <typename K, bool transparent_comparator, typename StoredKeyType>
concept LookupType = transparent_comparator || std::is_same_v<StoredKeyType, K>;

Expand Down Expand Up @@ -212,7 +200,7 @@ class bptree_base
depth_t depth_{};
}; // struct header

using node_pool = vm::vector<node_placeholder, node_slot::value_type, false>;
using node_pool = vm::vm_vector<node_placeholder, node_slot::value_type, false>;

protected:
void swap( bptree_base & other ) noexcept;
Expand Down Expand Up @@ -372,7 +360,7 @@ class bptree_base
[[ nodiscard ]] N & new_node() { return as<N>( new_node() ); }

private:
auto header_data() noexcept { return detail::header_data<header>( nodes_.user_header_data() ); }
auto header_data() noexcept { return vm::header_data<header>( nodes_.user_header_data() ); }

void assign_nodes_to_free_pool( node_slot::value_type starting_node ) noexcept;

Expand Down
16 changes: 9 additions & 7 deletions include/psi/vm/containers/crt_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <psi/vm/containers/vector_impl.hpp>

#include <boost/assert.hpp>
#include <boost/container/detail/allocation_type.hpp>
#include <boost/container/detail/version_type.hpp>

#include <cstdlib>

Expand Down Expand Up @@ -57,7 +59,7 @@ namespace detail
{
# if defined( _MSC_VER )
if constexpr ( alignment > guaranteed_alignment )
return _aligned_msize( const_cast<void *>( address ) );
return _aligned_msize( const_cast<void *>( address ), alignment, 0 );
# endif
return crt_alloc_size( address );
}
Expand Down Expand Up @@ -390,25 +392,25 @@ class [[ nodiscard, clang::trivial_abi ]] crt_vector

public:
using base::base;
crt_vector() noexcept : p_array_{ nullptr }, size_{ 0 }, capacity_{ 0 } {}
explicit crt_vector( crt_vector const & other )
constexpr crt_vector() noexcept : p_array_{ nullptr }, size_{ 0 }, capacity_{ 0 } {}
constexpr explicit crt_vector( crt_vector const & other )
{
auto const data{ storage_init( other.size() ) };
try { std::uninitialized_copy_n( other.data(), other.size(), data ); }
catch(...) { al::deallocate( data, capacity() ); throw; }
}
crt_vector( crt_vector && other ) noexcept : p_array_{ other.p_array_ }, size_{ other.size_ }, capacity_{ other.capacity_ } { other.mark_freed(); }
constexpr crt_vector( crt_vector && other ) noexcept : p_array_{ other.p_array_ }, size_{ other.size_ }, capacity_{ other.capacity_ } { other.mark_freed(); }

crt_vector & operator=( crt_vector const & other ) { *this = crt_vector( other ); }
crt_vector & operator=( crt_vector && other ) noexcept( std::is_nothrow_move_constructible_v<T> )
constexpr crt_vector & operator=( crt_vector const & other ) { *this = crt_vector( other ); }
constexpr crt_vector & operator=( crt_vector && other ) noexcept( std::is_nothrow_move_constructible_v<T> )
{
std::swap( this->p_array_ , other.p_array_ );
std::swap( this->size_ , other.size_ );
std::swap( this->capacity_, other.capacity_ );
other.free();
return *this;
}
~crt_vector() noexcept { free(); }
constexpr ~crt_vector() noexcept { free(); }

[[ nodiscard, gnu::pure ]] size_type size () const noexcept { return size_; }
[[ nodiscard, gnu::pure ]] size_type capacity() const noexcept
Expand Down
6 changes: 3 additions & 3 deletions include/psi/vm/containers/static_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ union noninitialized_array
T data[ size ];
}; // noninitialized_array

struct assert_on_overflow {
[[ noreturn ]] static void operator()() noexcept {
struct assert_on_overflow { // VS17.12.3 MSVC still does not support static operator()
[[ noreturn ]] void operator()() const noexcept {
BOOST_ASSERT_MSG( false, "Static vector overflow!" );
std::unreachable();
}
}; // assert_on_overflow
struct throw_on_overflow {
[[ noreturn ]] static void operator()() { detail::throw_out_of_range(); }
[[ noreturn ]] void operator()() const { detail::throw_out_of_range(); }
}; // throw_on_overflow

template <typename T, std::uint32_t maximum_size, auto overflow_handler = assert_on_overflow{}>
Expand Down
31 changes: 18 additions & 13 deletions include/psi/vm/containers/vector_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ namespace psi::vm
namespace detail
{
[[ noreturn, gnu::cold ]] void throw_out_of_range();
#if PSI_MALLOC_OVERCOMMIT != PSI_OVERCOMMIT_Full
[[ noreturn, gnu::cold ]] void throw_bad_alloc ();
#else
[[ gnu::cold ]] inline void throw_bad_alloc() noexcept
{
BOOST_ASSERT_MSG( false, "Unexpected allocation failure" );
std::unreachable();
}
#endif

template <typename T>
constexpr T * mutable_iter( T const * const ptr ) noexcept { return const_cast<T *>( ptr ); }
Expand Down Expand Up @@ -217,7 +225,7 @@ class [[ nodiscard, clang::trivial_abi ]] vector_impl
{
if constexpr ( std::random_access_iterator<It> )
{
auto const sz{ std::distance( first, last ) };
auto const sz{ static_cast<size_type>( std::distance( first, last ) ) };
auto & impl{ initialized_impl( sz, no_init ) };
// STL utility functions handle EH safety - no need to catch to
// reset size as Impl/the derived class should not attempt cleanup
Expand Down Expand Up @@ -335,14 +343,14 @@ class [[ nodiscard, clang::trivial_abi ]] vector_impl
//! of the reversed vector.
//! <b>Throws</b>: Nothing.
//! <b>Complexity</b>: Constant.
[[ nodiscard ]] auto rbegin( this auto & self ) noexcept { return std::make_reverse_iterator( self.begin() ); }
[[ nodiscard ]] auto rbegin( this auto & self ) noexcept { return std::make_reverse_iterator( self.end() ); }
[[ nodiscard ]] const_reverse_iterator crbegin( this Impl const & self ) noexcept { return self.rbegin(); }

//! <b>Effects</b>: Returns a reverse_iterator pointing to the end
//! of the reversed vector.
//! <b>Throws</b>: Nothing.
//! <b>Complexity</b>: Constant.
[[ nodiscard ]] auto rend( this auto & self ) noexcept { return std::make_reverse_iterator( self.end() ); }
[[ nodiscard ]] auto rend( this auto & self ) noexcept { return std::make_reverse_iterator( self.begin() ); }
[[ nodiscard ]] const_reverse_iterator crend( this Impl const & self ) noexcept { return self.rend(); }

//////////////////////////////////////////////
Expand Down Expand Up @@ -751,16 +759,6 @@ class [[ nodiscard, clang::trivial_abi ]] vector_impl
self.free();
}

//! <b>Effects</b>: Returns true if x and y are equal
//!
//! <b>Complexity</b>: Linear to the number of elements in the container.
[[ nodiscard ]] bool operator==( this auto const & self, std::ranges::range auto const & other ) noexcept { return std::equal( self.begin(), self.end(), other.begin(), other.end() ); }

//! <b>Effects</b>: Returns true if x and y are unequal
//!
//! <b>Complexity</b>: Linear to the number of elements in the container.
[[ nodiscard ]] bool operator!=( this auto const & self, std::ranges::range auto const & other ) noexcept { return !(self == other); }

void swap( this auto & self, auto & other ) noexcept { std::swap( self, other ); }


Expand Down Expand Up @@ -873,6 +871,13 @@ class [[ nodiscard, clang::trivial_abi ]] vector_impl
}
}; // class vector_impl


//! <b>Effects</b>: Returns the result of std::lexicographical_compare_three_way
//!
//! <b>Complexity</b>: Linear to the number of elements in the container.
[[ nodiscard ]] constexpr auto operator<=>( std::ranges::range auto const & left, std::ranges::range auto const & right ) noexcept { return std::lexicographical_compare_three_way( left.begin(), left.end(), right.begin(), right.end() ); }
[[ nodiscard ]] constexpr auto operator== ( std::ranges::range auto const & left, std::ranges::range auto const & right ) noexcept { return std::equal ( left.begin(), left.end(), right.begin(), right.end() ); }

//------------------------------------------------------------------------------
} // namespace psi::vm
//------------------------------------------------------------------------------
Loading

0 comments on commit b342225

Please sign in to comment.