Skip to content

Commit

Permalink
[exec] Improve allocations and fix that the graph_edge pool was using…
Browse files Browse the repository at this point in the history
… ridiculous amounts of memory for no reason
  • Loading branch information
jcelerier committed Apr 26, 2023
1 parent d091290 commit a6606aa
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 61 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/SmallFunction
14 changes: 13 additions & 1 deletion src/ossia/dataflow/graph/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@
#include <ossia/dataflow/graph/tick_setup.hpp>
#include <ossia/network/value/format_value.hpp>

#include <spdlog/spdlog.h>
#include <boost/pool/pool.hpp>

#include <spdlog/spdlog.h>
namespace ossia
{
using boost_pool = boost::pool<boost::default_user_allocator_malloc_free>;

struct edge_pool
{
boost_pool pool{graph_edge::size_of_allocated_memory_by_make_shared()};
ossia::audio_spin_mutex execution_storage_mut;
};

template <typename T>
struct edge_pool_alloc
{
Expand Down Expand Up @@ -68,6 +77,9 @@ struct edge_pool_alloc
graph_interface::graph_interface()
: pool{std::make_shared<edge_pool>()}
{
// Reserve some space
auto arr = pool->pool.ordered_malloc(1000);
pool->pool.ordered_free(arr);
}

edge_ptr graph_interface::allocate_edge(
Expand Down
17 changes: 4 additions & 13 deletions src/ossia/dataflow/graph/graph_interface.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#pragma once
#include <ossia/dataflow/dataflow_fwd.hpp>
#include <ossia/dataflow/graph_edge.hpp>
#include <ossia/dataflow/transport.hpp>
#include <ossia/detail/audio_spin_mutex.hpp>
#include <ossia/detail/logger_fwd.hpp>

#include <boost/pool/pool.hpp>

#include <tcb/span.hpp>

#include <smallfun.hpp>
Expand All @@ -16,15 +15,7 @@ class logger;
}
namespace ossia
{

using boost_pool = boost::pool<boost::default_user_allocator_malloc_free>;

struct edge_pool
{
boost_pool pool{1024 * 1024};
ossia::audio_spin_mutex execution_storage_mut;
};

struct edge_pool;
struct bench_map;
struct connection;
class time_interval;
Expand All @@ -51,8 +42,8 @@ class OSSIA_EXPORT graph_interface

virtual void print(std::ostream&) = 0;

[[nodiscard]] virtual tcb::span<ossia::graph_node* const>
get_nodes() const noexcept = 0;
[[nodiscard]] virtual tcb::span<ossia::graph_node* const> get_nodes() const noexcept
= 0;

std::shared_ptr<edge_pool> pool;

Expand Down
3 changes: 2 additions & 1 deletion src/ossia/dataflow/graph/graph_parallel_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class executor

std::atomic_thread_fence(std::memory_order_seq_cst);
#if defined(DISABLE_DONE_TASKS)
ossia::small_pod_vector<ossia::task*, 8> toCleanup;
thread_local ossia::small_pod_vector<ossia::task*, 8> toCleanup;
#endif
for(auto& task : tf.m_tasks)
{
Expand Down Expand Up @@ -205,6 +205,7 @@ class executor
{
process_done(*task);
}
toCleanup.clear();
#endif

while(m_doneTasks.load(std::memory_order_relaxed) != m_toDoTasks)
Expand Down
3 changes: 2 additions & 1 deletion src/ossia/dataflow/graph/graph_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#include <ossia/detail/ptr_set.hpp>
#include <ossia/editor/scenario/time_value.hpp>

#include <boost/predef.h>
#include <boost/graph/adjacency_list.hpp>
#include <boost/predef.h>
// broken due to dynamic_property_map requiring rtti...
// #include <boost/graph/graphviz.hpp>
#include <boost/graph/topological_sort.hpp>
Expand Down Expand Up @@ -682,6 +682,7 @@ struct OSSIA_EXPORT graph_base : graph_interface
graph_base() noexcept
{
m_nodes.reserve(1024);
m_node_list.reserve(1024);
m_edges.reserve(1024);
}
[[nodiscard]] tcb::span<ossia::graph_node* const>
Expand Down
9 changes: 6 additions & 3 deletions src/ossia/dataflow/graph_edge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@ struct OSSIA_EXPORT graph_edge
template <typename... Args>
friend std::shared_ptr<ossia::graph_edge> make_edge(Args&&...);
*/
private:
graph_edge() = default;

public:
graph_edge(
connection c, outlet_ptr pout, inlet_ptr pin, node_ptr pout_node,
node_ptr pin_node) noexcept;

void init() noexcept;

~graph_edge();

void init() noexcept;
void clear() noexcept;

static std::size_t size_of_allocated_memory_by_make_shared() noexcept;

connection con{};
outlet_ptr out{};
inlet_ptr in{};
Expand Down
49 changes: 49 additions & 0 deletions src/ossia/dataflow/graph_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,55 @@ graph_edge::graph_edge(
assert(in_node);
}

template <typename T>
struct alloc_observer
{
std::allocator<T> alloc;
std::size_t& request;
template <typename U>
using rebind = alloc_observer<U>;
using value_type = T;

alloc_observer(std::size_t& request) noexcept
: request(request)
{
// Tricky note: std::make/allocate_shared store the allocator. Thus the allocated size depend on alloc_observer / edge_pool_alloc size.
// To get the correct size, we have to make sure that sizeof(alloc_observer) == sizeof(edge_pool_alloc) (which is just a shared_ptr)

static_assert(sizeof(alloc_observer<T>) == sizeof(std::shared_ptr<int>));
}

template <typename U>
alloc_observer(alloc_observer<U> const& other) noexcept
: request(other.request)
{
static_assert(sizeof(alloc_observer<T>) == sizeof(std::shared_ptr<int>));
static_assert(sizeof(alloc_observer<U>) == sizeof(std::shared_ptr<int>));
}

T* allocate(const std::size_t n) noexcept
{
request = sizeof(T);
return alloc.allocate(n);
}
void deallocate(T* ptr, const size_t n) noexcept { return alloc.deallocate(ptr, n); }
};

std::size_t graph_edge::size_of_allocated_memory_by_make_shared() noexcept
{
struct private_bypass : graph_edge
{
public:
private_bypass() = default;
};
std::size_t request{};
alloc_observer<private_bypass> alloc{request};

std::allocate_shared<private_bypass, alloc_observer<private_bypass>>(alloc);

return request;
}

void graph_edge::init() noexcept
{
if(in && out)
Expand Down
2 changes: 2 additions & 0 deletions src/ossia/editor/curve/curve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ class curve final : public curve_abstract
static Y convert_to_template_type_value(
const ossia::value& value, ossia::destination_index::const_iterator idx);

void reserve(std::size_t count) { m_points.reserve(count); }

private:
mutable X m_x0;
mutable Y m_y0;
Expand Down
Loading

0 comments on commit a6606aa

Please sign in to comment.