From de744ad11de4ede6c2e85096d3be182b97cc6b8b Mon Sep 17 00:00:00 2001 From: rakshasa Date: Mon, 1 Jan 2007 14:57:10 +0000 Subject: [PATCH] * Converted VariableMap and ViewManager to using const char* as the key, since they are never changed nor added from outside. This cut the stripped binary size by 50KB. * Use a shared have piece queue for each download. Each connection has a time-stamp for the last have message they sent, which is checked against the queue. This also avoids the race condition where some peers would get incomplete views of our bitfield due to lost have messages during handshake. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@839 e378c898-3ddf-0310-93e7-cc216c733640 --- rak/functional.h | 22 +++++++++++----------- src/core/download.h | 10 +++++++--- src/core/view_manager.cc | 20 +++++++++----------- src/core/view_manager.h | 9 +++++++-- src/main.cc | 2 +- src/option_handler_rules.cc | 4 ++-- src/utils/variable_map.cc | 20 ++++++++++---------- src/utils/variable_map.h | 37 +++++++++++++++++++++++++------------ 8 files changed, 72 insertions(+), 52 deletions(-) diff --git a/rak/functional.h b/rak/functional.h index 9f26fce8..661175e1 100644 --- a/rak/functional.h +++ b/rak/functional.h @@ -462,15 +462,15 @@ public: typedef Ret (Object::*Function)() const; const_mem_fun0() : m_object(NULL) {} - const_mem_fun0(Object* o, Function f) : m_object(o), m_function(f) {} + const_mem_fun0(const Object* o, Function f) : m_object(o), m_function(f) {} bool is_valid() const { return m_object; } - Ret operator () () { return (m_object->*m_function)(); } + Ret operator () () const { return (m_object->*m_function)(); } private: - Object* m_object; - Function m_function; + const Object* m_object; + Function m_function; }; template @@ -498,15 +498,15 @@ public: typedef Ret (Object::*Function)(Arg1) const; const_mem_fun1() : m_object(NULL) {} - const_mem_fun1(Object* o, Function f) : m_object(o), m_function(f) {} + const_mem_fun1(const Object* o, Function f) : m_object(o), m_function(f) {} bool is_valid() const { return m_object; } - Ret operator () (Arg1 a1) { return (m_object->*m_function)(a1); } + Ret operator () (Arg1 a1) const { return (m_object->*m_function)(a1); } private: - Object* m_object; - Function m_function; + const Object* m_object; + Function m_function; }; template @@ -556,9 +556,9 @@ make_mem_fun(Object* o, Ret (Object::*f)()) { } template -inline const_mem_fun0 +inline const_mem_fun0 make_mem_fun(const Object* o, Ret (Object::*f)() const) { - return const_mem_fun0(o, f); + return const_mem_fun0(o, f); } template @@ -569,7 +569,7 @@ make_mem_fun(Object* o, Ret (Object::*f)(Arg1)) { template inline const_mem_fun1 -make_mem_fun(Object* o, Ret (Object::*f)(Arg1) const) { +make_mem_fun(const Object* o, Ret (Object::*f)(Arg1) const) { return const_mem_fun1(o, f); } diff --git a/src/core/download.h b/src/core/download.h index 9a15b4bb..dea54856 100644 --- a/src/core/download.h +++ b/src/core/download.h @@ -82,9 +82,13 @@ public: bool is_hash_failed() const { return m_hashFailed; } void set_hash_failed(bool v) { m_hashFailed = v; } - variable_map_type* variable() { return &m_variables; } - int64_t variable_value(const std::string& key) const { return m_variables.get_value(key); } - const std::string& variable_string(const std::string& key) const { return m_variables.get_string(key); } + variable_map_type* variable() { return &m_variables; } + + int64_t variable_value(const std::string& key) const { return m_variables.get_value(key.c_str()); } + const std::string& variable_string(const std::string& key) const { return m_variables.get_string(key.c_str()); } + + int64_t variable_value_c(const char* key) const { return m_variables.get_value(key); } + const std::string& variable_string_c(const char* key) const { return m_variables.get_string(key); } download_type* download() { return &m_download; } const download_type* c_download() const { return &m_download; } diff --git a/src/core/view_manager.cc b/src/core/view_manager.cc index 08dc4157..db978471 100644 --- a/src/core/view_manager.cc +++ b/src/core/view_manager.cc @@ -66,8 +66,7 @@ public: class ViewSortVariable : public ViewSort { public: - ViewSortVariable(const std::string& name, const std::string& value) : - m_name(name), m_value(value) {} + ViewSortVariable(const char* name, const char* value) : m_name(name), m_value(value) {} virtual bool operator () (Download* d1, Download* d2) const { return @@ -76,14 +75,13 @@ public: } private: - std::string m_name; - std::string m_value; + const char* m_name; + const char* m_value; }; class ViewSortVariableValue : public ViewSort { public: - ViewSortVariableValue(const std::string& name, bool reverse = false) : - m_name(name), m_reverse(reverse) {} + ViewSortVariableValue(const char* name, bool reverse = false) : m_name(name), m_reverse(reverse) {} virtual bool operator () (Download* d1, Download* d2) const { if (m_reverse) @@ -93,7 +91,7 @@ public: } private: - std::string m_name; + const char* m_name; bool m_reverse; }; @@ -112,7 +110,7 @@ private: class ViewFilterVariableValue : public ViewFilter { public: - ViewFilterVariableValue(const std::string& name, torrent::Object::value_type v, bool inverse = false) : + ViewFilterVariableValue(const char* name, torrent::Object::value_type v, bool inverse = false) : m_name(name), m_value(v), m_inverse(inverse) {} virtual bool operator () (Download* d1) const { @@ -120,7 +118,7 @@ public: } private: - std::string m_name; + const char* m_name; torrent::Object::value_type m_value; bool m_inverse; }; @@ -191,7 +189,7 @@ ViewManager::build_sort_list(const sort_args& args) { sortList.reserve(args.size()); for (sort_args::const_iterator itr = args.begin(), last = args.end(); itr != last; ++itr) { - sort_map::const_iterator sortItr = m_sort.find(*itr); + sort_map::const_iterator sortItr = m_sort.find(itr->c_str()); if (sortItr == m_sort.end()) throw torrent::input_error("Invalid sorting identifier."); @@ -235,7 +233,7 @@ ViewManager::build_filter_list(const filter_args& args) { filterList.reserve(args.size()); for (filter_args::const_iterator itr = args.begin(), last = args.end(); itr != last; ++itr) { - filter_map::const_iterator filterItr = m_filter.find(*itr); + filter_map::const_iterator filterItr = m_filter.find(itr->c_str()); if (filterItr == m_filter.end()) throw torrent::input_error("Invalid filtering identifier."); diff --git a/src/core/view_manager.h b/src/core/view_manager.h index 375172df..9b42cbef 100644 --- a/src/core/view_manager.h +++ b/src/core/view_manager.h @@ -38,6 +38,7 @@ #define RTORRENT_CORE_VIEW_MANAGER_H #include +#include #include #include @@ -45,17 +46,21 @@ namespace core { +struct view_manager_comp : public std::binary_function { + bool operator () (const char* arg1, const char* arg2) const { return std::strcmp(arg1, arg2) < 0; } +}; + class ViewSort; class ViewManager : public rak::unordered_vector { public: typedef rak::unordered_vector base_type; - typedef std::map sort_map; + typedef std::map sort_map; typedef View::sort_list sort_list; typedef std::list sort_args; - typedef std::map filter_map; + typedef std::map filter_map; typedef View::filter_list filter_list; typedef std::list filter_args; diff --git a/src/main.cc b/src/main.cc index 7c7197e1..a377a156 100644 --- a/src/main.cc +++ b/src/main.cc @@ -88,7 +88,7 @@ parse_options(Control* c, int argc, char** argv) { optionParser.insert_option('s', sigc::bind<0>(sigc::mem_fun(c->variable(), &utils::VariableMap::set_string), "session")); optionParser.insert_option('O', sigc::mem_fun(c->variable(), &utils::VariableMap::process_command)); - optionParser.insert_option_list('o', sigc::mem_fun(c->variable(), &utils::VariableMap::set_string)); + optionParser.insert_option_list('o', sigc::mem_fun(c->variable(), &utils::VariableMap::set_std_string)); return optionParser.process(argc, argv); diff --git a/src/option_handler_rules.cc b/src/option_handler_rules.cc index ea80c3d6..05cf14cc 100644 --- a/src/option_handler_rules.cc +++ b/src/option_handler_rules.cc @@ -409,13 +409,13 @@ apply_view_sort_new(Control* control, const std::string& arg) { void apply_import(const std::string& path) { - if (!control->variable()->process_file(path)) + if (!control->variable()->process_file(path.c_str())) throw torrent::input_error("Could not open option file: " + path); } void apply_try_import(const std::string& path) { - if (!control->variable()->process_file(path)) + if (!control->variable()->process_file(path.c_str())) control->core()->push_log("Could not read resource file: " + path); } diff --git a/src/utils/variable_map.cc b/src/utils/variable_map.cc index ee8f36a3..480c14a9 100644 --- a/src/utils/variable_map.cc +++ b/src/utils/variable_map.cc @@ -55,7 +55,7 @@ VariableMap::~VariableMap() { } void -VariableMap::insert(const std::string& key, Variable* v) { +VariableMap::insert(key_type key, Variable* v) { iterator itr = base_type::find(key); if (itr != base_type::end()) @@ -65,23 +65,23 @@ VariableMap::insert(const std::string& key, Variable* v) { } const VariableMap::mapped_type& -VariableMap::get(const std::string& key) const { +VariableMap::get(key_type key) const { const_iterator itr = base_type::find(key); if (itr == base_type::end()) - throw torrent::input_error("Variable \"" + key + "\" does not exist."); + throw torrent::input_error("Variable \"" + std::string(key) + "\" does not exist."); return itr->second->get(); } void -VariableMap::set(const std::string& key, const mapped_type& arg) { +VariableMap::set(key_type key, const mapped_type& arg) { iterator itr = base_type::find(key); // Later, allow the user to create new variables. Have a slot to // register that thing. if (itr == base_type::end()) - throw torrent::input_error("Variable \"" + key + "\" does not exist."); + throw torrent::input_error("Variable \"" + std::string(key) + "\" does not exist."); itr->second->set(arg); } @@ -160,17 +160,17 @@ VariableMap::process_command(const std::string& command) { parse_args(pos + 1, command.end(), &args.as_list()); if (args.as_list().empty()) - set(key, mapped_type()); + set(key.c_str(), mapped_type()); else if (++args.as_list().begin() == args.as_list().end()) - set(key, *args.as_list().begin()); + set(key.c_str(), *args.as_list().begin()); else - set(key, args); + set(key.c_str(), args); } bool -VariableMap::process_file(const std::string& path) { +VariableMap::process_file(key_type path) { std::fstream file(rak::path_expand(path).c_str(), std::ios::in); if (!file.is_open()) @@ -188,7 +188,7 @@ VariableMap::process_file(const std::string& path) { } } catch (torrent::input_error& e) { - snprintf(buffer, max_size_line, "Error in option file: %s:%i: %s", path.c_str(), lineNumber, e.what()); + snprintf(buffer, max_size_line, "Error in option file: %s:%i: %s", path, lineNumber, e.what()); throw torrent::input_error(buffer); } diff --git a/src/utils/variable_map.h b/src/utils/variable_map.h index a3ae905d..7da01a33 100644 --- a/src/utils/variable_map.h +++ b/src/utils/variable_map.h @@ -39,45 +39,58 @@ #include #include +#include #include #include +namespace torrent { + class Object; +} + namespace utils { +struct variable_map_comp : public std::binary_function { + bool operator () (const char* arg1, const char* arg2) const { return std::strcmp(arg1, arg2) < 0; } +}; + class Variable; -class VariableMap : public std::map { +class VariableMap : public std::map { public: - typedef std::map base_type; - typedef torrent::Object mapped_type; - typedef mapped_type::value_type mapped_value_type; + typedef std::map base_type; + + typedef torrent::Object mapped_type; + typedef mapped_type::value_type mapped_value_type; static const int max_size_key = 128; static const int max_size_opt = 1024; static const int max_size_line = max_size_key + max_size_opt + 64; using base_type::iterator; + using base_type::key_type; using base_type::value_type; VariableMap() {} ~VariableMap(); - void insert(const std::string& key, Variable* v); + void insert(key_type key, Variable* v); // Consider taking char* start and finish instead of std::string to // avoid copying. Or make a view class. - const mapped_type& get(const std::string& key) const; - const std::string& get_string(const std::string& key) const { return get(key).as_string(); } - mapped_value_type get_value(const std::string& key) const { return get(key).as_value(); } + const mapped_type& get(key_type key) const; + const std::string& get_string(key_type key) const { return get(key).as_string(); } + mapped_value_type get_value(key_type key) const { return get(key).as_value(); } - void set(const std::string& key, const mapped_type& arg); - void set_string(const std::string& key, const std::string& arg) { set(key, mapped_type(arg)); } - void set_value(const std::string& key, mapped_value_type arg) { set(key, mapped_type(arg)); } + void set(key_type key, const mapped_type& arg); + void set_string(key_type key, const std::string& arg) { set(key, mapped_type(arg)); } + void set_value(key_type key, mapped_value_type arg) { set(key, mapped_type(arg)); } + + void set_std_string(const std::string& key, const std::string& arg) { set(key.c_str(), mapped_type(arg)); } // Relocate. void process_command(const std::string& command); void process_stream(std::istream* str); - bool process_file(const std::string& path); + bool process_file(key_type path); private: VariableMap(const VariableMap&);