diff --git a/src/objit/src/ob_llvm_helper.cpp b/src/objit/src/ob_llvm_helper.cpp index d33a007b0..81ad2552f 100644 --- a/src/objit/src/ob_llvm_helper.cpp +++ b/src/objit/src/ob_llvm_helper.cpp @@ -32,6 +32,7 @@ #include "lib/container/ob_se_array.h" #include "share/rc/ob_tenant_base.h" #include "lib/alloc/malloc_hook.h" +#include "pl/ob_pl_allocator.h" using namespace llvm; @@ -587,7 +588,7 @@ int ObLLVMHelper::init_llvm() { void ObLLVMHelper::compile_module(bool optimization) { if (optimization) { - OB_LLVM_MALLOC_GUARD("PlCodeGen"); + OB_LLVM_MALLOC_GUARD(GET_PL_MOD_STRING(pl::OB_PL_CODE_GEN)); jc_->optimize(); LOG_INFO("================Optimized LLVM Module================"); dump_module(); @@ -598,7 +599,7 @@ void ObLLVMHelper::compile_module(bool optimization) void ObLLVMHelper::dump_module() { - OB_LLVM_MALLOC_GUARD("PlCodeGen"); + OB_LLVM_MALLOC_GUARD(GET_PL_MOD_STRING(pl::OB_PL_CODE_GEN)); if (OB_ISNULL(jc_)) { //do nothing } else { @@ -611,7 +612,7 @@ void ObLLVMHelper::dump_module() void ObLLVMHelper::dump_debuginfo() { - OB_LLVM_MALLOC_GUARD("PlCodeGen"); + OB_LLVM_MALLOC_GUARD(GET_PL_MOD_STRING(pl::OB_PL_CODE_GEN)); if (OB_ISNULL(jit_) || jit_->get_debug_info_size() <= 0) { // do nothing ... } else { @@ -621,7 +622,7 @@ void ObLLVMHelper::dump_debuginfo() int ObLLVMHelper::verify_function(ObLLVMFunction &function) { - OB_LLVM_MALLOC_GUARD("PlCodeGen"); + OB_LLVM_MALLOC_GUARD(GET_PL_MOD_STRING(pl::OB_PL_CODE_GEN)); int ret = OB_SUCCESS; if (OB_ISNULL(jc_)) { ret = OB_NOT_INIT; @@ -638,7 +639,7 @@ int ObLLVMHelper::verify_function(ObLLVMFunction &function) int ObLLVMHelper::verify_module() { - OB_LLVM_MALLOC_GUARD("PlCodeGen"); + OB_LLVM_MALLOC_GUARD(GET_PL_MOD_STRING(pl::OB_PL_CODE_GEN)); int ret = OB_SUCCESS; std::string verify_error; llvm::raw_string_ostream verify_raw_os(verify_error); diff --git a/src/pl/ob_pl_allocator.h b/src/pl/ob_pl_allocator.h index 7ba3d616f..0b1cfdd60 100644 --- a/src/pl/ob_pl_allocator.h +++ b/src/pl/ob_pl_allocator.h @@ -21,6 +21,7 @@ PL_MOD_DEF(OB_PL_DEBUG_MOD, "PlDebug") PL_MOD_DEF(OB_PL_DEBUG_SYS_MOD, "PlDebugSys") PL_MOD_DEF(OB_PL_ANY_DATA, "AnyData") PL_MOD_DEF(OB_PL_ANY_TYPE, "AnyType") +PL_MOD_DEF(OB_PL_CODE_GEN, "PlCodeGen") #endif @@ -142,8 +143,11 @@ static constexpr const char* OB_PL_MOD_DEF[PL_MOD_IDX_NUM] = #undef PL_MOD_DEF }; -#define GET_PL_MOD_STRING(type) (type > OB_MOD_INVALID && type < PL_MOD_IDX_NUM) ? OB_PL_MOD_DEF[type] : "PlTemp" - +#define GET_PL_MOD_STRING(type) \ + (type > oceanbase::pl::OB_MOD_INVALID && \ + type < oceanbase::pl::PL_MOD_IDX_NUM) \ + ? oceanbase::pl::OB_PL_MOD_DEF[type] \ + : "PlTemp" } } diff --git a/src/pl/ob_pl_code_generator.cpp b/src/pl/ob_pl_code_generator.cpp index 084cbefef..c58f6294f 100644 --- a/src/pl/ob_pl_code_generator.cpp +++ b/src/pl/ob_pl_code_generator.cpp @@ -3527,7 +3527,37 @@ int ObPLCodeGenerator::build_opaque_type(const ObUserDefinedType &opaque_type, int ObPLCodeGenerator::init() { int ret = OB_SUCCESS; - if (debug_mode_ && OB_FAIL(di_helper_.init(helper_.get_jc()))) { + + // CG local types + external types at least, so pre-allocate doubled buckets + // bucket number will grow up automatically if udt_count_guess is not enough + int64_t udt_count_guess = + (ast_.get_user_type_table().get_count() + + ast_.get_user_type_table().get_external_types().count()) * 2; + + // make udt_count_guess at least 64, to prevent size grow up frequently in bad case + if (udt_count_guess < 64) { + udt_count_guess = 64; + } + + int64_t goto_label_count_guess = 64; + if (OB_NOT_NULL(ast_.get_body()) && + ast_.get_body()->get_stmts().count() > goto_label_count_guess) { + goto_label_count_guess = ast_.get_body()->get_stmts().count(); + } + + if (OB_FAIL(user_type_map_.create( + udt_count_guess, + ObMemAttr(MTL_ID(), GET_PL_MOD_STRING(OB_PL_CODE_GEN))))){ + LOG_WARN("failed to create user_type_map_", K(ret), K(udt_count_guess)); + } else if (OB_FAIL(di_user_type_map_.create( + udt_count_guess, + ObMemAttr(MTL_ID(), GET_PL_MOD_STRING(OB_PL_CODE_GEN))))){ + LOG_WARN("failed to create di_user_type_map_", K(ret), K(udt_count_guess)); + } else if (OB_FAIL(goto_label_map_.create( + goto_label_count_guess, + ObMemAttr(MTL_ID(), GET_PL_MOD_STRING(OB_PL_CODE_GEN))))) { + LOG_WARN("failed to create goto_label_map_", K(ret), K(goto_label_count_guess)); + } else if (debug_mode_ && OB_FAIL(di_helper_.init(helper_.get_jc()))) { LOG_WARN("failed to init di helper", K(ret)); } else if (OB_FAIL(init_spi_service())) { LOG_WARN("failed to init spi service", K(ret)); diff --git a/src/pl/ob_pl_code_generator.h b/src/pl/ob_pl_code_generator.h index dd0b10425..483d048ba 100644 --- a/src/pl/ob_pl_code_generator.h +++ b/src/pl/ob_pl_code_generator.h @@ -194,9 +194,7 @@ public: di_user_type_map_(), debug_mode_(session_info_.is_pl_debug_on() && func_ast.is_routine()), oracle_mode_(oracle_mode) - { - goto_label_map_.create(func_ast.get_body()->get_stmts().count(), "PlCodeGen"); - } + { } virtual ~ObPLCodeGenerator() {} @@ -488,8 +486,28 @@ public: } inline jit::ObLLVMFunction &get_func() { return func_; } inline ObPLSEArray &get_vars() { return vars_; } - typedef common::hash::ObPlacementHashMap ObLLVMTypeMap; - typedef common::hash::ObPlacementHashMap ObLLVMDITypeMap; + typedef common::hash::ObHashMap< + uint64_t, jit::ObLLVMType, + common::hash::NoPthreadDefendMode, + common::hash::hash_func, + common::hash::equal_to, + common::hash::SimpleAllocer>>, + common::hash::NormalPointer, + common::ObMalloc, + 2> // EXTEND_RATIO + ObLLVMTypeMap; + typedef common::hash::ObHashMap< + uint64_t, jit::ObLLVMDIType, + common::hash::NoPthreadDefendMode, + common::hash::hash_func, + common::hash::equal_to, + common::hash::SimpleAllocer>>, + common::hash::NormalPointer, + common::ObMalloc, + 2> // EXTEND_RATIO + ObLLVMDITypeMap; inline ObLLVMTypeMap &get_user_type_map() { return user_type_map_; } int set_var_addr_to_param_store(int64_t var_index, jit::ObLLVMValue &var, jit::ObLLVMValue &init_value); int get_llvm_type(const ObPLDataType &pl_type, jit::ObLLVMType &ir_type); diff --git a/src/pl/ob_pl_compile.cpp b/src/pl/ob_pl_compile.cpp index cdbdc0b5e..2f43ad801 100644 --- a/src/pl/ob_pl_compile.cpp +++ b/src/pl/ob_pl_compile.cpp @@ -230,7 +230,7 @@ int ObPLCompiler::compile( func.get_di_helper(), lib::is_oracle_mode()) { #endif - lib::ObMallocHookAttrGuard malloc_guard(lib::ObMemAttr(MTL_ID(), "PlCodeGen")); + lib::ObMallocHookAttrGuard malloc_guard(lib::ObMemAttr(MTL_ID(), GET_PL_MOD_STRING(pl::OB_PL_CODE_GEN))); uint64_t lock_idx = stmt_id != OB_INVALID_ID ? stmt_id : murmurhash(block->str_value_, block->str_len_, 0); ObBucketHashWLockGuard compile_guard(GCTX.pl_engine_->get_jit_lock(), lock_idx); // check session status after get lock @@ -469,7 +469,7 @@ int ObPLCompiler::compile(const uint64_t id, ObPLFunction &func) func.get_di_helper(), lib::is_oracle_mode()) { #endif - lib::ObMallocHookAttrGuard malloc_guard(lib::ObMemAttr(MTL_ID(), "PlCodeGen")); + lib::ObMallocHookAttrGuard malloc_guard(lib::ObMemAttr(MTL_ID(), GET_PL_MOD_STRING(pl::OB_PL_CODE_GEN))); ObBucketHashWLockGuard compile_guard(GCTX.pl_engine_->get_jit_lock(), id); // check session status after get lock if (OB_FAIL(ObPL::check_session_alive(session_info_))) { @@ -801,7 +801,7 @@ int ObPLCompiler::compile_package(const ObPackageInfo &package_info, package.get_di_helper(), lib::is_oracle_mode()) { #endif - lib::ObMallocHookAttrGuard malloc_guard(lib::ObMemAttr(MTL_ID(), "PlCodeGen")); + lib::ObMallocHookAttrGuard malloc_guard(lib::ObMemAttr(MTL_ID(), GET_PL_MOD_STRING(pl::OB_PL_CODE_GEN))); OZ (cg.init()); OZ (cg.generate(package)); } @@ -1300,7 +1300,7 @@ int ObPLCompiler::compile_subprogram_table(common::ObIAllocator &allocator, routine->get_di_helper(), lib::is_oracle_mode()) { #endif - lib::ObMallocHookAttrGuard malloc_guard(lib::ObMemAttr(MTL_ID(), "PlCodeGen")); + lib::ObMallocHookAttrGuard malloc_guard(lib::ObMemAttr(MTL_ID(), GET_PL_MOD_STRING(pl::OB_PL_CODE_GEN))); if (OB_FAIL(cg.init())) { LOG_WARN("init code generator failed", K(ret)); } else if (OB_FAIL(cg.generate(*routine))) {