Browse Source

small optimization: has_method cache

David Rose 16 years ago
parent
commit
563bd81b68

+ 1 - 1
direct/src/plugin/binaryXml.cxx

@@ -16,7 +16,7 @@
 #include <sstream>
 
 
-static const bool debug_xml_output = true;
+static const bool debug_xml_output = false;
 
 #define DO_BINARY_XML 1
 

+ 13 - 0
direct/src/plugin/p3dPythonObject.cxx

@@ -189,6 +189,14 @@ set_property(const string &property, P3D_object *value) {
 ////////////////////////////////////////////////////////////////////
 bool P3DPythonObject::
 has_method(const string &method_name) {
+  // First, check the cache.
+  pair<HasMethod::iterator, bool> cresult = _has_method.insert(HasMethod::value_type(method_name, false));
+  HasMethod::iterator hi = cresult.first;
+  if (!cresult.second) {
+    // Already cached.
+    return (*hi).second;
+  }
+
   bool bresult = false;
 
   P3D_object *params[1];
@@ -202,6 +210,11 @@ has_method(const string &method_name) {
     P3D_OBJECT_DECREF(result);
   }
 
+  // Save the cached result, so we don't have to keep asking this
+  // question.  We assume that the set of methods on an object don't
+  // change substantially, so we can get away with keeping this cache.
+  (*hi).second = bresult;
+
   return bresult;
 }
 

+ 3 - 0
direct/src/plugin/p3dPythonObject.h

@@ -57,6 +57,9 @@ public:
 private:
   P3DSession *_session;
   int _object_id;
+
+  typedef map<string, bool> HasMethod;
+  HasMethod _has_method;
 };
 
 #endif