Commit 314a240e authored by sbc's avatar sbc Committed by Commit bot

[NaCl SDK] nacl_io: Fix excess realloc'ing in memfs nodes.

This was causes extreme performance issues when
untaring large files in naclports (specifically
with python).

Review URL: https://codereview.chromium.org/574803002

Cr-Commit-Position: refs/heads/master@{#295299}
parent 278cf418
...@@ -114,7 +114,7 @@ Error MemFs::OpenWithMode(const Path& path, int open_flags, mode_t mode, ...@@ -114,7 +114,7 @@ Error MemFs::OpenWithMode(const Path& path, int open_flags, mode_t mode,
return EEXIST; return EEXIST;
if (open_flags & O_TRUNC) if (open_flags & O_TRUNC)
static_cast<MemFsNode*>(node.get())->Resize(0); node->FTruncate(0);
} }
*out_node = node; *out_node = node;
......
...@@ -95,24 +95,29 @@ Error MemFsNode::Resize(off_t new_length) { ...@@ -95,24 +95,29 @@ Error MemFsNode::Resize(off_t new_length) {
return EINVAL; return EINVAL;
size_t new_size = static_cast<size_t>(new_length); size_t new_size = static_cast<size_t>(new_length);
size_t new_capacity = data_capacity_;
if (new_size > data_capacity_) { if (new_size > data_capacity_) {
// While the node size is small, grow exponentially. When it starts to get // While the node size is small, grow exponentially. When it starts to get
// larger, grow linearly. // larger, grow linearly.
size_t extra = std::min(new_size, kMaxResizeIncrement); size_t extra = std::min(new_size, kMaxResizeIncrement);
data_capacity_ = new_size + extra; new_capacity = new_size + extra;
} else { } else if (new_length < stat_.st_size) {
data_capacity_ = new_size; // Shrinking capacity
new_capacity = new_size;
} }
data_ = (char*)realloc(data_, data_capacity_); if (new_capacity != data_capacity_) {
if (data_capacity_ != 0) { data_ = (char*)realloc(data_, new_capacity);
if (new_capacity != 0) {
assert(data_ != NULL); assert(data_ != NULL);
if (data_ == NULL) if (data_ == NULL)
return ENOMEM; return ENOMEM;
if (new_length > stat_.st_size) }
memset(data_ + stat_.st_size, 0, new_length - stat_.st_size); data_capacity_ = new_capacity;
} }
if (new_length > stat_.st_size)
memset(data_ + stat_.st_size, 0, new_length - stat_.st_size);
stat_.st_size = new_length; stat_.st_size = new_length;
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment