1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-27 21:01:16 +02:00

StorePath: Rewrite in C++

On nix-env -qa -f '<nixpkgs>', this reduces maximum RSS by 20970 KiB
and runtime by 0.8%. This is mostly because we're not parsing the hash
part as a hash anymore (just validating that it consists of base-32
characters).

Also, replace storePathToHash() by StorePath::hashPart().
This commit is contained in:
Eelco Dolstra 2020-06-16 14:16:39 +02:00
parent 72e17290d4
commit 759947bf72
21 changed files with 110 additions and 111 deletions

View file

@ -1,59 +1,78 @@
#pragma once
#include "rust-ffi.hh"
#include "types.hh"
namespace nix {
/* See path.rs. */
struct StorePath;
class Store;
struct Hash;
extern "C" {
void ffi_StorePath_drop(void *);
bool ffi_StorePath_less_than(const StorePath & a, const StorePath & b);
bool ffi_StorePath_eq(const StorePath & a, const StorePath & b);
unsigned char * ffi_StorePath_hash_data(const StorePath & p);
}
struct StorePath : rust::Value<3 * sizeof(void *) + 24, ffi_StorePath_drop>
class StorePath
{
std::string baseName;
StorePath(const StorePath & path)
: baseName(path.baseName)
{ }
public:
/* Size of the hash part of store paths, in base-32 characters. */
constexpr static size_t HashLen = 32; // i.e. 160 bits
StorePath() = delete;
static StorePath make(std::string_view path, std::string_view storeDir);
StorePath(std::string_view baseName);
static StorePath make(unsigned char hash[20], std::string_view name);
StorePath(const Hash & hash, std::string_view name);
static StorePath fromBaseName(std::string_view baseName);
StorePath(StorePath && path)
: baseName(std::move(path.baseName))
{ }
rust::String to_string() const;
StorePath & operator = (StorePath && path)
{
baseName = std::move(path.baseName);
return *this;
}
std::string_view to_string() const
{
return baseName;
}
bool operator < (const StorePath & other) const
{
return ffi_StorePath_less_than(*this, other);
return baseName < other.baseName;
}
bool operator == (const StorePath & other) const
{
return ffi_StorePath_eq(*this, other);
return baseName == other.baseName;
}
bool operator != (const StorePath & other) const
{
return !(*this == other);
return baseName != other.baseName;
}
StorePath clone() const;
StorePath clone() const
{
return StorePath(*this);
}
/* Check whether a file name ends with the extension for
derivations. */
bool isDerivation() const;
std::string_view name() const;
unsigned char * hashData() const
std::string_view name() const
{
return ffi_StorePath_hash_data(*this);
return std::string_view(baseName).substr(HashLen + 1);
}
std::string_view hashPart() const
{
return std::string_view(baseName).substr(0, HashLen);
}
static StorePath dummy;
@ -67,9 +86,6 @@ StorePathSet storePathsToSet(const StorePaths & paths);
StorePathSet singleton(const StorePath & path);
/* Size of the hash part of store paths, in base-32 characters. */
const size_t storePathHashLen = 32; // i.e. 160 bits
/* Extension of derivations in the Nix store. */
const std::string drvExtension = ".drv";
@ -107,7 +123,7 @@ namespace std {
template<> struct hash<nix::StorePath> {
std::size_t operator()(const nix::StorePath & path) const noexcept
{
return * (std::size_t *) path.hashData();
return * (std::size_t *) path.to_string().data();
}
};