small optimization: has_method cache

This commit is contained in:
David Rose 2009-07-19 03:18:07 +00:00
parent efdeacb577
commit 563bd81b68
3 changed files with 17 additions and 1 deletions

View File

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

View File

@ -189,6 +189,14 @@ set_property(const string &property, P3D_object *value) {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
bool P3DPythonObject:: bool P3DPythonObject::
has_method(const string &method_name) { 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; bool bresult = false;
P3D_object *params[1]; P3D_object *params[1];
@ -202,6 +210,11 @@ has_method(const string &method_name) {
P3D_OBJECT_DECREF(result); 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; return bresult;
} }

View File

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