]> git.hungrycats.org Git - bees/commitdiff
hexdump: be a little more lock-friendly
authorZygo Blaxell <bees@furryterror.org>
Wed, 4 Dec 2024 04:37:48 +0000 (23:37 -0500)
committerZygo Blaxell <bees@furryterror.org>
Wed, 4 Dec 2024 04:39:33 +0000 (23:39 -0500)
hexdump processes a vector as a contiguous sequence of bytes, regardless
of V's value type, so hexdump should get a pointer and use uint8_t to
read the data.

Some vector types have a lock and some atomics in their operator[], so
let's avoid hammering those.

Signed-off-by: Zygo Blaxell <bees@furryterror.org>
include/crucible/hexdump.h

index 7e325917e85984047094be214ef2e281e7e48a1f..2e099c273e06b9873b543e477dafc0df06a7d36d 100644 (file)
@@ -12,12 +12,14 @@ namespace crucible {
        ostream &
        hexdump(ostream &os, const V &v)
        {
-               os << "V { size = " << v.size() << ", data:\n";
-               for (size_t i = 0; i < v.size(); i += 8) {
+               const auto v_size = v.size();
+               const uint8_t* const v_data = reinterpret_cast<uint8_t*>(v.data());
+               os << "V { size = " << v_size << ", data:\n";
+               for (size_t i = 0; i < v_size; i += 8) {
                        string hex, ascii;
                        for (size_t j = i; j < i + 8; ++j) {
-                               if (j < v.size()) {
-                                       uint8_t c = v[j];
+                               if (j < v_size) {
+                                       const uint8_t c = v_data[j];
                                        char buf[8];
                                        sprintf(buf, "%02x ", c);
                                        hex += buf;