Skip to content

Commit 621c23b

Browse files
authored
Merge pull request #11113 from NixOS/doc-comment-unordered-map
Doc comments: use std::unordered_map
2 parents 464e592 + f5ebaea commit 621c23b

File tree

8 files changed

+50
-20
lines changed

8 files changed

+50
-20
lines changed

src/libexpr/eval.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ struct Constant
130130
typedef std::map<std::string, Value *> ValMap;
131131
#endif
132132

133-
typedef std::map<PosIdx, DocComment> DocCommentMap;
133+
typedef std::unordered_map<PosIdx, DocComment> DocCommentMap;
134134

135135
struct Env
136136
{
@@ -335,7 +335,7 @@ private:
335335
* Associate source positions of certain AST nodes with their preceding doc comment, if they have one.
336336
* Grouped by file.
337337
*/
338-
std::map<SourcePath, DocCommentMap> positionToDocComment;
338+
std::unordered_map<SourcePath, DocCommentMap> positionToDocComment;
339339

340340
LookupPath lookupPath;
341341

src/libexpr/parser-state.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct LexerState
6464
/**
6565
* @brief Maps some positions to a DocComment, where the comment is relevant to the location.
6666
*/
67-
std::map<PosIdx, DocComment> & positionToDocComment;
67+
std::unordered_map<PosIdx, DocComment> & positionToDocComment;
6868

6969
PosTable & positions;
7070
PosTable::Origin origin;

src/libexpr/parser.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
namespace nix {
5050

51-
typedef std::map<PosIdx, DocComment> DocCommentMap;
51+
typedef std::unordered_map<PosIdx, DocComment> DocCommentMap;
5252

5353
Expr * parseExprFromBuf(
5454
char * text,

src/libexpr/pos-idx.hh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#pragma once
22

33
#include <cinttypes>
4+
#include <functional>
45

56
namespace nix {
67

78
class PosIdx
89
{
910
friend struct LazyPosAcessors;
1011
friend class PosTable;
12+
friend class std::hash<PosIdx>;
1113

1214
private:
1315
uint32_t id;
@@ -37,8 +39,26 @@ public:
3739
{
3840
return id == other.id;
3941
}
42+
43+
size_t hash() const noexcept
44+
{
45+
return std::hash<uint32_t>{}(id);
46+
}
4047
};
4148

4249
inline PosIdx noPos = {};
4350

4451
}
52+
53+
namespace std {
54+
55+
template<>
56+
struct hash<nix::PosIdx>
57+
{
58+
std::size_t operator()(nix::PosIdx pos) const noexcept
59+
{
60+
return pos.hash();
61+
}
62+
};
63+
64+
} // namespace std

src/libutil/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ headers = [config_h] + files(
216216
'source-accessor.hh',
217217
'source-path.hh',
218218
'split.hh',
219+
'std-hash.hh',
219220
'strings.hh',
220221
'strings-inline.hh',
221222
'suggestions.hh',

src/libutil/source-path.hh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
#include "ref.hh"
99
#include "canon-path.hh"
1010
#include "source-accessor.hh"
11-
12-
#include <boost/functional/hash.hpp> // for boost::hash_combine
11+
#include "std-hash.hh"
1312

1413
namespace nix {
1514

src/libutil/std-hash.hh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
//!@file Hashing utilities for use with unordered_map, etc. (ie low level implementation logic, not domain logic like
4+
//! Nix hashing)
5+
6+
#include <functional>
7+
8+
namespace nix {
9+
10+
/**
11+
* hash_combine() from Boost. Hash several hashable values together
12+
* into a single hash.
13+
*/
14+
inline void hash_combine(std::size_t & seed) {}
15+
16+
template<typename T, typename... Rest>
17+
inline void hash_combine(std::size_t & seed, const T & v, Rest... rest)
18+
{
19+
std::hash<T> hasher;
20+
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
21+
hash_combine(seed, rest...);
22+
}
23+
24+
} // namespace nix

src/libutil/util.hh

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -375,18 +375,4 @@ inline std::string operator + (std::string_view s1, const char * s2)
375375
return s;
376376
}
377377

378-
/**
379-
* hash_combine() from Boost. Hash several hashable values together
380-
* into a single hash.
381-
*/
382-
inline void hash_combine(std::size_t & seed) { }
383-
384-
template <typename T, typename... Rest>
385-
inline void hash_combine(std::size_t & seed, const T & v, Rest... rest)
386-
{
387-
std::hash<T> hasher;
388-
seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
389-
hash_combine(seed, rest...);
390-
}
391-
392378
}

0 commit comments

Comments
 (0)