From 4656d7ca7d92783ea6cc382fbccfeed1fdeb8f90 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 16 Apr 2019 18:11:43 +0200 Subject: [PATCH] Change pre-created types to unique_ptr to avoid copying and moving. --- libsolidity/ast/TypeProvider.cpp | 262 +++++++++++++++---------------- libsolidity/ast/TypeProvider.h | 14 +- 2 files changed, 135 insertions(+), 141 deletions(-) diff --git a/libsolidity/ast/TypeProvider.cpp b/libsolidity/ast/TypeProvider.cpp index 10f9291b8..35adaf037 100644 --- a/libsolidity/ast/TypeProvider.cpp +++ b/libsolidity/ast/TypeProvider.cpp @@ -34,158 +34,152 @@ TupleType const TypeProvider::m_emptyTupleType{}; AddressType const TypeProvider::m_payableAddressType{StateMutability::Payable}; AddressType const TypeProvider::m_addressType{StateMutability::NonPayable}; -array const TypeProvider::m_intM{ - IntegerType{8 * 1, IntegerType::Modifier::Signed}, - IntegerType{8 * 2, IntegerType::Modifier::Signed}, - IntegerType{8 * 3, IntegerType::Modifier::Signed}, - IntegerType{8 * 4, IntegerType::Modifier::Signed}, - IntegerType{8 * 5, IntegerType::Modifier::Signed}, - IntegerType{8 * 6, IntegerType::Modifier::Signed}, - IntegerType{8 * 7, IntegerType::Modifier::Signed}, - IntegerType{8 * 8, IntegerType::Modifier::Signed}, - IntegerType{8 * 9, IntegerType::Modifier::Signed}, - IntegerType{8 * 10, IntegerType::Modifier::Signed}, - IntegerType{8 * 11, IntegerType::Modifier::Signed}, - IntegerType{8 * 12, IntegerType::Modifier::Signed}, - IntegerType{8 * 13, IntegerType::Modifier::Signed}, - IntegerType{8 * 14, IntegerType::Modifier::Signed}, - IntegerType{8 * 15, IntegerType::Modifier::Signed}, - IntegerType{8 * 16, IntegerType::Modifier::Signed}, - IntegerType{8 * 17, IntegerType::Modifier::Signed}, - IntegerType{8 * 18, IntegerType::Modifier::Signed}, - IntegerType{8 * 19, IntegerType::Modifier::Signed}, - IntegerType{8 * 20, IntegerType::Modifier::Signed}, - IntegerType{8 * 21, IntegerType::Modifier::Signed}, - IntegerType{8 * 22, IntegerType::Modifier::Signed}, - IntegerType{8 * 23, IntegerType::Modifier::Signed}, - IntegerType{8 * 24, IntegerType::Modifier::Signed}, - IntegerType{8 * 25, IntegerType::Modifier::Signed}, - IntegerType{8 * 26, IntegerType::Modifier::Signed}, - IntegerType{8 * 27, IntegerType::Modifier::Signed}, - IntegerType{8 * 28, IntegerType::Modifier::Signed}, - IntegerType{8 * 29, IntegerType::Modifier::Signed}, - IntegerType{8 * 30, IntegerType::Modifier::Signed}, - IntegerType{8 * 31, IntegerType::Modifier::Signed}, - IntegerType{8 * 32, IntegerType::Modifier::Signed} -}; +array, 32> const TypeProvider::m_intM{{ + {make_unique(8 * 1, IntegerType::Modifier::Signed)}, + {make_unique(8 * 2, IntegerType::Modifier::Signed)}, + {make_unique(8 * 3, IntegerType::Modifier::Signed)}, + {make_unique(8 * 4, IntegerType::Modifier::Signed)}, + {make_unique(8 * 5, IntegerType::Modifier::Signed)}, + {make_unique(8 * 6, IntegerType::Modifier::Signed)}, + {make_unique(8 * 7, IntegerType::Modifier::Signed)}, + {make_unique(8 * 8, IntegerType::Modifier::Signed)}, + {make_unique(8 * 9, IntegerType::Modifier::Signed)}, + {make_unique(8 * 10, IntegerType::Modifier::Signed)}, + {make_unique(8 * 11, IntegerType::Modifier::Signed)}, + {make_unique(8 * 12, IntegerType::Modifier::Signed)}, + {make_unique(8 * 13, IntegerType::Modifier::Signed)}, + {make_unique(8 * 14, IntegerType::Modifier::Signed)}, + {make_unique(8 * 15, IntegerType::Modifier::Signed)}, + {make_unique(8 * 16, IntegerType::Modifier::Signed)}, + {make_unique(8 * 17, IntegerType::Modifier::Signed)}, + {make_unique(8 * 18, IntegerType::Modifier::Signed)}, + {make_unique(8 * 19, IntegerType::Modifier::Signed)}, + {make_unique(8 * 20, IntegerType::Modifier::Signed)}, + {make_unique(8 * 21, IntegerType::Modifier::Signed)}, + {make_unique(8 * 22, IntegerType::Modifier::Signed)}, + {make_unique(8 * 23, IntegerType::Modifier::Signed)}, + {make_unique(8 * 24, IntegerType::Modifier::Signed)}, + {make_unique(8 * 25, IntegerType::Modifier::Signed)}, + {make_unique(8 * 26, IntegerType::Modifier::Signed)}, + {make_unique(8 * 27, IntegerType::Modifier::Signed)}, + {make_unique(8 * 28, IntegerType::Modifier::Signed)}, + {make_unique(8 * 29, IntegerType::Modifier::Signed)}, + {make_unique(8 * 30, IntegerType::Modifier::Signed)}, + {make_unique(8 * 31, IntegerType::Modifier::Signed)}, + {make_unique(8 * 32, IntegerType::Modifier::Signed)} +}}; -array const TypeProvider::m_uintM{ - IntegerType{8 * 1, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 2, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 3, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 4, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 5, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 6, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 7, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 8, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 9, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 10, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 11, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 12, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 13, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 14, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 15, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 16, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 17, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 18, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 19, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 20, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 21, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 22, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 23, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 24, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 25, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 26, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 27, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 28, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 29, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 30, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 31, IntegerType::Modifier::Unsigned}, - IntegerType{8 * 32, IntegerType::Modifier::Unsigned} -}; +array, 32> const TypeProvider::m_uintM{{ + {make_unique(8 * 1, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 2, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 3, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 4, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 5, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 6, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 7, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 8, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 9, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 10, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 11, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 12, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 13, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 14, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 15, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 16, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 17, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 18, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 19, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 20, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 21, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 22, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 23, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 24, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 25, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 26, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 27, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 28, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 29, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 30, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 31, IntegerType::Modifier::Unsigned)}, + {make_unique(8 * 32, IntegerType::Modifier::Unsigned)} +}}; -array const TypeProvider::m_bytesM{ - FixedBytesType{1}, - FixedBytesType{2}, - FixedBytesType{3}, - FixedBytesType{4}, - FixedBytesType{5}, - FixedBytesType{6}, - FixedBytesType{7}, - FixedBytesType{8}, - FixedBytesType{9}, - FixedBytesType{10}, - FixedBytesType{11}, - FixedBytesType{12}, - FixedBytesType{13}, - FixedBytesType{14}, - FixedBytesType{15}, - FixedBytesType{16}, - FixedBytesType{17}, - FixedBytesType{18}, - FixedBytesType{19}, - FixedBytesType{20}, - FixedBytesType{21}, - FixedBytesType{22}, - FixedBytesType{23}, - FixedBytesType{24}, - FixedBytesType{25}, - FixedBytesType{26}, - FixedBytesType{27}, - FixedBytesType{28}, - FixedBytesType{29}, - FixedBytesType{30}, - FixedBytesType{31}, - FixedBytesType{32} -}; +array, 32> const TypeProvider::m_bytesM{{ + {make_unique(1)}, + {make_unique(2)}, + {make_unique(3)}, + {make_unique(4)}, + {make_unique(5)}, + {make_unique(6)}, + {make_unique(7)}, + {make_unique(8)}, + {make_unique(9)}, + {make_unique(10)}, + {make_unique(11)}, + {make_unique(12)}, + {make_unique(13)}, + {make_unique(14)}, + {make_unique(15)}, + {make_unique(16)}, + {make_unique(17)}, + {make_unique(18)}, + {make_unique(19)}, + {make_unique(20)}, + {make_unique(21)}, + {make_unique(22)}, + {make_unique(23)}, + {make_unique(24)}, + {make_unique(25)}, + {make_unique(26)}, + {make_unique(27)}, + {make_unique(28)}, + {make_unique(29)}, + {make_unique(30)}, + {make_unique(31)}, + {make_unique(32)} +}}; -array const TypeProvider::m_magicTypes{ - MagicType{MagicType::Kind::Block}, - MagicType{MagicType::Kind::Message}, - MagicType{MagicType::Kind::Transaction}, - MagicType{MagicType::Kind::ABI} +array, 4> const TypeProvider::m_magicTypes{{ + {make_unique(MagicType::Kind::Block)}, + {make_unique(MagicType::Kind::Message)}, + {make_unique(MagicType::Kind::Transaction)}, + {make_unique(MagicType::Kind::ABI)} // MetaType is stored separately -}; +}}; inline void clearCache(Type const& type) { type.clearCache(); } -template -inline void clearCache(Type const& type, Args&... moreTypes) +template +inline void clearCache(unique_ptr const& type) { - clearCache(type); - clearCache(moreTypes...); + type->clearCache(); } template -inline void clearAllCaches(Container& container) +inline void clearCaches(Container& container) { - for_each(begin(container), end(container), [](Type const& t) { t.clearCache(); }); -} - -template -inline void clearAllCaches(Container& types, Args&... more) -{ - clearAllCaches(types); - clearAllCaches(more...); + for (auto const& e: container) + clearCache(e); } void TypeProvider::reset() { - clearCache( - m_boolType, - m_inaccessibleDynamicType, - m_bytesStorageType, - m_bytesMemoryType, - m_stringStorageType, - m_stringMemoryType, - m_emptyTupleType, - m_payableAddressType, - m_addressType - ); - clearAllCaches(instance().m_intM, instance().m_uintM, instance().m_bytesM, instance().m_magicTypes); + clearCache(m_boolType); + clearCache(m_inaccessibleDynamicType); + clearCache(m_bytesStorageType); + clearCache(m_bytesMemoryType); + clearCache(m_stringStorageType); + clearCache(m_stringMemoryType); + clearCache(m_emptyTupleType); + clearCache(m_payableAddressType); + clearCache(m_addressType); + clearCaches(instance().m_intM); + clearCaches(instance().m_uintM); + clearCaches(instance().m_bytesM); + clearCaches(instance().m_magicTypes); instance().m_generalTypes.clear(); instance().m_stringLiteralTypes.clear(); @@ -498,7 +492,7 @@ ModifierType const* TypeProvider::modifierType(ModifierDefinition const& _def) MagicType const* TypeProvider::magicType(MagicType::Kind _kind) { solAssert(_kind != MagicType::Kind::MetaType, "MetaType is handled separately"); - return &m_magicTypes.at(static_cast(_kind)); + return m_magicTypes.at(static_cast(_kind)).get(); } MagicType const* TypeProvider::metaType(Type const* _type) diff --git a/libsolidity/ast/TypeProvider.h b/libsolidity/ast/TypeProvider.h index ddfb50f48..5e52ba759 100644 --- a/libsolidity/ast/TypeProvider.h +++ b/libsolidity/ast/TypeProvider.h @@ -64,7 +64,7 @@ public: static BoolType const* boolType() noexcept { return &m_boolType; } static FixedBytesType const* byteType() { return fixedBytesType(1); } - static FixedBytesType const* fixedBytesType(unsigned m) { return &m_bytesM.at(m - 1); } + static FixedBytesType const* fixedBytesType(unsigned m) { return m_bytesM.at(m - 1).get(); } static ArrayType const* bytesType() noexcept { return &m_bytesStorageType; } static ArrayType const* bytesMemoryType() noexcept { return &m_bytesMemoryType; } @@ -87,9 +87,9 @@ public: { solAssert((_bits % 8) == 0, ""); if (_modifier == IntegerType::Modifier::Unsigned) - return &m_uintM.at(_bits / 8 - 1); + return m_uintM.at(_bits / 8 - 1).get(); else - return &m_intM.at(_bits / 8 - 1); + return m_intM.at(_bits / 8 - 1).get(); } static IntegerType const* uint(unsigned _bits) { return integerType(_bits, IntegerType::Modifier::Unsigned); } @@ -206,10 +206,10 @@ private: static TupleType const m_emptyTupleType; static AddressType const m_payableAddressType; static AddressType const m_addressType; - static std::array const m_intM; - static std::array const m_uintM; - static std::array const m_bytesM; - static std::array const m_magicTypes; ///< MagicType's except MetaType + static std::array, 32> const m_intM; + static std::array, 32> const m_uintM; + static std::array, 32> const m_bytesM; + static std::array, 4> const m_magicTypes; ///< MagicType's except MetaType std::map, std::unique_ptr> m_ufixedMxN{}; std::map, std::unique_ptr> m_fixedMxN{};