Skip to content

Commit c304e71

Browse files
committed
[RP003] Revives my_hash_*() functions and changes to use HASH instead of std::unordered_[map/set]
std::unordered_[map/set] are estimated to waste CPU data cache more than the legacy mysys HASH implementation. So, reverts to use the legacy functions for frequent paths. - The filename "hash.[cc/h]" changed to "my_hash.[cc/h]" to avoid name confliction - table_def_cache, Table_cache, Tablespace_hash_set, schema_set have been changed This change is almost consist of the following commits partial reverting. > commit a8a7961 > Author: Steinar H. Gunderson <steinar.gunderson@oracle.com> > Date: Fri Jul 7 12:04:18 2017 +0200 > > Bug #25997748: MIGRATE FROM HASH TO STD::UNORDERED_MAP [patch 15] > commit b77037d > Author: Steinar H. Gunderson <steinar.gunderson@oracle.com> > Date: Fri May 26 13:05:59 2017 +0200 > > Bug #25997748: MIGRATE FROM HASH TO STD::UNORDERED_MAP [patch 8, noclose] > commit 532421a > Author: Steinar H. Gunderson <steinar.gunderson@oracle.com> > Date: Tue May 2 10:41:39 2017 +0200 > > Bug #25997748: MIGRATE FROM HASH TO STD::UNORDERED_MAP * Just picked up reset_dynamic() and pop_dynamic() from > commit 781d234 > Author: Jon Olav Hauglid <jon.hauglid@oracle.com> > Date: Tue Jul 30 10:28:15 2019 +0200 > > Bug#30112842: REMOVE DEAD CODE IDENTIFIED BY FASTCOV [noclose]
1 parent 023a9f0 commit c304e71

File tree

22 files changed

+1156
-173
lines changed

22 files changed

+1156
-173
lines changed

MYSQL_VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ MYSQL_VERSION_MINOR=0
33
MYSQL_VERSION_PATCH=42
44
MYSQL_VERSION_EXTRA=
55
MYSQL_VERSION_STABILITY="LTS"
6-
MYSQL_RP_REVISION="-RP002"
6+
MYSQL_RP_REVISION="-RP003"

