Skip to content

Commit

Permalink
Use std::hash on record coord string representation
Browse files Browse the repository at this point in the history
This simplifies the code a bit.
  • Loading branch information
bernhardmgruber committed Jun 8, 2023
1 parent 5b16063 commit 77fb27d
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions include/llama/DumpMapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
# include "StructName.hpp"
# include "View.hpp"

# include <boost/functional/hash.hpp>
# include <fmt/format.h>
# include <functional>
# include <optional>
# include <string>
# include <vector>
Expand All @@ -28,15 +28,9 @@ namespace llama
return computed;
}

template<std::size_t... Coords>
auto toVec(RecordCoord<Coords...>) -> std::vector<std::size_t>
inline auto color(std::string_view recordCoordTags) -> std::size_t
{
return {Coords...};
}

inline auto color(const std::vector<std::size_t>& recordCoord) -> std::size_t
{
auto c = boost::hash<std::vector<std::size_t>>{}(recordCoord) &std::size_t{0xFFFFFF};
auto c = std::hash<std::string_view>{}(recordCoordTags) &std::size_t{0xFFFFFF};
c |= std::size_t{0x404040}; // ensure color per channel is at least 0x40.
return c;
}
Expand Down Expand Up @@ -97,8 +91,7 @@ namespace llama
struct FieldBox
{
ArrayIndex arrayIndex;
std::vector<std::size_t> recordCoord;
std::string_view recordTags;
std::string_view recordCoordTags;
NrAndOffset<std::size_t> nrAndOffset;
std::size_t size;
};
Expand All @@ -122,16 +115,16 @@ namespace llama
using RecordDim = typename Mapping::RecordDim;

auto emitInfo = [&](auto nrAndOffset, std::size_t size) {
infos.push_back({ai, internal::toVec(rc), prettyRecordCoord<RecordDim>(rc), nrAndOffset, size});
infos.push_back({ai, prettyRecordCoord<RecordDim>(rc), nrAndOffset, size});
};

using Type = GetType<RecordDim, decltype(rc)>;
// computed values can come from anywhere, so we can only apply heuristics
auto& blobs = view.blobs();
auto&& ref = view.mapping().compute(ai, rc, blobs);

// try to find the mapped address in one of the blobs
if constexpr(std::is_reference_v<decltype(ref)>)
// if we get a reference, try to find the mapped address in one of the blobs
if constexpr(std::is_lvalue_reference_v<decltype(ref)>)
{
auto address = reinterpret_cast<std::intptr_t>(&ref);
for(std::size_t i = 0; i < blobs.size(); i++)
Expand All @@ -153,10 +146,10 @@ namespace llama
const auto infosBefore = infos.size();

// try to observe written bytes
const auto pattern = std::is_same_v<Type, bool> ? std::uint8_t{0xFF} : std::uint8_t{0xAA};
const auto pattern = std::uint8_t{0xFF};
fillBlobsWithPattern(view, pattern);
ref = Type{}; // a broad range of types is default constructible and should write
// zero bytes
// something zero-ish
auto wasTouched = [&](auto b) { return static_cast<std::uint8_t>(b) != pattern; };
for(std::size_t i = 0; i < Mapping::blobCount; i++)
{
Expand Down Expand Up @@ -210,7 +203,6 @@ namespace llama
const auto [nr, off] = mapping.blobNrAndOffset(ai, rc);
infos.push_back(
{ai,
internal::toVec(rc),
prettyRecordCoord<RecordDim>(rc),
{static_cast<std::size_t>(nr), static_cast<std::size_t>(off)},
sizeof(Type)});
Expand Down Expand Up @@ -326,7 +318,7 @@ namespace llama
}();
auto x = (offset % wrapByteCount) * byteSizeInPixel + blobBlockWidth;
auto y = (offset / wrapByteCount) * byteSizeInPixel + blobY;
const auto fill = internal::color(info.recordCoord);
const auto fill = internal::color(info.recordCoordTags);
const auto width = byteSizeInPixel * info.size;

const auto nextOffset = [&]
Expand Down Expand Up @@ -380,7 +372,7 @@ namespace llama
x + width / 2,
y + byteSizeInPixel * 3 / 4,
internal::formatArrayIndex(info.arrayIndex),
internal::xmlEscape(std::string{info.recordTags}));
internal::xmlEscape(std::string{info.recordCoordTags}));
if(cropBoxes)
svg += R"(</svg>
)";
Expand Down Expand Up @@ -466,7 +458,7 @@ namespace llama
)",
internal::cssClass(std::string{prettyRecordCoord<RecordDim>(rc)}),
byteSizeInPixel * size,
internal::color(internal::toVec(rc)));
internal::color(prettyRecordCoord<RecordDim>(rc)));
});

html += fmt::format(R"(</style>
Expand Down Expand Up @@ -494,9 +486,9 @@ namespace llama
}
html += fmt::format(
R"(<div class="box {0}" title="{1} {2}">{1} {2}</div>)",
internal::cssClass(std::string{info.recordTags}),
internal::cssClass(std::string{info.recordCoordTags}),
internal::formatArrayIndex(info.arrayIndex),
internal::xmlEscape(std::string{info.recordTags}));
internal::xmlEscape(std::string{info.recordCoordTags}));
}
html += R"(</body>
</html>)";
Expand Down

0 comments on commit 77fb27d

Please sign in to comment.