From 563bd81b68f142c7ee24cb410c9dd5ee31d17d49 Mon Sep 17 00:00:00 2001 From: David Rose Date: Sun, 19 Jul 2009 03:18:07 +0000 Subject: [PATCH] small optimization: has_method cache --- direct/src/plugin/binaryXml.cxx | 2 +- direct/src/plugin/p3dPythonObject.cxx | 13 +++++++++++++ direct/src/plugin/p3dPythonObject.h | 3 +++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/direct/src/plugin/binaryXml.cxx b/direct/src/plugin/binaryXml.cxx index ad59945bd4..dc7464ead3 100644 --- a/direct/src/plugin/binaryXml.cxx +++ b/direct/src/plugin/binaryXml.cxx @@ -16,7 +16,7 @@ #include -static const bool debug_xml_output = true; +static const bool debug_xml_output = false; #define DO_BINARY_XML 1 diff --git a/direct/src/plugin/p3dPythonObject.cxx b/direct/src/plugin/p3dPythonObject.cxx index a2c9f76486..abe949d138 100644 --- a/direct/src/plugin/p3dPythonObject.cxx +++ b/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 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; } diff --git a/direct/src/plugin/p3dPythonObject.h b/direct/src/plugin/p3dPythonObject.h index 99f020d8c0..2b4cbe91d7 100644 --- a/direct/src/plugin/p3dPythonObject.h +++ b/direct/src/plugin/p3dPythonObject.h @@ -57,6 +57,9 @@ public: private: P3DSession *_session; int _object_id; + + typedef map HasMethod; + HasMethod _has_method; }; #endif