Merge pull request #11113 from NixOS/doc-comment-unordered-map

Doc comments: use std::unordered_map
This commit is contained in:
Eelco Dolstra 2024-07-17 16:50:48 +02:00 committed by GitHub
commit 621c23bbea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 50 additions and 20 deletions

View file

@ -130,7 +130,7 @@ struct Constant
typedef std::map<std::string, Value *> ValMap;
#endif
typedef std::map<PosIdx, DocComment> DocCommentMap;
typedef std::unordered_map<PosIdx, DocComment> DocCommentMap;
struct Env
{
@ -335,7 +335,7 @@ private:
* Associate source positions of certain AST nodes with their preceding doc comment, if they have one.
* Grouped by file.
*/
std::map<SourcePath, DocCommentMap> positionToDocComment;
std::unordered_map<SourcePath, DocCommentMap> positionToDocComment;
LookupPath lookupPath;

View file

@ -64,7 +64,7 @@ struct LexerState
/**
* @brief Maps some positions to a DocComment, where the comment is relevant to the location.
*/
std::map<PosIdx, DocComment> & positionToDocComment;
std::unordered_map<PosIdx, DocComment> & positionToDocComment;
PosTable & positions;
PosTable::Origin origin;

View file

@ -48,7 +48,7 @@
namespace nix {
typedef std::map<PosIdx, DocComment> DocCommentMap;
typedef std::unordered_map<PosIdx, DocComment> DocCommentMap;
Expr * parseExprFromBuf(
char * text,

View file

@ -1,6 +1,7 @@
#pragma once
#include <cinttypes>
#include <functional>
namespace nix {
@ -8,6 +9,7 @@ class PosIdx
{
friend struct LazyPosAcessors;
friend class PosTable;
friend class std::hash<PosIdx>;
private:
uint32_t id;
@ -37,8 +39,26 @@ public:
{
return id == other.id;
}
size_t hash() const noexcept
{
return std::hash<uint32_t>{}(id);
}
};
inline PosIdx noPos = {};
}
namespace std {
template<>
struct hash<nix::PosIdx>
{
std::size_t operator()(nix::PosIdx pos) const noexcept
{
return pos.hash();
}
};
} // namespace std

View file

@ -216,6 +216,7 @@ headers = [config_h] + files(
'source-accessor.hh',
'source-path.hh',
'split.hh',
'std-hash.hh',
'strings.hh',
'strings-inline.hh',
'suggestions.hh',

View file

@ -8,8 +8,7 @@
#include "ref.hh"
#include "canon-path.hh"
#include "source-accessor.hh"
#include <boost/functional/hash.hpp> // for boost::hash_combine
#include "std-hash.hh"
namespace nix {

24
src/libutil/std-hash.hh Normal file
View file

@ -0,0 +1,24 @@
#pragma once
//!@file Hashing utilities for use with unordered_map, etc. (ie low level implementation logic, not domain logic like
//! Nix hashing)
#include <functional>
namespace nix {
/**
* hash_combine() from Boost. Hash several hashable values together
* into a single hash.
*/
inline void hash_combine(std::size_t & seed) {}
template<typename T, typename... Rest>
inline void hash_combine(std::size_t & seed, const T & v, Rest... rest)
{
std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
hash_combine(seed, rest...);
}
} // namespace nix

View file

@ -375,18 +375,4 @@ inline std::string operator + (std::string_view s1, const char * s2)
return s;
}
/**
* hash_combine() from Boost. Hash several hashable values together
* into a single hash.
*/
inline void hash_combine(std::size_t & seed) { }
template <typename T, typename... Rest>
inline void hash_combine(std::size_t & seed, const T & v, Rest... rest)
{
std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
hash_combine(seed, rest...);
}
}