[pypy-commit] pypy reflex-support: optimization: prevent copies of TClassRef handles

wlav noreply at buildbot.pypy.org
Sat Dec 15 05:28:22 CET 2012


Author: Wim Lavrijsen <WLavrijsen at lbl.gov>
Branch: reflex-support
Changeset: r59433:06e52a567243
Date: 2012-12-13 16:16 -0800
http://bitbucket.org/pypy/pypy/changeset/06e52a567243/

Log:	optimization: prevent copies of TClassRef handles

diff --git a/pypy/module/cppyy/src/cintcwrapper.cxx b/pypy/module/cppyy/src/cintcwrapper.cxx
--- a/pypy/module/cppyy/src/cintcwrapper.cxx
+++ b/pypy/module/cppyy/src/cintcwrapper.cxx
@@ -173,13 +173,13 @@
     return cppstring_to_cstring(true_name);
 }
 
-static inline TClassRef type_from_handle(cppyy_type_t handle) {
+static inline TClassRef& type_from_handle(cppyy_type_t handle) {
     assert((ClassRefs_t::size_type)handle < g_classrefs.size());
     return g_classrefs[(ClassRefs_t::size_type)handle];
 }
 
 static inline TFunction* type_get_method(cppyy_type_t handle, cppyy_index_t idx) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass())
         return (TFunction*)cr->GetListOfMethods()->At(idx);
     return (TFunction*)idx;
@@ -220,7 +220,7 @@
 
 /* name to opaque C++ scope representation -------------------------------- */
 int cppyy_num_scopes(cppyy_scope_t handle) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass()) {
         /* not supported as CINT does not store classes hierarchically */
         return 0;
@@ -229,7 +229,7 @@
 }
 
 char* cppyy_scope_name(cppyy_scope_t handle, int iscope) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass()) {
         /* not supported as CINT does not store classes hierarchically */
         assert(!"scope name lookup not supported on inner scopes");
@@ -278,7 +278,10 @@
     if (icr != g_classref_indices.end())
         return (cppyy_type_t)icr->second;
 
-    // use TClass directly, to enable auto-loading
+    if (strcmp(scope_name, "#define") == 0)
+        return (cppyy_type_t)NULL;
+
+     // use TClass directly, to enable auto-loading
     TClassRef cr(TClass::GetClass(scope_name, kTRUE, kTRUE));
     if (!cr.GetClass())
         return (cppyy_type_t)NULL;
@@ -311,7 +314,7 @@
 }
 
 cppyy_type_t cppyy_actual_class(cppyy_type_t klass, cppyy_object_t obj) {
-    TClassRef cr = type_from_handle(klass);
+    TClassRef& cr = type_from_handle(klass);
     TClass* clActual = cr->GetActualClass( (void*)obj );
     if (clActual && clActual != cr.GetClass()) {
         // TODO: lookup through name should not be needed
@@ -323,7 +326,7 @@
 
 /* memory management ------------------------------------------------------ */
 cppyy_object_t cppyy_allocate(cppyy_type_t handle) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     return (cppyy_object_t)malloc(cr->Size());
 }
 
@@ -332,7 +335,7 @@
 }
 
 void cppyy_destruct(cppyy_type_t handle, cppyy_object_t self) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     cr->Destructor((void*)self, true);
 }
 
@@ -492,7 +495,7 @@
 
 /* scope reflection information ------------------------------------------- */
 int cppyy_is_namespace(cppyy_scope_t handle) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass() && cr->GetClassInfo())
         return cr->Property() & G__BIT_ISNAMESPACE;
     if (strcmp(cr.GetClassName(), "") == 0)
@@ -508,7 +511,7 @@
 
 /* type/class reflection information -------------------------------------- */
 char* cppyy_final_name(cppyy_type_t handle) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass() && cr->GetClassInfo()) {
         std::string true_name = G__TypeInfo(cr->GetName()).TrueName();
         std::string::size_type pos = true_name.rfind("::");
@@ -520,7 +523,7 @@
 }
 
 char* cppyy_scoped_final_name(cppyy_type_t handle) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass() && cr->GetClassInfo()) {
         std::string true_name = G__TypeInfo(cr->GetName()).TrueName();
         return cppstring_to_cstring(true_name);
@@ -535,29 +538,29 @@
 }
 
 int cppyy_num_bases(cppyy_type_t handle) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass() && cr->GetListOfBases() != 0)
         return cr->GetListOfBases()->GetSize();
     return 0;
 }
 
 char* cppyy_base_name(cppyy_type_t handle, int base_index) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     TBaseClass* b = (TBaseClass*)cr->GetListOfBases()->At(base_index);
     return type_cppstring_to_cstring(b->GetName());
 }
 
 int cppyy_is_subtype(cppyy_type_t derived_handle, cppyy_type_t base_handle) {
-    TClassRef derived_type = type_from_handle(derived_handle);
-    TClassRef base_type = type_from_handle(base_handle);
+    TClassRef& derived_type = type_from_handle(derived_handle);
+    TClassRef& base_type = type_from_handle(base_handle);
     return derived_type->GetBaseClass(base_type) != 0;
 }
 
 size_t cppyy_base_offset(cppyy_type_t derived_handle, cppyy_type_t base_handle,
                        cppyy_object_t address, int /* direction */) {
     // WARNING: CINT can not handle actual dynamic casts!
-    TClassRef derived_type = type_from_handle(derived_handle);
-    TClassRef base_type = type_from_handle(base_handle);
+    TClassRef& derived_type = type_from_handle(derived_handle);
+    TClassRef& base_type = type_from_handle(base_handle);
 
     long offset = 0;
 
@@ -586,7 +589,7 @@
 
 /* method/function reflection information --------------------------------- */
 int cppyy_num_methods(cppyy_scope_t handle) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass() && cr->GetListOfMethods())
         return cr->GetListOfMethods()->GetSize();
     else if (strcmp(cr.GetClassName(), "") == 0) {
@@ -608,7 +611,7 @@
 }
 
 cppyy_index_t cppyy_method_index_at(cppyy_scope_t handle, int imeth) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass())
         return (cppyy_index_t)imeth;
     return (cppyy_index_t)&g_globalfuncs[imeth];
