Skip to content

Commit

Permalink
Merge pull request #11113 from NixOS/doc-comment-unordered-map
Browse files Browse the repository at this point in the history
Doc comments: use std::unordered_map
  • Loading branch information
edolstra authored Jul 17, 2024
2 parents 464e592 + f5ebaea commit 621c23b
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/libexpr/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/parser-state.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

namespace nix {

typedef std::map<PosIdx, DocComment> DocCommentMap;
typedef std::unordered_map<PosIdx, DocComment> DocCommentMap;

Expr * parseExprFromBuf(
char * text,
Expand Down
20 changes: 20 additions & 0 deletions src/libexpr/pos-idx.hh
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#pragma once

#include <cinttypes>
#include <functional>

namespace nix {

class PosIdx
{
friend struct LazyPosAcessors;
friend class PosTable;
friend class std::hash<PosIdx>;

private:
uint32_t id;
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions src/libutil/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 1 addition & 2 deletions src/libutil/source-path.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
24 changes: 24 additions & 0 deletions src/libutil/std-hash.hh
Original file line number Diff line number Diff line change
@@ -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
14 changes: 0 additions & 14 deletions src/libutil/util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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...);
}

}

0 comments on commit 621c23b

Please sign in to comment.