Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove useless buf and prealloc in InternalKey #1245

Merged
merged 2 commits into from
Feb 5, 2023
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
52 changes: 17 additions & 35 deletions src/storage/redis_metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ const char *kErrMsgWrongType = "WRONGTYPE Operation against a key holding the wr
const char *kErrMsgKeyExpired = "the key was expired";
const char *kErrMetadataTooShort = "metadata is too short";

InternalKey::InternalKey(Slice input, bool slot_id_encoded) {
slot_id_encoded_ = slot_id_encoded;
InternalKey::InternalKey(Slice input, bool slot_id_encoded) : slot_id_encoded_(slot_id_encoded) {
uint32_t key_size = 0;
uint8_t namespace_size = 0;
GetFixed8(&input, &namespace_size);
Expand All @@ -55,12 +54,10 @@ InternalKey::InternalKey(Slice input, bool slot_id_encoded) {
input.remove_prefix(key_size);
GetFixed64(&input, &version_);
sub_key_ = Slice(input.data(), input.size());
buf_ = nullptr;
memset(prealloc_, '\0', sizeof(prealloc_));
}

InternalKey::InternalKey(Slice ns_key, Slice sub_key, uint64_t version, bool slot_id_encoded) {
slot_id_encoded_ = slot_id_encoded;
InternalKey::InternalKey(Slice ns_key, Slice sub_key, uint64_t version, bool slot_id_encoded)
: slot_id_encoded_(slot_id_encoded) {
uint8_t namespace_size = 0;
GetFixed8(&ns_key, &namespace_size);
namespace_ = Slice(ns_key.data(), namespace_size);
Expand All @@ -71,12 +68,6 @@ InternalKey::InternalKey(Slice ns_key, Slice sub_key, uint64_t version, bool slo
key_ = ns_key;
sub_key_ = sub_key;
version_ = version;
buf_ = nullptr;
memset(prealloc_, '\0', sizeof(prealloc_));
}

InternalKey::~InternalKey() {
if (buf_ != nullptr && buf_ != prealloc_) delete[] buf_;
}

Slice InternalKey::GetNamespace() const { return namespace_; }
Expand All @@ -94,28 +85,24 @@ void InternalKey::Encode(std::string *out) {
if (slot_id_encoded_) {
total += 2;
}
if (total < sizeof(prealloc_)) {
buf_ = prealloc_;
} else {
buf_ = new char[total];
}
EncodeFixed8(buf_ + pos, static_cast<uint8_t>(namespace_.size()));
out->resize(total);
auto buf = out->data();
EncodeFixed8(buf + pos, static_cast<uint8_t>(namespace_.size()));
pos += 1;
memcpy(buf_ + pos, namespace_.data(), namespace_.size());
memcpy(buf + pos, namespace_.data(), namespace_.size());
pos += namespace_.size();
if (slot_id_encoded_) {
EncodeFixed16(buf_ + pos, slotid_);
EncodeFixed16(buf + pos, slotid_);
pos += 2;
}
EncodeFixed32(buf_ + pos, static_cast<uint32_t>(key_.size()));
EncodeFixed32(buf + pos, static_cast<uint32_t>(key_.size()));
pos += 4;
memcpy(buf_ + pos, key_.data(), key_.size());
memcpy(buf + pos, key_.data(), key_.size());
pos += key_.size();
EncodeFixed64(buf_ + pos, version_);
EncodeFixed64(buf + pos, version_);
pos += 8;
memcpy(buf_ + pos, sub_key_.data(), sub_key_.size());
pos += sub_key_.size();
out->assign(buf_, pos);
memcpy(buf + pos, sub_key_.data(), sub_key_.size());
// pos += sub_key_.size();
}

bool InternalKey::operator==(const InternalKey &that) const {
Expand Down Expand Up @@ -161,11 +148,8 @@ void ComposeSlotKeyPrefix(const Slice &ns, int slotid, std::string *output) {
PutFixed16(output, static_cast<uint16_t>(slotid));
}

Metadata::Metadata(RedisType type, bool generate_version) {
flags = (uint8_t)0x0f & type;
expire = 0;
version = 0;
size = 0;
Metadata::Metadata(RedisType type, bool generate_version)
: flags((uint8_t)0x0f & type), expire(0), version(0), size(0) {
if (generate_version) version = generateVersion();
}

Expand Down Expand Up @@ -251,10 +235,8 @@ bool Metadata::Expired() const {
return expire < now;
}

ListMetadata::ListMetadata(bool generate_version) : Metadata(kRedisList, generate_version) {
head = UINT64_MAX / 2;
tail = head;
}
ListMetadata::ListMetadata(bool generate_version)
: Metadata(kRedisList, generate_version), head(UINT64_MAX / 2), tail(head) {}

void ListMetadata::Encode(std::string *dst) {
Metadata::Encode(dst);
Expand Down
4 changes: 1 addition & 3 deletions src/storage/redis_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class InternalKey {
public:
explicit InternalKey(Slice ns_key, Slice sub_key, uint64_t version, bool slot_id_encoded);
explicit InternalKey(Slice input, bool slot_id_encoded);
~InternalKey();
~InternalKey() = default;

Slice GetNamespace() const;
Slice GetKey() const;
Expand All @@ -93,8 +93,6 @@ class InternalKey {
Slice key_;
Slice sub_key_;
uint64_t version_;
char *buf_;
char prealloc_[256];
uint16_t slotid_;
bool slot_id_encoded_;
};
Expand Down