Commit d67de079 authored by Egor Pasko's avatar Egor Pasko Committed by Commit Bot

Simplify cached_metadata_handler.h

The main purpose is to replace the default CacheType argument with a
normal one. The callers are already passing this parameter explicitly in
the vast majority of cases, as it is really helpful to know whether the
operation will be persisted across platform instances.

For ClearCachedMetadata(): all callers are passing the parameter
explicitly. We will continue to need both modes (clearing on disk for
privacy reasons, clearing from memory to free up some memory). Making it
non-default is trivial.

For SetCachedMetadata(): the documentation says that is should always
persist, hence it seems that this was the intention initially. The
kCacheLocally is used only in tests. Making a test-only carveout with
DisableSendToPlatformForTesting(). The reason for not persisting in
those tests is not clear to me, perhaps this makes the tests run faster?

Increasing the size of the class is slightly unfortunate, I could not
find an elegant solution to overcome this. Though the size of these
objects is small compared to all of the serialized data, so it feels OK.

Bug: 1045052
Change-Id: Ifec42fbd66498c6860b32d86a25d3e857a97b4c5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2019246Reviewed-by: default avatarHiroshige Hayashizaki <hiroshige@chromium.org>
Reviewed-by: default avatarLeszek Swirski <leszeks@chromium.org>
Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Auto-Submit: Egor Pasko <pasko@chromium.org>
Commit-Queue: Egor Pasko <pasko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#736869}
parent 970ccde6
...@@ -341,9 +341,9 @@ TEST_F(ScriptStreamingTest, DISABLED_SuppressingStreaming) { ...@@ -341,9 +341,9 @@ TEST_F(ScriptStreamingTest, DISABLED_SuppressingStreaming) {
SingleCachedMetadataHandler* cache_handler = resource_->CacheHandler(); SingleCachedMetadataHandler* cache_handler = resource_->CacheHandler();
EXPECT_TRUE(cache_handler); EXPECT_TRUE(cache_handler);
cache_handler->DisableSendToPlatformForTesting();
cache_handler->SetCachedMetadata(V8CodeCache::TagForCodeCache(cache_handler), cache_handler->SetCachedMetadata(V8CodeCache::TagForCodeCache(cache_handler),
reinterpret_cast<const uint8_t*>("X"), 1, reinterpret_cast<const uint8_t*>("X"), 1);
CachedMetadataHandler::kCacheLocally);
AppendData("function foo() {"); AppendData("function foo() {");
AppendPadding(); AppendPadding();
......
...@@ -237,8 +237,7 @@ static void ProduceCacheInternal( ...@@ -237,8 +237,7 @@ static void ProduceCacheInternal(
cache_handler->ClearCachedMetadata( cache_handler->ClearCachedMetadata(
CachedMetadataHandler::kCacheLocally); CachedMetadataHandler::kCacheLocally);
cache_handler->SetCachedMetadata( cache_handler->SetCachedMetadata(
V8CodeCache::TagForCodeCache(cache_handler), data, length, V8CodeCache::TagForCodeCache(cache_handler), data, length);
CachedMetadataHandler::kSendToPlatform);
} }
TRACE_EVENT_END1( TRACE_EVENT_END1(
...@@ -297,9 +296,9 @@ void V8CodeCache::SetCacheTimeStamp( ...@@ -297,9 +296,9 @@ void V8CodeCache::SetCacheTimeStamp(
SingleCachedMetadataHandler* cache_handler) { SingleCachedMetadataHandler* cache_handler) {
uint64_t now_ms = base::TimeTicks::Now().since_origin().InMilliseconds(); uint64_t now_ms = base::TimeTicks::Now().since_origin().InMilliseconds();
cache_handler->ClearCachedMetadata(CachedMetadataHandler::kCacheLocally); cache_handler->ClearCachedMetadata(CachedMetadataHandler::kCacheLocally);
cache_handler->SetCachedMetadata( cache_handler->SetCachedMetadata(TagForTimeStamp(cache_handler),
TagForTimeStamp(cache_handler), reinterpret_cast<uint8_t*>(&now_ms), reinterpret_cast<uint8_t*>(&now_ms),
sizeof(now_ms), CachedMetadataHandler::kSendToPlatform); sizeof(now_ms));
} }
// static // static
......
...@@ -39,10 +39,7 @@ void ServiceWorkerScriptCachedMetadataHandler::Trace(blink::Visitor* visitor) { ...@@ -39,10 +39,7 @@ void ServiceWorkerScriptCachedMetadataHandler::Trace(blink::Visitor* visitor) {
void ServiceWorkerScriptCachedMetadataHandler::SetCachedMetadata( void ServiceWorkerScriptCachedMetadataHandler::SetCachedMetadata(
uint32_t data_type_id, uint32_t data_type_id,
const uint8_t* data, const uint8_t* data,
size_t size, size_t size) {
CacheType type) {
if (type != kSendToPlatform)
return;
cached_metadata_ = CachedMetadata::Create(data_type_id, data, size); cached_metadata_ = CachedMetadata::Create(data_type_id, data, size);
base::span<const uint8_t> serialized_data = base::span<const uint8_t> serialized_data =
cached_metadata_->SerializedData(); cached_metadata_->SerializedData();
......
...@@ -27,8 +27,7 @@ class ServiceWorkerScriptCachedMetadataHandler ...@@ -27,8 +27,7 @@ class ServiceWorkerScriptCachedMetadataHandler
void Trace(blink::Visitor*) override; void Trace(blink::Visitor*) override;
void SetCachedMetadata(uint32_t data_type_id, void SetCachedMetadata(uint32_t data_type_id,
const uint8_t*, const uint8_t*,
size_t, size_t) override;
CacheType) override;
void ClearCachedMetadata(CacheType) override; void ClearCachedMetadata(CacheType) override;
scoped_refptr<CachedMetadata> GetCachedMetadata( scoped_refptr<CachedMetadata> GetCachedMetadata(
uint32_t data_type_id) const override; uint32_t data_type_id) const override;
......
...@@ -47,8 +47,8 @@ PLATFORM_EXPORT bool ShouldUseIsolatedCodeCache(mojom::RequestContextType, ...@@ -47,8 +47,8 @@ PLATFORM_EXPORT bool ShouldUseIsolatedCodeCache(mojom::RequestContextType,
class CachedMetadataHandler : public GarbageCollected<CachedMetadataHandler> { class CachedMetadataHandler : public GarbageCollected<CachedMetadataHandler> {
public: public:
enum CacheType { enum CacheType {
kSendToPlatform, // send cache data to blink::Platform::cacheMetadata kSendToPlatform, // send cache data to blink::Platform::CacheMetadata
kCacheLocally // cache only in Resource's member variables kCacheLocally // cache only in objects of this class
}; };
// Enum for marking serialized cached metadatas so that the deserializers // Enum for marking serialized cached metadatas so that the deserializers
...@@ -63,7 +63,7 @@ class CachedMetadataHandler : public GarbageCollected<CachedMetadataHandler> { ...@@ -63,7 +63,7 @@ class CachedMetadataHandler : public GarbageCollected<CachedMetadataHandler> {
virtual void Trace(blink::Visitor* visitor) {} virtual void Trace(blink::Visitor* visitor) {}
// Reset existing metadata, to allow setting new data. // Reset existing metadata, to allow setting new data.
virtual void ClearCachedMetadata(CacheType = kCacheLocally) = 0; virtual void ClearCachedMetadata(CacheType) = 0;
// Returns the encoding to which the cache is specific. // Returns the encoding to which the cache is specific.
virtual String Encoding() const = 0; virtual String Encoding() const = 0;
...@@ -88,8 +88,13 @@ class SingleCachedMetadataHandler : public CachedMetadataHandler { ...@@ -88,8 +88,13 @@ class SingleCachedMetadataHandler : public CachedMetadataHandler {
// identifier that is used to distinguish data generated by the caller. // identifier that is used to distinguish data generated by the caller.
virtual void SetCachedMetadata(uint32_t data_type_id, virtual void SetCachedMetadata(uint32_t data_type_id,
const uint8_t*, const uint8_t*,
size_t, size_t) = 0;
CacheType = kSendToPlatform) = 0;
// Permanently disable persisting CachedMetadata in the platform only when it
// is set.
void DisableSendToPlatformForTesting() {
disable_send_to_platform_for_testing_ = true;
}
// Returns cached metadata of the given type associated with this resource. // Returns cached metadata of the given type associated with this resource.
// This cached metadata can be pruned at any time. // This cached metadata can be pruned at any time.
...@@ -98,6 +103,7 @@ class SingleCachedMetadataHandler : public CachedMetadataHandler { ...@@ -98,6 +103,7 @@ class SingleCachedMetadataHandler : public CachedMetadataHandler {
protected: protected:
SingleCachedMetadataHandler() = default; SingleCachedMetadataHandler() = default;
bool disable_send_to_platform_for_testing_ = false;
}; };
} // namespace blink } // namespace blink
......
...@@ -18,22 +18,19 @@ void ScriptCachedMetadataHandler::Trace(blink::Visitor* visitor) { ...@@ -18,22 +18,19 @@ void ScriptCachedMetadataHandler::Trace(blink::Visitor* visitor) {
CachedMetadataHandler::Trace(visitor); CachedMetadataHandler::Trace(visitor);
} }
void ScriptCachedMetadataHandler::SetCachedMetadata( void ScriptCachedMetadataHandler::SetCachedMetadata(uint32_t data_type_id,
uint32_t data_type_id,
const uint8_t* data, const uint8_t* data,
size_t size, size_t size) {
CachedMetadataHandler::CacheType cache_type) {
// Currently, only one type of cached metadata per resource is supported. If // Currently, only one type of cached metadata per resource is supported. If
// the need arises for multiple types of metadata per resource this could be // the need arises for multiple types of metadata per resource this could be
// enhanced to store types of metadata in a map. // enhanced to store types of metadata in a map.
DCHECK(!cached_metadata_); DCHECK(!cached_metadata_);
cached_metadata_ = CachedMetadata::Create(data_type_id, data, size); cached_metadata_ = CachedMetadata::Create(data_type_id, data, size);
if (cache_type == CachedMetadataHandler::kSendToPlatform) if (!disable_send_to_platform_for_testing_)
SendToPlatform(); SendToPlatform();
} }
void ScriptCachedMetadataHandler::ClearCachedMetadata( void ScriptCachedMetadataHandler::ClearCachedMetadata(CacheType cache_type) {
CachedMetadataHandler::CacheType cache_type) {
cached_metadata_ = nullptr; cached_metadata_ = nullptr;
if (cache_type == CachedMetadataHandler::kSendToPlatform) if (cache_type == CachedMetadataHandler::kSendToPlatform)
SendToPlatform(); SendToPlatform();
......
...@@ -34,7 +34,7 @@ class PLATFORM_EXPORT ScriptCachedMetadataHandler final ...@@ -34,7 +34,7 @@ class PLATFORM_EXPORT ScriptCachedMetadataHandler final
std::unique_ptr<CachedMetadataSender>); std::unique_ptr<CachedMetadataSender>);
~ScriptCachedMetadataHandler() override = default; ~ScriptCachedMetadataHandler() override = default;
void Trace(blink::Visitor*) override; void Trace(blink::Visitor*) override;
void SetCachedMetadata(uint32_t, const uint8_t*, size_t, CacheType) override; void SetCachedMetadata(uint32_t, const uint8_t*, size_t) override;
void ClearCachedMetadata(CacheType) override; void ClearCachedMetadata(CacheType) override;
scoped_refptr<CachedMetadata> GetCachedMetadata(uint32_t) const override; scoped_refptr<CachedMetadata> GetCachedMetadata(uint32_t) const override;
......
...@@ -28,12 +28,11 @@ class SourceKeyedCachedMetadataHandler::SingleKeyHandler final ...@@ -28,12 +28,11 @@ class SourceKeyedCachedMetadataHandler::SingleKeyHandler final
void SetCachedMetadata(uint32_t data_type_id, void SetCachedMetadata(uint32_t data_type_id,
const uint8_t* data, const uint8_t* data,
size_t size, size_t size) override {
CacheType cache_type) override {
DCHECK(!parent_->cached_metadata_map_.Contains(key_)); DCHECK(!parent_->cached_metadata_map_.Contains(key_));
parent_->cached_metadata_map_.insert( parent_->cached_metadata_map_.insert(
key_, CachedMetadata::Create(data_type_id, data, size)); key_, CachedMetadata::Create(data_type_id, data, size));
if (cache_type == CachedMetadataHandler::kSendToPlatform) if (!disable_send_to_platform_for_testing_)
parent_->SendToPlatform(); parent_->SendToPlatform();
} }
......
...@@ -293,8 +293,8 @@ TEST(SourceKeyedCachedMetadataHandlerTest, Serialize_SetWithNoSendDoesNotSend) { ...@@ -293,8 +293,8 @@ TEST(SourceKeyedCachedMetadataHandlerTest, Serialize_SetWithNoSendDoesNotSend) {
handler->HandlerForSource(source2); handler->HandlerForSource(source2);
Vector<uint8_t> data1 = {1, 2, 3}; Vector<uint8_t> data1 = {1, 2, 3};
source1_handler->SetCachedMetadata(0xbeef, data1.data(), data1.size(), source1_handler->DisableSendToPlatformForTesting();
CachedMetadataHandler::kCacheLocally); source1_handler->SetCachedMetadata(0xbeef, data1.data(), data1.size());
Vector<uint8_t> data2 = {3, 4, 5, 6}; Vector<uint8_t> data2 = {3, 4, 5, 6};
source2_handler->SetCachedMetadata(0x5eed, data2.data(), data2.size()); source2_handler->SetCachedMetadata(0x5eed, data2.data(), data2.size());
......
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