include/my_hash.h

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
2+
3+
This program is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License as published by
5+
the Free Software Foundation; version 2 of the License.
6+
7+
This program is distributed in the hope that it will be useful,
8+
but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
GNU General Public License for more details.
11+
12+
You should have received a copy of the GNU General Public License
13+
along with this program; if not, write to the Free Software
14+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15+
16+
#ifndef HASH_INCLUDED
17+
#define HASH_INCLUDED
18+
19+
/**
20+
@file include/hash.h
21+
Dynamic hashing of record with different key-length.
22+
*/
23+
24+
#include <stddef.h>
25+
#include <sys/types.h>
26+
#include <new>
27+
28+
#include "m_ctype.h"
29+
#include "my_inttypes.h"
30+
#include "my_sys.h" /* DYNAMIC_ARRAY */
31+
#include "mysql/psi/psi_memory.h"
32+
33+
struct CHARSET_INFO;
34+
35+
/*
36+
Overhead to store an element in hash
37+
Can be used to approximate memory consumption for a hash
38+
*/
39+
#define HASH_OVERHEAD (sizeof(char *) * 2)
40+
41+
/* flags for hash_init */
42+
#define HASH_UNIQUE 1 /* hash_insert fails on duplicate key */
43+
44+
typedef uint my_hash_value_type;
45+
46+
/**
47+
Callback for extracting key and key length from user data in a HASH.
48+
@param arg Pointer to user data.
49+
@param[out] length Store key length here.
50+
@return Pointer to key to be hashed.
51+
52+
@note Was my_hash_get_key, with lots of C-style casting when calling
53+
my_hash_init. Renamed to force build error (since signature changed)
54+
in case someone keeps following that coding style.
55+
*/
56+
typedef const uchar *(*hash_get_key_function)(const uchar *arg, size_t *length);
57+
58+
typedef void (*hash_free_element_function)(void *);
59+
60+
typedef struct st_hash {
61+
st_hash()
62+
: key_length(0),
63+
blength(0),
64+
records(0),
65+
flags(0),
66+
get_key(nullptr),
67+
free_element(nullptr),
68+
charset(nullptr),
69+
m_psi_key(PSI_NOT_INSTRUMENTED) {
70+
array = DYNAMIC_ARRAY();
71+
}
72+
size_t key_length; /* Length of key if const length */
73+
size_t blength;
74+
ulong records;
75+
uint flags;
76+
DYNAMIC_ARRAY array; /* Place for hash_keys */
77+
hash_get_key_function get_key;
78+
hash_free_element_function free_element;
79+
const CHARSET_INFO *charset;
80+
PSI_memory_key m_psi_key;
81+
} HASH;
82+
83+
/* A search iterator state */
84+
typedef uint HASH_SEARCH_STATE;
85+
86+
bool my_hash_init(HASH *hash, const CHARSET_INFO *charset, ulong reserve_size,
87+
size_t key_length, hash_get_key_function get_key,
88+
hash_free_element_function free_element, uint flags,
89+
PSI_memory_key psi_key);
90+
void my_hash_free(HASH *tree);
91+
void my_hash_reset(HASH *hash);
92+
uchar *my_hash_element(HASH *hash, ulong idx);
93+
uchar *my_hash_search(const HASH *info, const uchar *key, size_t length);
94+
uchar *my_hash_search_using_hash_value(const HASH *info,
95+
my_hash_value_type hash_value,
96+
const uchar *key, size_t length);
97+
my_hash_value_type my_calc_hash(const HASH *info, const uchar *key,
98+
size_t length);
99+
uchar *my_hash_first(const HASH *info, const uchar *key, size_t length,
100+
HASH_SEARCH_STATE *state);
101+
uchar *my_hash_first_from_hash_value(const HASH *info,
102+
my_hash_value_type hash_value,
103+
const uchar *key, size_t length,
104+
HASH_SEARCH_STATE *state);
105+
uchar *my_hash_next(const HASH *info, const uchar *key, size_t length,
106+
HASH_SEARCH_STATE *state);
107+
bool my_hash_insert(HASH *info, const uchar *data);
108+
bool my_hash_delete(HASH *hash, uchar *record);
109+
bool my_hash_update(HASH *hash, uchar *record, uchar *old_key,
110+
size_t old_key_length);
111+
void my_hash_replace(HASH *hash, HASH_SEARCH_STATE *state, uchar *new_row);
112+
bool my_hash_check(HASH *hash); /* Only in debug library */
113+
114+
inline void my_hash_clear(HASH *h) { new (h) st_hash(); }
115+
inline bool my_hash_inited(const HASH *h) { return h->blength != 0; }
116+
117+
#endif // HASH_INCLUDED

include/my_sys.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,9 @@ extern bool my_init_dynamic_array(DYNAMIC_ARRAY *array, PSI_memory_key key,
793793
/* Some functions are still in use in C++, because HASH uses DYNAMIC_ARRAY */
794794
extern bool insert_dynamic(DYNAMIC_ARRAY *array, const void *element);
795795
extern void *alloc_dynamic(DYNAMIC_ARRAY *array);
796+
extern void *pop_dynamic(DYNAMIC_ARRAY *);
796797
extern void delete_dynamic(DYNAMIC_ARRAY *array);
798+
static inline void reset_dynamic(DYNAMIC_ARRAY *array) { array->elements = 0; }
797799

798800
extern bool init_dynamic_string(DYNAMIC_STRING *str, const char *init_str,
799801
size_t init_alloc);

mysys/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ SET(MYSYS_SOURCES
7474
my_fstream.cc
7575
my_gethwaddr.cc
7676
my_getwd.cc
77+
my_hash.cc
7778
my_init.cc
7879
my_lib.cc
7980
my_malloc.cc

mysys/array.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,24 @@ void *alloc_dynamic(DYNAMIC_ARRAY *array) {
165165
return array->buffer + (array->elements++ * array->size_of_element);
166166
}
167167

168+
/*
169+
Pop last element from array.
170+
171+
SYNOPSIS
172+
pop_dynamic()
173+
array
174+
175+
RETURN VALUE
176+
pointer Ok
177+
0 Array is empty
178+
*/
179+
180+
void *pop_dynamic(DYNAMIC_ARRAY *array) {
181+
if (array->elements)
182+
return array->buffer + (--array->elements * array->size_of_element);
183+
return 0;
184+
}
185+
168186
/*
169187
Empty array by freeing all memory
170188

0 commit comments

Comments
 (0)