From bd1b57f930569cbb976a2765f52fd7ca36f1ba50 Mon Sep 17 00:00:00 2001 From: "dlenev@mockturtle.local" <> Date: Wed, 1 Nov 2006 15:41:48 +0300 Subject: [PATCH] Small cleanup in code handling stored routines/table prelocking. Use lazy initialization for Query_tables_list::sroutines hash. This step should significantly decrease amount of memory consumed by stored routines as we no longer will allocate chunk of memory required for this HASH for each statement in routine. --- include/hash.h | 2 ++ sql/sp.cc | 4 ++++ sql/sql_lex.cc | 11 ++++++++++- sql/sql_lex.h | 6 +++++- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/hash.h b/include/hash.h index 8f5ff21ae5e..4d6ee77fa0c 100644 --- a/include/hash.h +++ b/include/hash.h @@ -65,6 +65,8 @@ my_bool hash_check(HASH *hash); /* Only in debug library */ #define hash_clear(H) bzero((char*) (H),sizeof(*(H))) #define hash_inited(H) ((H)->array.buffer != 0) +#define hash_init_opt(A,B,C,D,E,F,G,H) \ + (!hash_inited(A) && _hash_init(A,B,C,D,E,F,G, H CALLER_INFO)) #ifdef __cplusplus } diff --git a/sql/sp.cc b/sql/sp.cc index 49dbed79fe5..5d5c2b148d4 100644 --- a/sql/sp.cc +++ b/sql/sp.cc @@ -1369,6 +1369,10 @@ static bool add_used_routine(LEX *lex, Query_arena *arena, const LEX_STRING *key, TABLE_LIST *belong_to_view) { + hash_init_opt(&lex->sroutines, system_charset_info, + Query_tables_list::START_SROUTINES_HASH_SIZE, + 0, 0, sp_sroutine_key, 0, 0); + if (!hash_search(&lex->sroutines, (byte *)key->str, key->length)) { Sroutine_hash_entry *rn= diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 405f576ac04..24b9766a658 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -1634,9 +1634,18 @@ void Query_tables_list::reset_query_tables_list(bool init) query_tables_last= &query_tables; query_tables_own_last= 0; if (init) - hash_init(&sroutines, system_charset_info, 0, 0, 0, sp_sroutine_key, 0, 0); + { + /* + We delay real initialization of hash (and therefore related + memory allocation) until first insertion into this hash. + */ + hash_clear(&sroutines); + } else if (sroutines.records) + { + /* Non-zero sroutines.records means that hash was initialized. */ my_hash_reset(&sroutines); + } sroutines_list.empty(); sroutines_list_own_last= sroutines_list.next; sroutines_list_own_elements= 0; diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 378f968118e..bfe7d9518f3 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -742,7 +742,11 @@ public: 0 - indicates that this query does not need prelocking. */ TABLE_LIST **query_tables_own_last; - /* Set of stored routines called by statement. */ + /* + Set of stored routines called by statement. + (Note that we use lazy-initialization for this hash). + */ + enum { START_SROUTINES_HASH_SIZE= 16 }; HASH sroutines; /* List linking elements of 'sroutines' set. Allows you to add new elements