Chromium Code Reviews| 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 2b354cd0f68043a93b4f70956991a7261a2e422b..7df2a069053ac7fdd47cb281180e83bb5e183367 100644 |
| --- a/net/disk_cache/simple/simple_entry_impl.cc |
| +++ b/net/disk_cache/simple/simple_entry_impl.cc |
| @@ -376,23 +376,14 @@ int SimpleEntryImpl::ReadData(int stream_index, |
| RecordReadResult(cache_type_, READ_RESULT_INVALID_ARGUMENT); |
| return net::ERR_INVALID_ARGUMENT; |
| } |
| - if (pending_operations_.empty() && (offset >= GetDataSize(stream_index) || |
| - offset < 0 || !buf_len)) { |
| - if (net_log_.IsCapturing()) { |
| - net_log_.AddEvent(net::NetLogEventType::SIMPLE_CACHE_ENTRY_READ_END, |
| - CreateNetLogReadWriteCompleteCallback(0)); |
| - } |
| - |
| - RecordReadResult(cache_type_, READ_RESULT_NONBLOCK_EMPTY_RETURN); |
| - return 0; |
| - } |
| - // TODO(clamy): return immediatly when reading from stream 0. |
| - |
| - // TODO(felipeg): Optimization: Add support for truly parallel read |
| - // operations. |
| bool alone_in_queue = |
| pending_operations_.size() == 0 && state_ == STATE_READY; |
| + |
| + if (alone_in_queue) { |
| + return ReadDataInternal(true, stream_index, offset, buf, buf_len, callback); |
| + } |
| + |
| pending_operations_.push(SimpleEntryOperation::ReadOperation( |
| this, stream_index, offset, buf_len, buf, callback, alone_in_queue)); |
| RunNextOperationIfNeeded(); |
| @@ -639,10 +630,8 @@ void SimpleEntryImpl::RunNextOperationIfNeeded() { |
| break; |
| case SimpleEntryOperation::TYPE_READ: |
| RecordReadIsParallelizable(*operation); |
| - ReadDataInternal(operation->index(), |
| - operation->offset(), |
| - operation->buf(), |
| - operation->length(), |
| + ReadDataInternal(false, operation->index(), operation->offset(), |
| + operation->buf(), operation->length(), |
| operation->callback()); |
| break; |
| case SimpleEntryOperation::TYPE_WRITE: |
| @@ -818,11 +807,12 @@ void SimpleEntryImpl::CloseInternal() { |
| } |
| } |
| -void SimpleEntryImpl::ReadDataInternal(int stream_index, |
| - int offset, |
| - net::IOBuffer* buf, |
| - int buf_len, |
| - const CompletionCallback& callback) { |
| +int SimpleEntryImpl::ReadDataInternal(bool try_sync, |
| + int stream_index, |
| + int offset, |
| + net::IOBuffer* buf, |
| + int buf_len, |
| + const CompletionCallback& callback) { |
| DCHECK(io_thread_checker_.CalledOnValidThread()); |
| ScopedOperationRunner operation_runner(this); |
| @@ -833,7 +823,7 @@ void SimpleEntryImpl::ReadDataInternal(int stream_index, |
| } |
| if (state_ == STATE_FAILURE || state_ == STATE_UNINITIALIZED) { |
| - if (!callback.is_null()) { |
| + if (!callback.is_null() && !try_sync) { |
| RecordReadResult(cache_type_, READ_RESULT_BAD_STATE); |
|
Maks Orlovich
2017/04/12 14:55:49
I should probably double-check the condition for t
pasko
2017/04/12 16:12:51
this won't appear on the benchmark, so why bother?
|
| // Note that the API states that client-provided callbacks for entry-level |
| // (i.e. non-backend) operations (e.g. read, write) are invoked even if |
| @@ -845,17 +835,18 @@ void SimpleEntryImpl::ReadDataInternal(int stream_index, |
| net_log_.AddEvent(net::NetLogEventType::SIMPLE_CACHE_ENTRY_READ_END, |
| CreateNetLogReadWriteCompleteCallback(net::ERR_FAILED)); |
| } |
| - return; |
| + return net::ERR_FAILED; |
| } |
| DCHECK_EQ(STATE_READY, state_); |
| if (offset >= GetDataSize(stream_index) || offset < 0 || !buf_len) { |
| - RecordReadResult(cache_type_, READ_RESULT_FAST_EMPTY_RETURN); |
| + RecordReadResult(cache_type_, try_sync ? READ_RESULT_NONBLOCK_EMPTY_RETURN |
| + : READ_RESULT_FAST_EMPTY_RETURN); |
| // If there is nothing to read, we bail out before setting state_ to |
| // STATE_IO_PENDING. |
| - if (!callback.is_null()) |
| + if (!callback.is_null() && !try_sync) |
| base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| base::Bind(callback, 0)); |
| - return; |
| + return 0; |
| } |
| buf_len = std::min(buf_len, GetDataSize(stream_index) - offset); |
| @@ -863,11 +854,11 @@ void SimpleEntryImpl::ReadDataInternal(int stream_index, |
| // Since stream 0 data is kept in memory, it is read immediately. |
| if (stream_index == 0) { |
| int ret_value = ReadStream0Data(buf, offset, buf_len); |
| - if (!callback.is_null()) { |
| + if (!callback.is_null() && !try_sync) { |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| FROM_HERE, base::Bind(callback, ret_value)); |
| } |
| - return; |
| + return ret_value; |
| } |
| state_ = STATE_IO_PENDING; |
| @@ -891,6 +882,7 @@ void SimpleEntryImpl::ReadDataInternal(int stream_index, |
| base::Passed(&entry_stat), |
| base::Passed(&result)); |
| worker_pool_->PostTaskAndReply(FROM_HERE, task, reply); |
| + return net::ERR_IO_PENDING; |
| } |
| void SimpleEntryImpl::WriteDataInternal(int stream_index, |