From 5b26d5d9629d4ed2755a42cfb6b07b2ca1217fde Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 12 Nov 2012 09:49:00 +0000 Subject: [PATCH] merged utp_socket_manager optimization from libtorrent_aio --- include/libtorrent/torrent.hpp | 1 + include/libtorrent/utp_socket_manager.hpp | 4 ++++ src/utp_socket_manager.cpp | 14 ++++++++++---- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 8f5ff0ade..923fb9501 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -1033,6 +1033,7 @@ namespace libtorrent }; // this list is sorted by time_critical_piece::deadline + // TODO: this should be a deque std::list m_time_critical_pieces; std::string m_trackerid; diff --git a/include/libtorrent/utp_socket_manager.hpp b/include/libtorrent/utp_socket_manager.hpp index 2ebf2ad13..88d75eb21 100644 --- a/include/libtorrent/utp_socket_manager.hpp +++ b/include/libtorrent/utp_socket_manager.hpp @@ -128,6 +128,10 @@ namespace libtorrent // the routing table mutable ptime m_last_route_update; + // cache of interfaces + mutable std::vector m_interfaces; + mutable ptime m_last_if_update; + // the buffer size of the socket. This is used // to now lower the buffer size int m_sock_buf_size; diff --git a/src/utp_socket_manager.cpp b/src/utp_socket_manager.cpp index 55833497a..e7f0b3d05 100644 --- a/src/utp_socket_manager.cpp +++ b/src/utp_socket_manager.cpp @@ -51,6 +51,7 @@ namespace libtorrent , m_new_connection(-1) , m_sett(sett) , m_last_route_update(min_time()) + , m_last_if_update(min_time()) , m_sock_buf_size(0) {} @@ -235,11 +236,16 @@ namespace libtorrent // for this target. Now figure out what the local address // is for that interface - std::vector net = enum_net_interfaces(m_sock.get_io_service(), ec); - if (ec) return socket_ep; + if (time_now() - m_last_if_update > seconds(60)) + { + m_last_if_update = time_now(); + error_code ec; + m_interfaces = enum_net_interfaces(m_sock.get_io_service(), ec); + if (ec) return socket_ep; + } - for (std::vector::iterator i = net.begin() - , end(net.end()); i != end; ++i) + for (std::vector::iterator i = m_interfaces.begin() + , end(m_interfaces.end()); i != end; ++i) { if (i->interface_address.is_v4() != remote.is_v4()) continue;