Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessary locks in ROOT function GetBaseClassOffset() #11441

Merged
merged 3 commits into from
Sep 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions DataFormats/Common/interface/RefHolderBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
*
*/
#include <memory>

#include "FWCore/Utilities/interface/TypeWithDict.h"
#include <typeinfo>

namespace edm {
class ProductID;
Expand Down Expand Up @@ -52,7 +51,7 @@ namespace edm {
// and cast it to the type specified by toType.
// Return 0 if the real type is not toType nor a subclass of
// toType.
virtual void const* pointerToType(TypeWithDict const& toType) const = 0;
virtual void const* pointerToType(std::type_info const& toType) const = 0;
};

//------------------------------------------------------------------
Expand All @@ -67,8 +66,7 @@ namespace edm {
T const*
RefHolderBase::getPtr() const
{
static const TypeWithDict s_type(typeid(T));
return static_cast<T const*>(pointerToType(s_type));
return static_cast<T const*>(pointerToType(typeid(T)));
}

}
Expand Down
15 changes: 8 additions & 7 deletions DataFormats/Common/interface/RefHolder_.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

#include "DataFormats/Common/interface/RefHolderBase.h"
#include "DataFormats/Provenance/interface/ProductID.h"
#include "FWCore/Utilities/interface/TypeWithDict.h"
#include "FWCore/Utilities/interface/OffsetToBase.h"
#include "FWCore/Utilities/interface/DictionaryTools.h"
#include "FWCore/Utilities/interface/GCC11Compatibility.h"
#include <memory>
#include <typeinfo>

namespace edm {
namespace reftobase {
Expand Down Expand Up @@ -44,7 +45,7 @@ namespace edm {
//Needed for ROOT storage
CMS_CLASS_VERSION(10)
private:
virtual void const* pointerToType(TypeWithDict const& iToType) const GCC11_OVERRIDE;
virtual void const* pointerToType(std::type_info const& iToType) const GCC11_OVERRIDE;
REF ref_;
};

Expand Down Expand Up @@ -133,12 +134,12 @@ namespace edm {

template <class REF>
void const*
RefHolder<REF>::pointerToType(TypeWithDict const& iToType) const
{
RefHolder<REF>::pointerToType(std::type_info const& iToType) const {
typedef typename REF::value_type contained_type;
static TypeWithDict const s_type(typeid(contained_type));

return iToType.pointerToBaseType(ref_.get(), s_type);
if(iToType == typeid(contained_type)) {
return ref_.get();
}
return pointerToBase(iToType, ref_.get());
}
} // namespace reftobase
}
Expand Down
8 changes: 3 additions & 5 deletions DataFormats/Common/interface/fillPtrVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// user include files
#include "DataFormats/Common/interface/FillView.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/Utilities/interface/TypeWithDict.h"
#include "FWCore/Utilities/interface/OffsetToBase.h"

#include "DataFormats/Common/interface/fwd_fillPtrVector.h"

Expand Down Expand Up @@ -56,17 +56,15 @@ namespace edm {
oPtr.push_back(address);
}
} else {
static TypeWithDict const s_type(typeid(element_type));

for(std::vector<unsigned long>::const_iterator itIndex = iIndicies.begin(),
itEnd = iIndicies.end();
itIndex != itEnd;
++itIndex) {
iter it = coll.begin();
std::advance(it, *itIndex);
element_type const* address = GetProduct<product_type>::address(it);
void const* ptr = TypeWithDict(iToType).pointerToBaseType(address, s_type);
if(0 != ptr) {
void const* ptr = pointerToBase(iToType,address);
if(nullptr != ptr) {
oPtr.push_back(ptr);
} else {
Exception::throwThis(errors::LogicError,
Expand Down
12 changes: 5 additions & 7 deletions DataFormats/Common/interface/setPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "DataFormats/Common/interface/FillView.h"
#include "DataFormats/Common/interface/fwd_setPtr.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/Utilities/interface/TypeWithDict.h"
#include "FWCore/Utilities/interface/OffsetToBase.h"

// system include files
#include <typeinfo>
Expand All @@ -45,18 +45,16 @@ namespace edm {
if(iToType == typeid(element_type)) {
iter it = coll.begin();
std::advance(it,iIndex);
element_type const* address = GetProduct<product_type>::address( it );
element_type const* address = GetProduct<product_type>::address(it);
oPtr = address;
} else {
static TypeWithDict const s_type(TypeWithDict(typeid(element_type)));

iter it = coll.begin();
std::advance(it,iIndex);
element_type const* address = GetProduct<product_type>::address( it );
element_type const* address = GetProduct<product_type>::address(it);

oPtr = TypeWithDict(iToType).pointerToBaseType(address, s_type);
oPtr = pointerToBase(iToType,address);

if(0 == oPtr) {
if(nullptr == oPtr) {
Exception::throwThis(errors::LogicError,
"TypeConversionError"
"edm::Ptr<> : unable to convert type ",
Expand Down
1 change: 1 addition & 0 deletions DataFormats/Common/test/testOwnVector.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "cppunit/extensions/HelperMacros.h"
#include <algorithm>
#include <cstring>
#include <iterator>
#include <iostream>
#include "DataFormats/Common/interface/OwnVector.h"
Expand Down
1 change: 1 addition & 0 deletions DataFormats/PatCandidates/src/PackedTriggerPrescales.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "DataFormats/PatCandidates/interface/PackedTriggerPrescales.h"
#include "DataFormats/Common/interface/RefProd.h"
#include <cstring>

pat::PackedTriggerPrescales::PackedTriggerPrescales(const edm::Handle<edm::TriggerResults> & handle) :
prescaleValues_(),
Expand Down
56 changes: 56 additions & 0 deletions FWCore/Utilities/interface/OffsetToBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef FWCore_Utilities_OffsetToBase_h
#define FWCore_Utilities_OffsetToBase_h
#include <typeinfo>

/*
* For any class used in Views, RefToBase, or Ptr,
* class template OffsetToBase must be specialized for any class
* with multiple inheritance (i.e. with non-zero offset to
* at least one base class).
* A specialization would look something like this
* (in YourClass.h"
*
* #include "FWCore/Utilities/interface/OffsetToBase.h"
* namespace edm {
* template<>
* class OffsetToBase<YourClass> {
* public OffsetToBase() {}
* size_t offsetToBase(std::type_info const& baseTypeInfo) const {
* int const dummy = 0;
* YourClass const* object = reinterpret_cast<YourClass const*>(&dummy);
* void const* objectPtr = object;
* if(baseTypeInfo == typeid(BaseClass1)) {
* BaseClass1 const* base = object;
* void const* basePtr = base;
* return static_cast<char const*>(basePtr) - static_cast<char const*>(objectPtr);
* }
* if(baseTypeInfo == typeid(BaseClass2)) {
* ...
* }
* etc.
* }
* };
* }
*
*/


namespace edm {
template<typename T>
class OffsetToBase {
public:
OffsetToBase() {}
size_t offsetToBase(std::type_info const& baseTypeInfo) const {
return 0;
}
};

template<typename T>
void const* pointerToBase(std::type_info const& baseTypeInfo, T const* address) {
OffsetToBase<T> offsetToBase;
int offset = offsetToBase.offsetToBase(baseTypeInfo);
void const* ptr = address;
return static_cast<char const*>(ptr) + offset;
}
}
#endif
1 change: 1 addition & 0 deletions PhysicsTools/HepMCCandAlgos/interface/MCTruthHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


#include <iostream>
#include <unordered_set>

template<typename P>
class MCTruthHelper {
Expand Down