Speed up iterating over an entire PortSet by keeping a
authorCarl Hetherington <carl@carlh.net>
Mon, 23 Jan 2012 19:35:56 +0000 (19:35 +0000)
committerCarl Hetherington <carl@carlh.net>
Mon, 23 Jan 2012 19:35:56 +0000 (19:35 +0000)
separate list of all ports.

git-svn-id: svn://localhost/ardour2/branches/3.0@11318 d708f5d6-7413-0410-9779-e7cbd77b26cf

libs/ardour/ardour/port_set.h
libs/ardour/port_set.cc

index a6a0593946680a221cde74cadaf7e752891369c0..690875cd8624179e02b8cf4c9ce8cfda21a410c2 100644 (file)
@@ -34,6 +34,10 @@ class MidiPort;
  * This allows access to all the ports as a list, ignoring type, or accessing
  * the nth port of a given type.  Note that port(n) and nth_audio_port(n) may
  * NOT return the same port.
+ *
+ * Each port is held twice; once in a per-type vector of vectors (_ports)
+ * and once in a vector of all port (_all_ports).  This is to speed up the
+ * fairly common case of iterating over all ports.
  */
 class PortSet : public boost::noncopyable {
 public:
@@ -60,7 +64,7 @@ public:
        /** Remove all ports from the PortSet.  Ports are not deregistered with
         * the engine, it's the caller's responsibility to not leak here!
         */
-       void clear() { _ports.clear(); }
+       void clear();
 
        const ChanCount& count() const { return _count; }
 
@@ -132,6 +136,8 @@ private:
 
        // Vector of vectors, indexed by DataType::to_index()
        std::vector<PortVec> _ports;
+       // All ports in _ports in one vector, to speed some operations
+       PortVec _all_ports;
 
        ChanCount _count;
 };
index 8ad8531f5d1c32a31fe68c7c244a389b2326538a..7e9f0656c598a760886c541c5f3681ec18d10562 100644 (file)
@@ -71,14 +71,27 @@ static bool sort_ports_by_name (boost::shared_ptr<Port> a, boost::shared_ptr<Por
        }
 }
 
+
+static bool sort_ports_by_type_and_name (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
+{
+       if (a->type() != b->type()) {
+               return a->type() < b->type();
+       }
+
+       return sort_ports_by_name (a, b);
+}
+
 void
 PortSet::add (boost::shared_ptr<Port> port)
 {
        PortVec& v = _ports[port->type()];
 
        v.push_back(port);
+       _all_ports.push_back(port);
 
        sort(v.begin(), v.end(), sort_ports_by_name);
+       sort(_all_ports.begin(), _all_ports.end(), sort_ports_by_type_and_name);
+       
        _count.set(port->type(), _count.get(port->type()) + 1);
        assert(_count.get(port->type()) == _ports[port->type()].size());
 }
@@ -86,6 +99,11 @@ PortSet::add (boost::shared_ptr<Port> port)
 bool
 PortSet::remove (boost::shared_ptr<Port> port)
 {
+       PortVec::iterator i = find(_all_ports.begin(), _all_ports.end(), port);
+       if (i != _all_ports.end()) {
+               _all_ports.erase(i);
+       }
+       
        for (std::vector<PortVec>::iterator l = _ports.begin(); l != _ports.end(); ++l) {
                PortVec::iterator i = find(l->begin(), l->end(), port);
                if (i != l->end()) {
@@ -103,40 +121,20 @@ PortSet::remove (boost::shared_ptr<Port> port)
 size_t
 PortSet::num_ports() const
 {
-       size_t ret = 0;
-
-       for (std::vector<PortVec>::const_iterator l = _ports.begin(); l != _ports.end(); ++l)
-               ret += (*l).size();
-
-       return ret;
+       return _all_ports.size();
 }
 
 bool
 PortSet::contains (boost::shared_ptr<const Port> port) const
 {
-       for (std::vector<PortVec>::const_iterator l = _ports.begin(); l != _ports.end(); ++l)
-               if (find (l->begin(), l->end(), port) != l->end())
-                       return true;
-
-       return false;
+       return find(_all_ports.begin(), _all_ports.end(), port) != _all_ports.end();
 }
 
 boost::shared_ptr<Port>
 PortSet::port(size_t n) const
 {
-       // This is awesome.  Awesomely slow.
-
-       size_t size_so_far = 0;
-
-       for (std::vector<PortVec>::const_iterator l = _ports.begin(); l != _ports.end(); ++l) {
-               if (n < size_so_far + l->size()) {
-                       return (*l)[n - size_so_far];
-               } else {
-                       size_so_far += l->size();
-               }
-       }
-
-       return boost::shared_ptr<Port> (); // n out of range
+       assert(n < _all_ports.size());
+       return _all_ports[n];
 }
 
 boost::shared_ptr<Port>
@@ -163,4 +161,11 @@ PortSet::nth_midi_port(size_t n) const
        return boost::dynamic_pointer_cast<MidiPort> (port (DataType::MIDI, n));
 }
 
+void
+PortSet::clear()
+{
+       _ports.clear();
+       _all_ports.clear();
+}
+
 } // namepace ARDOUR