@@ -616,7 +619,7 @@
 
 cppyy_index_t* cppyy_method_indices_from_name(cppyy_scope_t handle, const char* name) {
     std::vector<cppyy_index_t> result;
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass()) {
         gInterpreter->UpdateListOfMethods(cr.GetClass());
         int imeth = 0;
@@ -659,7 +662,7 @@
 }
 
 char* cppyy_method_result_type(cppyy_scope_t handle, cppyy_index_t idx) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass() && cppyy_is_constructor(handle, idx))
         return cppstring_to_cstring("constructor");
     TFunction* f = type_get_method(handle, idx);
@@ -688,7 +691,7 @@
 }
 
 char* cppyy_method_signature(cppyy_scope_t handle, cppyy_index_t idx) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     TFunction* f = type_get_method(handle, idx);
     std::ostringstream sig;
     if (cr.GetClass() && cr->GetClassInfo()
@@ -707,7 +710,7 @@
 
 
 cppyy_method_t cppyy_get_method(cppyy_scope_t handle, cppyy_index_t idx) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     TFunction* f = type_get_method(handle, idx);
     if (cr && cr.GetClass() && !cr->IsLoaded()) {
         G__ClassInfo* gcl = (G__ClassInfo*)cr->GetClassInfo();
@@ -731,8 +734,8 @@
 }
 
 cppyy_index_t cppyy_get_global_operator(cppyy_scope_t scope, cppyy_scope_t lc, cppyy_scope_t rc, const char* op) {
-    TClassRef lccr = type_from_handle(lc);
-    TClassRef rccr = type_from_handle(rc);
+    TClassRef& lccr = type_from_handle(lc);
+    TClassRef& rccr = type_from_handle(rc);
 
     if (!lccr.GetClass() || !rccr.GetClass() || scope != GLOBAL_HANDLE)
         return (cppyy_index_t)-1;  // (void*)-1 is in kernel space, so invalid as a method handle
@@ -762,13 +765,13 @@
 
 /* method properties -----------------------------------------------------  */
 int cppyy_is_constructor(cppyy_type_t handle, cppyy_index_t idx) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     TMethod* m = (TMethod*)cr->GetListOfMethods()->At(idx);
     return strcmp(m->GetName(), ((G__ClassInfo*)cr->GetClassInfo())->Name()) == 0;
 }
 
 int cppyy_is_staticmethod(cppyy_type_t handle, cppyy_index_t idx) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     TMethod* m = (TMethod*)cr->GetListOfMethods()->At(idx);
     return m->Property() & G__BIT_ISSTATIC;
 }
@@ -776,7 +779,7 @@
 
 /* data member reflection information ------------------------------------- */
 int cppyy_num_datamembers(cppyy_scope_t handle) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass() && cr->GetListOfDataMembers())
         return cr->GetListOfDataMembers()->GetSize();
     else if (strcmp(cr.GetClassName(), "") == 0) {
@@ -798,7 +801,7 @@
 }
 
 char* cppyy_datamember_name(cppyy_scope_t handle, int datamember_index) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass()) {
         TDataMember* m = (TDataMember*)cr->GetListOfDataMembers()->At(datamember_index);
         return cppstring_to_cstring(m->GetName());
@@ -808,7 +811,7 @@
 }
 
 char* cppyy_datamember_type(cppyy_scope_t handle, int datamember_index) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass())  {
         TDataMember* m = (TDataMember*)cr->GetListOfDataMembers()->At(datamember_index);
         std::string fullType = m->GetFullTypeName();
@@ -826,7 +829,7 @@
 }
 
 size_t cppyy_datamember_offset(cppyy_scope_t handle, int datamember_index) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass()) {
         TDataMember* m = (TDataMember*)cr->GetListOfDataMembers()->At(datamember_index);
         return (size_t)m->GetOffsetCint();
@@ -836,7 +839,7 @@
 }
 
 int cppyy_datamember_index(cppyy_scope_t handle, const char* name) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass()) {
         // called from updates; add a hard reset as the code itself caches in
         // Class (TODO: by-pass ROOT/meta)
@@ -871,7 +874,7 @@
 
 /* data member properties ------------------------------------------------  */
 int cppyy_is_publicdata(cppyy_scope_t handle, int datamember_index) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass()) {
         TDataMember* m = (TDataMember*)cr->GetListOfDataMembers()->At(datamember_index);
         return m->Property() & G__BIT_ISPUBLIC;
@@ -880,7 +883,7 @@
 }
 
 int cppyy_is_staticdata(cppyy_scope_t handle, int datamember_index) {
-    TClassRef cr = type_from_handle(handle);
+    TClassRef& cr = type_from_handle(handle);
     if (cr.GetClass()) {
         TDataMember* m = (TDataMember*)cr->GetListOfDataMembers()->At(datamember_index);
         return m->Property() & G__BIT_ISSTATIC;


More information about the pypy-commit mailing list