From b5f24e53792127b221c539ba22db6375b61bd131 Mon Sep 17 00:00:00 2001 From: Twice Date: Sun, 20 Aug 2023 10:21:46 +0800 Subject: [PATCH] Add returned next pointer for EncodeFixed (#1685) --- src/common/encoding.cc | 2 +- src/common/encoding.h | 19 +++++++++++++------ src/storage/redis_metadata.cc | 22 +++++++--------------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/common/encoding.cc b/src/common/encoding.cc index c2f738c97ed..e17fbd347c9 100644 --- a/src/common/encoding.cc +++ b/src/common/encoding.cc @@ -58,7 +58,7 @@ inline double DecodeDoubleFromUInt64(uint64_t value) { return result; } -void EncodeDouble(char *buf, double value) { EncodeFixed64(buf, EncodeDoubleToUInt64(value)); } +char *EncodeDouble(char *buf, double value) { return EncodeFixed64(buf, EncodeDoubleToUInt64(value)); } void PutDouble(std::string *dst, double value) { PutFixed64(dst, EncodeDoubleToUInt64(value)); } diff --git a/src/common/encoding.h b/src/common/encoding.h index 1fa4b020ffa..d497793068a 100644 --- a/src/common/encoding.h +++ b/src/common/encoding.h @@ -44,17 +44,24 @@ constexpr inline uint32_t BitSwap(uint32_t x) { return __builtin_bswap32(x); } constexpr inline uint64_t BitSwap(uint64_t x) { return __builtin_bswap64(x); } template -constexpr void EncodeFixed(char *buf, T value) { +constexpr char *EncodeFixed(char *buf, T value) { if constexpr (IsLittleEndian()) { value = BitSwap(value); } __builtin_memcpy(buf, &value, sizeof(value)); + return buf + sizeof(value); } -inline void EncodeFixed8(char *buf, uint8_t value) { EncodeFixed(buf, value); } -inline void EncodeFixed16(char *buf, uint16_t value) { EncodeFixed(buf, value); } -inline void EncodeFixed32(char *buf, uint32_t value) { EncodeFixed(buf, value); } -inline void EncodeFixed64(char *buf, uint64_t value) { EncodeFixed(buf, value); } +inline char *EncodeFixed8(char *buf, uint8_t value) { return EncodeFixed(buf, value); } +inline char *EncodeFixed16(char *buf, uint16_t value) { return EncodeFixed(buf, value); } +inline char *EncodeFixed32(char *buf, uint32_t value) { return EncodeFixed(buf, value); } +inline char *EncodeFixed64(char *buf, uint64_t value) { return EncodeFixed(buf, value); } + +inline char *EncodeBuffer(char *buf, rocksdb::Slice value) { + __builtin_memcpy(buf, value.data(), value.size()); + + return buf + value.size(); +} template void PutFixed(std::string *dst, T value) { @@ -95,7 +102,7 @@ inline bool GetFixed16(rocksdb::Slice *input, uint16_t *value) { return GetFixed inline bool GetFixed32(rocksdb::Slice *input, uint32_t *value) { return GetFixed(input, value); } inline bool GetFixed64(rocksdb::Slice *input, uint64_t *value) { return GetFixed(input, value); } -void EncodeDouble(char *buf, double value); +char *EncodeDouble(char *buf, double value); void PutDouble(std::string *dst, double value); double DecodeDouble(const char *ptr); bool GetDouble(rocksdb::Slice *input, double *value); diff --git a/src/storage/redis_metadata.cc b/src/storage/redis_metadata.cc index 4c6b6c5c19f..4157866f53d 100644 --- a/src/storage/redis_metadata.cc +++ b/src/storage/redis_metadata.cc @@ -77,29 +77,21 @@ uint64_t InternalKey::GetVersion() const { return version_; } std::string InternalKey::Encode() const { std::string out; - size_t pos = 0; size_t total = 1 + namespace_.size() + 4 + key_.size() + 8 + sub_key_.size(); if (slot_id_encoded_) { total += 2; } out.resize(total); auto buf = out.data(); - EncodeFixed8(buf + pos, static_cast(namespace_.size())); - pos += 1; - memcpy(buf + pos, namespace_.data(), namespace_.size()); - pos += namespace_.size(); + buf = EncodeFixed8(buf, static_cast(namespace_.size())); + buf = EncodeBuffer(buf, namespace_); if (slot_id_encoded_) { - EncodeFixed16(buf + pos, slotid_); - pos += 2; + buf = EncodeFixed16(buf, slotid_); } - EncodeFixed32(buf + pos, static_cast(key_.size())); - pos += 4; - memcpy(buf + pos, key_.data(), key_.size()); - pos += key_.size(); - EncodeFixed64(buf + pos, version_); - pos += 8; - memcpy(buf + pos, sub_key_.data(), sub_key_.size()); - // pos += sub_key_.size(); + buf = EncodeFixed32(buf, static_cast(key_.size())); + buf = EncodeBuffer(buf, key_); + buf = EncodeFixed64(buf, version_); + EncodeBuffer(buf, sub_key_); return out; }