Skip to content

Commit 6a3ab88

Browse files
authored
Merge pull request #4918 from pwojcikdev/tcp-channel-queue-optimize
Optimize `tcp_channel_queue`
1 parent 71d8bae commit 6a3ab88

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

nano/node/transport/tcp_channel.cpp

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,12 @@ nano::transport::tcp_channel_queue::tcp_channel_queue ()
222222

223223
bool nano::transport::tcp_channel_queue::empty () const
224224
{
225-
return std::all_of (queues.begin (), queues.end (), [] (auto const & queue) {
226-
return queue.second.empty ();
227-
});
225+
return total_size == 0;
228226
}
229227

230228
size_t nano::transport::tcp_channel_queue::size () const
231229
{
232-
return std::accumulate (queues.begin (), queues.end (), size_t{ 0 }, [] (size_t acc, auto const & queue) {
233-
return acc + queue.second.size ();
234-
});
230+
return total_size;
235231
}
236232

237233
size_t nano::transport::tcp_channel_queue::size (traffic_type type) const
@@ -252,7 +248,8 @@ bool nano::transport::tcp_channel_queue::full (traffic_type type) const
252248
void nano::transport::tcp_channel_queue::push (traffic_type type, entry_t entry)
253249
{
254250
debug_assert (!full (type)); // Should be checked before calling this function
255-
queues.at (type).second.push_back (entry);
251+
queues.at (type).second.push_back (std::move (entry));
252+
++total_size;
256253
}
257254

258255
auto nano::transport::tcp_channel_queue::next () -> value_t
@@ -277,6 +274,22 @@ auto nano::transport::tcp_channel_queue::next () -> value_t
277274
return false;
278275
};
279276

277+
auto seek_next = [&, this] () {
278+
counter = 0;
279+
do
280+
{
281+
if (current != queues.end ())
282+
{
283+
++current;
284+
}
285+
if (current == queues.end ())
286+
{
287+
current = queues.begin ();
288+
}
289+
release_assert (current != queues.end ());
290+
} while (current->second.empty ());
291+
};
292+
280293
if (should_seek ())
281294
{
282295
seek_next ();
@@ -288,16 +301,16 @@ auto nano::transport::tcp_channel_queue::next () -> value_t
288301
auto & queue = current->second;
289302

290303
++counter;
304+
--total_size;
291305

292306
release_assert (!queue.empty ());
293-
auto entry = queue.front ();
307+
auto entry = std::move (queue.front ());
294308
queue.pop_front ();
295-
return { source, entry };
309+
return { source, std::move (entry) };
296310
}
297311

298312
auto nano::transport::tcp_channel_queue::next_batch (size_t max_count) -> batch_t
299313
{
300-
// TODO: Naive implementation, could be optimized
301314
std::deque<value_t> result;
302315
while (!empty () && result.size () < max_count)
303316
{
@@ -316,21 +329,4 @@ size_t nano::transport::tcp_channel_queue::priority (traffic_type type) const
316329
default:
317330
return 4;
318331
}
319-
}
320-
321-
void nano::transport::tcp_channel_queue::seek_next ()
322-
{
323-
counter = 0;
324-
do
325-
{
326-
if (current != queues.end ())
327-
{
328-
++current;
329-
}
330-
if (current == queues.end ())
331-
{
332-
current = queues.begin ();
333-
}
334-
release_assert (current != queues.end ());
335-
} while (current->second.empty ());
336-
}
332+
}

nano/node/transport/tcp_channel.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ class tcp_channel_queue final
3333
constexpr static size_t full_size = 4 * max_size;
3434

3535
private:
36-
void seek_next ();
3736
size_t priority (traffic_type) const;
3837

3938
using queue_t = std::pair<traffic_type, std::deque<entry_t>>;
4039
nano::enum_array<traffic_type, queue_t> queues{};
4140
nano::enum_array<traffic_type, queue_t>::iterator current{ queues.end () };
4241
size_t counter{ 0 };
42+
size_t total_size{ 0 };
4343
};
4444

4545
class tcp_channel final : public nano::transport::channel, public std::enable_shared_from_this<tcp_channel>

0 commit comments

Comments
 (0)