2
2
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
3
3
4
4
use std:: collections:: HashMap ;
5
- use std:: hash:: Hasher ;
5
+ use std:: hash:: { BuildHasher , Hasher } ;
6
6
use std:: sync:: Arc ;
7
7
8
8
use fnv:: FnvHasher ;
@@ -11,6 +11,7 @@ use parking_lot::RwLock;
11
11
use crate :: desc:: { Desc , Describer } ;
12
12
use crate :: errors:: { Error , Result } ;
13
13
use crate :: metrics:: { Collector , Metric } ;
14
+ use crate :: nohash:: BuildNoHashHasher ;
14
15
use crate :: proto:: { MetricFamily , MetricType } ;
15
16
16
17
/// An interface for building a metric vector.
@@ -26,7 +27,8 @@ pub trait MetricVecBuilder: Send + Sync + Clone {
26
27
27
28
#[ derive( Debug ) ]
28
29
pub ( crate ) struct MetricVecCore < T : MetricVecBuilder > {
29
- pub children : RwLock < HashMap < u64 , T :: M > > ,
30
+ // the key is pre-hashed, and so we use a no-hash hasher to avoid hashing again.
31
+ pub children : RwLock < HashMap < u64 , T :: M , BuildNoHashHasher > > ,
30
32
pub desc : Desc ,
31
33
pub metric_type : MetricType ,
32
34
pub new_metric : T ,
@@ -62,7 +64,7 @@ impl<T: MetricVecBuilder> MetricVecCore<T> {
62
64
self . get_or_create_metric ( h, vals)
63
65
}
64
66
65
- pub fn get_metric_with < V > ( & self , labels : & HashMap < & str , V > ) -> Result < T :: M >
67
+ pub fn get_metric_with < V , S : BuildHasher > ( & self , labels : & HashMap < & str , V , S > ) -> Result < T :: M >
66
68
where
67
69
V : AsRef < str > + std:: fmt:: Debug ,
68
70
{
@@ -90,7 +92,7 @@ impl<T: MetricVecBuilder> MetricVecCore<T> {
90
92
Ok ( ( ) )
91
93
}
92
94
93
- pub fn delete < V > ( & self , labels : & HashMap < & str , V > ) -> Result < ( ) >
95
+ pub fn delete < V , S : BuildHasher > ( & self , labels : & HashMap < & str , V , S > ) -> Result < ( ) >
94
96
where
95
97
V : AsRef < str > + std:: fmt:: Debug ,
96
98
{
@@ -128,7 +130,7 @@ impl<T: MetricVecBuilder> MetricVecCore<T> {
128
130
Ok ( h. finish ( ) )
129
131
}
130
132
131
- fn hash_labels < V > ( & self , labels : & HashMap < & str , V > ) -> Result < u64 >
133
+ fn hash_labels < V , S : BuildHasher > ( & self , labels : & HashMap < & str , V , S > ) -> Result < u64 >
132
134
where
133
135
V : AsRef < str > + std:: fmt:: Debug ,
134
136
{
@@ -155,7 +157,10 @@ impl<T: MetricVecBuilder> MetricVecCore<T> {
155
157
Ok ( h. finish ( ) )
156
158
}
157
159
158
- fn get_label_values < ' a , V > ( & ' a self , labels : & ' a HashMap < & str , V > ) -> Result < Vec < & ' a str > >
160
+ fn get_label_values < ' a , V , S : BuildHasher > (
161
+ & ' a self ,
162
+ labels : & ' a HashMap < & str , V , S > ,
163
+ ) -> Result < Vec < & ' a str > >
159
164
where
160
165
V : AsRef < str > + std:: fmt:: Debug ,
161
166
{
@@ -212,7 +217,7 @@ impl<T: MetricVecBuilder> MetricVec<T> {
212
217
pub fn create ( metric_type : MetricType , new_metric : T , opts : T :: P ) -> Result < MetricVec < T > > {
213
218
let desc = opts. describe ( ) ?;
214
219
let v = MetricVecCore {
215
- children : RwLock :: new ( HashMap :: new ( ) ) ,
220
+ children : RwLock :: new ( HashMap :: default ( ) ) ,
216
221
desc,
217
222
metric_type,
218
223
new_metric,
@@ -264,7 +269,7 @@ impl<T: MetricVecBuilder> MetricVec<T> {
264
269
/// This method is used for the same purpose as
265
270
/// `get_metric_with_label_values`. See there for pros and cons of the two
266
271
/// methods.
267
- pub fn get_metric_with < V > ( & self , labels : & HashMap < & str , V > ) -> Result < T :: M >
272
+ pub fn get_metric_with < V , S : BuildHasher > ( & self , labels : & HashMap < & str , V , S > ) -> Result < T :: M >
268
273
where
269
274
V : AsRef < str > + std:: fmt:: Debug ,
270
275
{
@@ -294,7 +299,7 @@ impl<T: MetricVecBuilder> MetricVec<T> {
294
299
/// `with` works as `get_metric_with`, but panics if an error occurs. The method allows
295
300
/// neat syntax like:
296
301
/// httpReqs.with(Labels{"status":"404", "method":"POST"}).inc()
297
- pub fn with < V > ( & self , labels : & HashMap < & str , V > ) -> T :: M
302
+ pub fn with < V , S : BuildHasher > ( & self , labels : & HashMap < & str , V , S > ) -> T :: M
298
303
where
299
304
V : AsRef < str > + std:: fmt:: Debug ,
300
305
{
@@ -328,7 +333,7 @@ impl<T: MetricVecBuilder> MetricVec<T> {
328
333
///
329
334
/// This method is used for the same purpose as `delete_label_values`. See
330
335
/// there for pros and cons of the two methods.
331
- pub fn remove < V > ( & self , labels : & HashMap < & str , V > ) -> Result < ( ) >
336
+ pub fn remove < V , S : BuildHasher > ( & self , labels : & HashMap < & str , V , S > ) -> Result < ( ) >
332
337
where
333
338
V : AsRef < str > + std:: fmt:: Debug ,
334
339
{
0 commit comments