Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1310)

Unified Diff: net/disk_cache/simple/simple_entry_impl.cc

Issue 23983005: SimpleCache: merge the first and second stream in one file (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/disk_cache/simple/simple_entry_impl.cc
diff --git a/net/disk_cache/simple/simple_entry_impl.cc b/net/disk_cache/simple/simple_entry_impl.cc
index cfc2689622b7b4495aaafc278584f8131c590237..4e8243a96b5e6fd01eaa95bb1b4027e8c8d618cb 100644
--- a/net/disk_cache/simple/simple_entry_impl.cc
+++ b/net/disk_cache/simple/simple_entry_impl.cc
@@ -335,7 +335,7 @@ int SimpleEntryImpl::ReadData(int stream_index,
false));
}
- if (stream_index < 0 || stream_index >= kSimpleEntryFileCount ||
+ if (stream_index < 0 || stream_index >= kSimpleEntryStreamCount ||
buf_len < 0) {
if (net_log_.IsLoggingAllEvents()) {
net_log_.AddEvent(net::NetLog::TYPE_SIMPLE_CACHE_ENTRY_READ_END,
@@ -381,8 +381,8 @@ int SimpleEntryImpl::WriteData(int stream_index,
truncate));
}
- if (stream_index < 0 || stream_index >= kSimpleEntryFileCount || offset < 0 ||
- buf_len < 0) {
+ if (stream_index < 0 || stream_index >= kSimpleEntryStreamCount ||
+ offset < 0 || buf_len < 0) {
if (net_log_.IsLoggingAllEvents()) {
net_log_.AddEvent(
net::NetLog::TYPE_SIMPLE_CACHE_ENTRY_WRITE_END,
@@ -402,16 +402,11 @@ int SimpleEntryImpl::WriteData(int stream_index,
}
ScopedOperationRunner operation_runner(this);
- // Currently, Simple Cache is only used for HTTP, which stores the headers in
- // stream 0 and always writes them with a single, truncating write. Detect
- // these writes and record the size and size changes of the headers. Also,
- // note writes to stream 0 that violate those assumptions.
- if (stream_index == 0) {
- if (offset == 0 && truncate)
- RecordHeaderSizeChange(cache_type_, data_size_[0], buf_len);
- else
- RecordUnexpectedStream0Write(cache_type_);
- }
+ // Stream 0 data is kept in memory, so can be written immediatly if there are
+ // no IO operations pending.
+ if (stream_index == 0 && state_ == STATE_READY &&
+ pending_operations_.size() == 0)
+ return WriteStream0Data(buf, offset, buf_len, truncate);
pasko 2013/09/09 20:01:22 This is an optimization over copying the contents
clamy 2013/09/10 14:35:59 Done.
// We can only do optimistic Write if there is no pending operations, so
// that we are sure that the next call to RunNextOperationIfNeeded will
@@ -676,7 +671,7 @@ void SimpleEntryImpl::CreateEntryInternal(bool have_index,
last_used_ = last_modified_ = base::Time::Now();
// If creation succeeds, we should mark all streams to be saved on close.
- for (int i = 0; i < kSimpleEntryFileCount; ++i)
+ for (int i = 0; i < kSimpleEntryStreamCount; ++i)
have_written_[i] = true;
const base::TimeTicks start_time = base::TimeTicks::Now();
@@ -711,7 +706,7 @@ void SimpleEntryImpl::CloseInternal() {
if (state_ == STATE_READY) {
DCHECK(synchronous_entry_);
state_ = STATE_IO_PENDING;
- for (int i = 0; i < kSimpleEntryFileCount; ++i) {
+ for (int i = 0; i < kSimpleEntryStreamCount; ++i) {
if (have_written_[i]) {
if (GetDataSize(i) == crc32s_end_offset_[i]) {
int32 crc = GetDataSize(i) == 0 ? crc32(0, Z_NULL, 0) : crc32s_[i];
@@ -730,12 +725,13 @@ void SimpleEntryImpl::CloseInternal() {
base::Bind(&SimpleSynchronousEntry::Close,
base::Unretained(synchronous_entry_),
SimpleEntryStat(last_used_, last_modified_, data_size_),
- base::Passed(&crc32s_to_write));
+ base::Passed(&crc32s_to_write),
+ stream_0_data_);
Closure reply = base::Bind(&SimpleEntryImpl::CloseOperationComplete, this);
synchronous_entry_ = NULL;
worker_pool_->PostTaskAndReply(FROM_HERE, task, reply);
- for (int i = 0; i < kSimpleEntryFileCount; ++i) {
+ for (int i = 0; i < kSimpleEntryStreamCount; ++i) {
if (!have_written_[i]) {
SIMPLE_CACHE_UMA(ENUMERATION,
"CheckCRCResult", cache_type_,
@@ -787,6 +783,16 @@ void SimpleEntryImpl::ReadDataInternal(int stream_index,
return;
}
+ // Stream 0 data is kept in memory, so can be read immediately.
pasko 2013/09/09 20:01:22 s/so can be// we do not have an option to post a
clamy 2013/09/10 14:35:59 Done.
+ if (stream_index == 0) {
+ int ret_value = ReadStream0Data(buf, offset, buf_len);
+ if (!callback.is_null()) {
+ MessageLoopProxy::current()
+ ->PostTask(FROM_HERE, base::Bind(callback, ret_value));
+ }
+ return;
+ }
+
buf_len = std::min(buf_len, GetDataSize(stream_index) - offset);
state_ = STATE_IO_PENDING;
@@ -795,14 +801,15 @@ void SimpleEntryImpl::ReadDataInternal(int stream_index,
scoped_ptr<uint32> read_crc32(new uint32());
scoped_ptr<int> result(new int());
- scoped_ptr<base::Time> last_used(new base::Time());
+ scoped_ptr<SimpleEntryStat> entry_stat(
+ new SimpleEntryStat(last_used_, last_modified_, data_size_));
Closure task = base::Bind(
&SimpleSynchronousEntry::ReadData,
base::Unretained(synchronous_entry_),
SimpleSynchronousEntry::EntryOperationData(stream_index, offset, buf_len),
make_scoped_refptr(buf),
read_crc32.get(),
- last_used.get(),
+ entry_stat.get(),
result.get());
Closure reply = base::Bind(&SimpleEntryImpl::ReadOperationComplete,
this,
@@ -810,7 +817,7 @@ void SimpleEntryImpl::ReadDataInternal(int stream_index,
offset,
callback,
base::Passed(&read_crc32),
- base::Passed(&last_used),
+ base::Passed(&entry_stat),
base::Passed(&result));
worker_pool_->PostTaskAndReply(FROM_HERE, task, reply);
}
@@ -848,6 +855,17 @@ void SimpleEntryImpl::WriteDataInternal(int stream_index,
return;
}
+ // Stream 0 data is kept in memory, so can be written immediatly.
+ if (stream_index == 0) {
+ int ret_value = WriteStream0Data(buf, offset, buf_len, truncate);
+ if (!callback.is_null()) {
+ // Postask prevents creating a loop when calling the callback directly.
+ MessageLoopProxy::current()
+ ->PostTask(FROM_HERE, base::Bind(callback, ret_value));
+ }
+ return;
+ }
+
DCHECK_EQ(STATE_READY, state_);
pasko 2013/09/09 20:01:22 this DCHECK belongs better to a place _before_ we
clamy 2013/09/10 14:35:59 Done.
state_ = STATE_IO_PENDING;
if (backend_.get())
@@ -883,6 +901,9 @@ void SimpleEntryImpl::WriteDataInternal(int stream_index,
last_used_ = last_modified_ = base::Time::Now();
have_written_[stream_index] = true;
+ // Writing on stream 1 affects the placement of stream 0 in the file.
+ if (stream_index == 1)
+ have_written_[0] = true;
scoped_ptr<int> result(new int());
Closure task = base::Bind(&SimpleSynchronousEntry::WriteData,
@@ -934,6 +955,9 @@ void SimpleEntryImpl::CreationOperationComplete(
state_ = STATE_READY;
synchronous_entry_ = in_results->sync_entry;
+ stream_0_data_ = in_results->stream_0_data;
+ // The crc was read in SimpleSynchronousEntry.
+ crc_check_state_[0] = CRC_CHECK_DONE;
if (key_.empty()) {
SetKey(synchronous_entry_->key());
} else {
@@ -984,7 +1008,7 @@ void SimpleEntryImpl::ReadOperationComplete(
int offset,
const CompletionCallback& completion_callback,
scoped_ptr<uint32> read_crc32,
- scoped_ptr<base::Time> last_used,
+ scoped_ptr<SimpleEntryStat> entry_stat,
scoped_ptr<int> result) {
DCHECK(io_thread_checker_.CalledOnValidThread());
DCHECK(synchronous_entry_);
@@ -1020,7 +1044,7 @@ void SimpleEntryImpl::ReadOperationComplete(
Closure task = base::Bind(&SimpleSynchronousEntry::CheckEOFRecord,
base::Unretained(synchronous_entry_),
stream_index,
- data_size_[stream_index],
+ data_size_,
crc32s_[stream_index],
new_result.get());
Closure reply = base::Bind(&SimpleEntryImpl::ChecksumOperationComplete,
@@ -1049,10 +1073,7 @@ void SimpleEntryImpl::ReadOperationComplete(
}
EntryOperationComplete(
- stream_index,
- completion_callback,
- SimpleEntryStat(*last_used, last_modified_, data_size_),
- result.Pass());
+ stream_index, completion_callback, *entry_stat, result.Pass());
pasko 2013/09/09 20:01:22 probably .Pass() would be more stylish
clamy 2013/09/10 14:35:59 EntryOperationComplete expects a const SimpleEntry
}
void SimpleEntryImpl::WriteOperationComplete(
@@ -1129,7 +1150,7 @@ void SimpleEntryImpl::UpdateDataFromEntryStat(
last_used_ = entry_stat.last_used;
last_modified_ = entry_stat.last_modified;
- for (int i = 0; i < kSimpleEntryFileCount; ++i) {
+ for (int i = 0; i < kSimpleEntryStreamCount; ++i) {
data_size_[i] = entry_stat.data_size[i];
}
if (backend_.get())
@@ -1138,7 +1159,7 @@ void SimpleEntryImpl::UpdateDataFromEntryStat(
int64 SimpleEntryImpl::GetDiskUsage() const {
int64 file_size = 0;
- for (int i = 0; i < kSimpleEntryFileCount; ++i) {
+ for (int i = 0; i < kSimpleEntryStreamCount; ++i) {
file_size +=
simple_util::GetFileSizeFromKeyAndDataSize(key_, data_size_[i]);
}
@@ -1216,4 +1237,66 @@ void SimpleEntryImpl::RecordWriteDependencyType(
type, WRITE_DEPENDENCY_TYPE_MAX);
}
+int SimpleEntryImpl::ReadStream0Data(net::IOBuffer* buf,
+ int offset,
+ int buf_len) {
+ int read_size = std::min(data_size_[0] - offset, buf_len);
+ if (read_size < 0) {
+ RecordReadResult(cache_type_, READ_RESULT_SUCCESS);
+ return 0;
+ }
+ memcpy(buf->data(), stream_0_data_->data() + offset, read_size);
+ UpdateDataFromEntryStat(
+ SimpleEntryStat(base::Time::Now(), last_modified_, data_size_));
+ RecordReadResult(cache_type_, READ_RESULT_SUCCESS);
+ return read_size;
+}
+
+int SimpleEntryImpl::WriteStream0Data(net::IOBuffer* buf,
+ int offset,
+ int buf_len,
+ bool truncate) {
+ // Currently, Simple Cache is only used for HTTP, which stores the headers in
+ // stream 0 and always writes them with a single, truncating write. Detect
+ // these writes and record the size and size changes of the headers. Also,
+ // note writes to stream 0 that violate those assumptions.
pasko 2013/09/09 20:01:22 nit: not sure: s/note/support/ ?
clamy 2013/09/10 14:35:59 Done.
+ have_written_[0] = true;
+ if (offset == 0 && truncate) {
+ RecordHeaderSizeChange(cache_type_, data_size_[0], buf_len);
+ stream_0_data_ = new IOBuffer(buf_len);
pasko 2013/09/09 20:01:22 please use a GrowableIOBuffer, it might be less op
clamy 2013/09/10 14:35:59 Done.
+ memcpy(stream_0_data_->data(), buf->data(), buf_len);
+ data_size_[0] = buf_len;
+ } else {
+ RecordUnexpectedStream0Write(cache_type_);
+ int buffer_size;
+ if (truncate)
+ buffer_size = offset + buf_len;
+ else
+ buffer_size = std::max(offset + buf_len, data_size_[0]);
+ scoped_refptr<IOBuffer> new_data = new IOBuffer(buffer_size);
+ memset(new_data->data(), 0, buffer_size);
+ if (stream_0_data_) {
+ memcpy(new_data->data(),
+ stream_0_data_->data(),
+ std::min(data_size_[0], buffer_size));
+ }
+ if (buf)
+ memcpy(new_data->data() + offset, buf->data(), buf_len);
+ stream_0_data_ = new_data;
+ data_size_[0] = buffer_size;
+ }
+ base::Time modification_time = base::Time::Now();
+ UpdateDataFromEntryStat(
+ SimpleEntryStat(modification_time, modification_time, data_size_));
+ if (stream_0_data_) {
+ crc32s_[0] = crc32(crc32(0L, Z_NULL, 0),
+ reinterpret_cast<const Bytef*>(stream_0_data_->data()),
+ data_size_[0]);
+ } else {
+ crc32s_[0] = crc32(0L, Z_NULL, 0);
+ }
+ RecordWriteResult(cache_type_, WRITE_RESULT_SUCCESS);
+ return buf_len;
+}
+
} // namespace disk_cache

Powered by Google App Engine
This is Rietveld 408576698