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

avoid unnecessary memory copy #819

Merged
merged 1 commit into from
Jul 13, 2020
Merged

avoid unnecessary memory copy #819

merged 1 commit into from
Jul 13, 2020

Conversation

wzk784533
Copy link
Contributor

@wzk784533 wzk784533 commented Jul 11, 2020

During the compaction phase, in this function

Status DBImpl::WriteLevel0Table(MemTable* mem, VersionEdit* edit,
                                Version* base)

It will then call

BuildTable(const std::string& dbname, Env* env, const Options& options,
                  TableCache* table_cache, Iterator* iter, FileMetaData* meta)

In BuildTable function, the code fragment is this:

TableBuilder* builder = new TableBuilder(options, file);
    meta->smallest.DecodeFrom(iter->key());
    for (; iter->Valid(); iter->Next()) {
      Slice key = iter->key();
      meta->largest.DecodeFrom(key);
      builder->Add(key, iter->value());
    }

In the iterator loop, call meta->largest.DecodeFrom(key) every time is unnecessary, which will copy every key to meta's largest field. We just need to copy the last key after the iteration is done.

bool DecodeFrom(const Slice& s) {
    rep_.assign(s.data(), s.size());
    return !rep_.empty();
  }

DecodeFrom just copy the slice to rep_. When there are many keys in Skiplist, and each key is a very long string, the wasting of CPU cycles are considerable.

Copy link
Member

@pwnall pwnall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great find. Thank you!

@pwnall
Copy link
Member

pwnall commented Jul 13, 2020

I'll get this PR through our internal repository. It will get merged in a few days. Thank you very much for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants