Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions llvm/include/llvm/CAS/OnDiskTrieRawHashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,38 +133,38 @@ class OnDiskTrieRawHashMap {
bool IsValue = false;
};

class pointer;
class const_pointer : public PointerImpl<ConstValueProxy> {
class OnDiskPtr;
class ConstOnDiskPtr : public PointerImpl<ConstValueProxy> {
public:
const_pointer() = default;
ConstOnDiskPtr() = default;

private:
friend class pointer;
friend class OnDiskPtr;
friend class OnDiskTrieRawHashMap;
using const_pointer::PointerImpl::PointerImpl;
using ConstOnDiskPtr::PointerImpl::PointerImpl;
};

class pointer : public PointerImpl<ValueProxy> {
class OnDiskPtr : public PointerImpl<ValueProxy> {
public:
operator const_pointer() const {
return const_pointer(Value, getOffset(), IsValue);
operator ConstOnDiskPtr() const {
return ConstOnDiskPtr(Value, getOffset(), IsValue);
}

pointer() = default;
OnDiskPtr() = default;

private:
friend class OnDiskTrieRawHashMap;
using pointer::PointerImpl::PointerImpl;
using OnDiskPtr::PointerImpl::PointerImpl;
};

/// Find the value from hash.
///
/// \returns pointer to the value if exists, otherwise returns a non-value
/// pointer that evaluates to `false` when convert to boolean.
const_pointer find(ArrayRef<uint8_t> Hash) const;
ConstOnDiskPtr find(ArrayRef<uint8_t> Hash) const;

/// Helper function to recover a pointer into the trie from file offset.
Expected<const_pointer> recoverFromFileOffset(FileOffset Offset) const;
Expected<ConstOnDiskPtr> recoverFromFileOffset(FileOffset Offset) const;

using LazyInsertOnConstructCB =
function_ref<void(FileOffset TentativeOffset, ValueProxy TentativeValue)>;
Expand All @@ -186,11 +186,11 @@ class OnDiskTrieRawHashMap {
/// The in-memory \a TrieRawHashMap uses LazyAtomicPointer to synchronize
/// simultaneous writes, but that seems dangerous to use in a memory-mapped
/// file in case a process crashes in the busy state.
Expected<pointer> insertLazy(ArrayRef<uint8_t> Hash,
LazyInsertOnConstructCB OnConstruct = nullptr,
LazyInsertOnLeakCB OnLeak = nullptr);
Expected<OnDiskPtr> insertLazy(ArrayRef<uint8_t> Hash,
LazyInsertOnConstructCB OnConstruct = nullptr,
LazyInsertOnLeakCB OnLeak = nullptr);

Expected<pointer> insert(const ConstValueProxy &Value) {
Expected<OnDiskPtr> insert(const ConstValueProxy &Value) {
return insertLazy(Value.Hash, [&](FileOffset, ValueProxy Allocated) {
assert(Allocated.Hash == Value.Hash);
assert(Allocated.Data.size() == Value.Data.size());
Expand Down
23 changes: 12 additions & 11 deletions llvm/lib/CAS/OnDiskTrieRawHashMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ TrieRawHashMapHandle::createRecord(MappedFileRegionArena &Alloc,
return Record;
}

Expected<OnDiskTrieRawHashMap::const_pointer>
Expected<OnDiskTrieRawHashMap::ConstOnDiskPtr>
OnDiskTrieRawHashMap::recoverFromFileOffset(FileOffset Offset) const {
// Check alignment.
if (!isAligned(MappedFileRegionArena::getAlign(), Offset.get()))
Expand All @@ -448,31 +448,31 @@ OnDiskTrieRawHashMap::recoverFromFileOffset(FileOffset Offset) const {
// Looks okay...
TrieRawHashMapHandle::RecordData D =
Impl->Trie.getRecord(SubtrieSlotValue::getDataOffset(Offset));
return const_pointer(D.Proxy, D.getFileOffset());
return ConstOnDiskPtr(D.Proxy, D.getFileOffset());
}

OnDiskTrieRawHashMap::const_pointer
OnDiskTrieRawHashMap::ConstOnDiskPtr
OnDiskTrieRawHashMap::find(ArrayRef<uint8_t> Hash) const {
TrieRawHashMapHandle Trie = Impl->Trie;
assert(Hash.size() == Trie.getNumHashBytes() && "Invalid hash");

SubtrieHandle S = Trie.getRoot();
if (!S)
return const_pointer();
return ConstOnDiskPtr();

TrieHashIndexGenerator IndexGen = Trie.getIndexGen(S, Hash);
size_t Index = IndexGen.next();
for (;;) {
// Try to set the content.
SubtrieSlotValue V = S.load(Index);
if (!V)
return const_pointer();
return ConstOnDiskPtr();

// Check for an exact match.
if (V.isData()) {
TrieRawHashMapHandle::RecordData D = Trie.getRecord(V);
return D.Proxy.Hash == Hash ? const_pointer(D.Proxy, D.getFileOffset())
: const_pointer();
return D.Proxy.Hash == Hash ? ConstOnDiskPtr(D.Proxy, D.getFileOffset())
: ConstOnDiskPtr();
}

Index = IndexGen.next();
Expand All @@ -490,7 +490,7 @@ void SubtrieHandle::reinitialize(uint32_t StartBit, uint32_t NumBits) {
H->NumBits = NumBits;
}

Expected<OnDiskTrieRawHashMap::pointer>
Expected<OnDiskTrieRawHashMap::OnDiskPtr>
OnDiskTrieRawHashMap::insertLazy(ArrayRef<uint8_t> Hash,
LazyInsertOnConstructCB OnConstruct,
LazyInsertOnLeakCB OnLeak) {
Expand Down Expand Up @@ -523,7 +523,8 @@ OnDiskTrieRawHashMap::insertLazy(ArrayRef<uint8_t> Hash,
}

if (S->compare_exchange_strong(Index, Existing, NewRecord->Offset))
return pointer(NewRecord->Proxy, NewRecord->Offset.asDataFileOffset());
return OnDiskPtr(NewRecord->Proxy,
NewRecord->Offset.asDataFileOffset());

// Race means that Existing is no longer empty; fall through...
}
Expand All @@ -540,8 +541,8 @@ OnDiskTrieRawHashMap::insertLazy(ArrayRef<uint8_t> Hash,
if (NewRecord && OnLeak)
OnLeak(NewRecord->Offset.asDataFileOffset(), NewRecord->Proxy,
ExistingRecord.Offset.asDataFileOffset(), ExistingRecord.Proxy);
return pointer(ExistingRecord.Proxy,
ExistingRecord.Offset.asDataFileOffset());
return OnDiskPtr(ExistingRecord.Proxy,
ExistingRecord.Offset.asDataFileOffset());
}

// Sink the existing content as long as the indexes match.
Expand Down
10 changes: 5 additions & 5 deletions llvm/unittests/CAS/OnDiskTrieRawHashMapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ TEST_P(OnDiskTrieRawHashMapTestFixture, General) {
std::optional<FileOffset> Offset;
std::optional<MutableArrayRef<char>> Data;
{
std::optional<OnDiskTrieRawHashMap::pointer> Insertion;
std::optional<OnDiskTrieRawHashMap::OnDiskPtr> Insertion;
ASSERT_THAT_ERROR(Trie1->insert({Hash0, Data0v1}).moveInto(Insertion),
Succeeded());
EXPECT_EQ(Hash0, (*Insertion)->Hash);
Expand Down Expand Up @@ -128,7 +128,7 @@ TEST_P(OnDiskTrieRawHashMapTestFixture, General) {

// Recover from an offset.
{
OnDiskTrieRawHashMap::const_pointer Recovered;
OnDiskTrieRawHashMap::ConstOnDiskPtr Recovered;
ASSERT_THAT_ERROR(Trie1->recoverFromFileOffset(*Offset).moveInto(Recovered),
Succeeded());
ASSERT_TRUE(Recovered);
Expand All @@ -140,14 +140,14 @@ TEST_P(OnDiskTrieRawHashMapTestFixture, General) {
// Recover from a bad offset.
{
FileOffset BadOffset(1);
OnDiskTrieRawHashMap::const_pointer Recovered;
OnDiskTrieRawHashMap::ConstOnDiskPtr Recovered;
ASSERT_THAT_ERROR(
Trie1->recoverFromFileOffset(BadOffset).moveInto(Recovered), Failed());
}

// Insert another thing.
{
std::optional<OnDiskTrieRawHashMap::pointer> Insertion;
std::optional<OnDiskTrieRawHashMap::OnDiskPtr> Insertion;
ASSERT_THAT_ERROR(Trie1->insert({Hash1, Data1}).moveInto(Insertion),
Succeeded());
EXPECT_EQ(Hash1, (*Insertion)->Hash);
Expand Down Expand Up @@ -210,7 +210,7 @@ TEST(OnDiskTrieRawHashMapTest, OutOfSpace) {
auto Hash0 = ArrayRef(Hash0Bytes);
constexpr StringLiteral Data0v1Bytes = "data0.v1";
ArrayRef<char> Data0v1 = ArrayRef(Data0v1Bytes.data(), Data0v1Bytes.size());
std::optional<OnDiskTrieRawHashMap::pointer> Insertion;
std::optional<OnDiskTrieRawHashMap::OnDiskPtr> Insertion;
ASSERT_THAT_ERROR(Trie->insert({Hash0, Data0v1}).moveInto(Insertion),
Failed());
}
Expand Down