Commit e6c97c5e authored by Staffan Gimåker's avatar Staffan Gimåker

Reduce memory usage of mtrie.

Signed-off-by: 's avatarStaffan Gimåker <staffan@spotify.com>
parent 7e8a839a
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "mtrie.hpp" #include "mtrie.hpp"
zmq::mtrie_t::mtrie_t () : zmq::mtrie_t::mtrie_t () :
pipes (0),
min (0), min (0),
count (0), count (0),
live_nodes (0) live_nodes (0)
...@@ -42,6 +43,11 @@ zmq::mtrie_t::mtrie_t () : ...@@ -42,6 +43,11 @@ zmq::mtrie_t::mtrie_t () :
zmq::mtrie_t::~mtrie_t () zmq::mtrie_t::~mtrie_t ()
{ {
if (pipes) {
delete pipes;
pipes = 0;
}
if (count == 1) { if (count == 1) {
zmq_assert (next.node); zmq_assert (next.node);
delete next.node; delete next.node;
...@@ -65,8 +71,10 @@ bool zmq::mtrie_t::add_helper (unsigned char *prefix_, size_t size_, ...@@ -65,8 +71,10 @@ bool zmq::mtrie_t::add_helper (unsigned char *prefix_, size_t size_,
{ {
// We are at the node corresponding to the prefix. We are done. // We are at the node corresponding to the prefix. We are done.
if (!size_) { if (!size_) {
bool result = pipes.empty (); bool result = !pipes;
pipes.insert (pipe_); if (!pipes)
pipes = new pipes_t;
pipes->insert (pipe_);
return result; return result;
} }
...@@ -154,8 +162,11 @@ void zmq::mtrie_t::rm_helper (pipe_t *pipe_, unsigned char **buff_, ...@@ -154,8 +162,11 @@ void zmq::mtrie_t::rm_helper (pipe_t *pipe_, unsigned char **buff_,
void *arg_) void *arg_)
{ {
// Remove the subscription from this node. // Remove the subscription from this node.
if (pipes.erase (pipe_) && pipes.empty ()) if (pipes && pipes->erase (pipe_) && pipes->empty ()) {
func_ (*buff_, buffsize_, arg_); func_ (*buff_, buffsize_, arg_);
delete pipes;
pipes = 0;
}
// Adjust the buffer. // Adjust the buffer.
if (buffsize_ >= maxbuffsize_) { if (buffsize_ >= maxbuffsize_) {
...@@ -207,9 +218,15 @@ bool zmq::mtrie_t::rm_helper (unsigned char *prefix_, size_t size_, ...@@ -207,9 +218,15 @@ bool zmq::mtrie_t::rm_helper (unsigned char *prefix_, size_t size_,
pipe_t *pipe_) pipe_t *pipe_)
{ {
if (!size_) { if (!size_) {
pipes_t::size_type erased = pipes.erase (pipe_); if (pipes) {
zmq_assert (erased == 1); pipes_t::size_type erased = pipes->erase (pipe_);
return pipes.empty (); zmq_assert (erased == 1);
if (pipes->empty ()) {
delete pipes;
pipes = 0;
}
}
return !pipes;
} }
unsigned char c = *prefix_; unsigned char c = *prefix_;
...@@ -245,9 +262,11 @@ void zmq::mtrie_t::match (unsigned char *data_, size_t size_, ...@@ -245,9 +262,11 @@ void zmq::mtrie_t::match (unsigned char *data_, size_t size_,
while (true) { while (true) {
// Signal the pipes attached to this node. // Signal the pipes attached to this node.
for (pipes_t::iterator it = current->pipes.begin (); if (current->pipes) {
it != current->pipes.end (); ++it) for (pipes_t::iterator it = current->pipes->begin ();
func_ (*it, arg_); it != current->pipes->end (); ++it)
func_ (*it, arg_);
}
// If we are at the end of the message, there's nothing more to match. // If we are at the end of the message, there's nothing more to match.
if (!size_) if (!size_)
...@@ -281,5 +300,5 @@ void zmq::mtrie_t::match (unsigned char *data_, size_t size_, ...@@ -281,5 +300,5 @@ void zmq::mtrie_t::match (unsigned char *data_, size_t size_,
bool zmq::mtrie_t::is_redundant () const bool zmq::mtrie_t::is_redundant () const
{ {
return pipes.empty () && live_nodes == 0; return !pipes && live_nodes == 0;
} }
...@@ -73,7 +73,7 @@ namespace zmq ...@@ -73,7 +73,7 @@ namespace zmq
bool is_redundant () const; bool is_redundant () const;
typedef std::set <zmq::pipe_t*> pipes_t; typedef std::set <zmq::pipe_t*> pipes_t;
pipes_t pipes; pipes_t *pipes;
unsigned char min; unsigned char min;
unsigned short count; unsigned short count;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment