forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blink: Add ParkableStringManager, don't track off-main thread strings.
ParkableStringManager tracks all the current ParkableStrings. This replace ParkableStringTable. Since strings not on the main thread were never parked, don't remember them at all. Bug: 877044 Change-Id: I17ad0b4fc38264ddb344776a5b202fa5475bb906 Reviewed-on: https://chromium-review.googlesource.com/1199383 Commit-Queue: Benoit L <lizeb@chromium.org> Reviewed-by: Alexander Timin <altimin@chromium.org> Reviewed-by: Kentaro Hara <haraken@chromium.org> Cr-Commit-Position: refs/heads/master@{#588843}
- Loading branch information
Benoit Lize
authored and
Commit Bot
committed
Sep 5, 2018
1 parent
1677f1e
commit 5a1630f
Showing
8 changed files
with
216 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
third_party/blink/renderer/platform/bindings/parkable_string_manager.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "third_party/blink/renderer/platform/bindings/parkable_string_manager.h" | ||
|
||
#include <utility> | ||
|
||
#include "base/bind.h" | ||
#include "base/macros.h" | ||
#include "base/metrics/histogram_macros.h" | ||
#include "third_party/blink/public/platform/platform.h" | ||
#include "third_party/blink/public/platform/web_thread.h" | ||
#include "third_party/blink/renderer/platform/bindings/parkable_string.h" | ||
#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h" | ||
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h" | ||
#include "third_party/blink/renderer/platform/wtf/wtf.h" | ||
|
||
namespace blink { | ||
|
||
ParkableStringManager& ParkableStringManager::Instance() { | ||
DCHECK(IsMainThread()); | ||
static ParkableStringManager instance; | ||
return instance; | ||
} | ||
|
||
ParkableStringManager::~ParkableStringManager() {} | ||
|
||
void ParkableStringManager::SetRendererBackgrounded(bool backgrounded) { | ||
backgrounded_ = backgrounded; | ||
|
||
if (backgrounded_) { | ||
scoped_refptr<base::SingleThreadTaskRunner> task_runner = | ||
Platform::Current()->CurrentThread()->GetTaskRunner(); | ||
if (task_runner) { // nullptr in tests. | ||
task_runner->PostDelayedTask( | ||
FROM_HERE, | ||
base::BindOnce(&ParkableStringManager::ParkAllIfRendererBackgrounded, | ||
base::Unretained(this)), | ||
base::TimeDelta::FromSeconds(10)); | ||
} | ||
} | ||
} | ||
|
||
bool ParkableStringManager::IsRendererBackgrounded() const { | ||
return backgrounded_; | ||
} | ||
|
||
// static | ||
bool ParkableStringManager::ShouldPark(const StringImpl* string) { | ||
// Don't attempt to park strings smaller than this size. | ||
static constexpr unsigned int kSizeThreshold = 10000; | ||
// TODO(lizeb): Consider parking non-main thread strings. | ||
return string && string->length() > kSizeThreshold && IsMainThread(); | ||
} | ||
|
||
scoped_refptr<ParkableStringImpl> ParkableStringManager::Add( | ||
scoped_refptr<StringImpl>&& string) { | ||
StringImpl* raw_ptr = string.get(); | ||
auto it = table_.find(raw_ptr); | ||
if (it != table_.end()) | ||
return it->value; | ||
|
||
auto new_parkable_string = | ||
base::MakeRefCounted<ParkableStringImpl>(std::move(string)); | ||
table_.insert(raw_ptr, new_parkable_string.get()); | ||
return new_parkable_string; | ||
} | ||
|
||
void ParkableStringManager::Remove(StringImpl* string) { | ||
DCHECK(ShouldPark(string)); | ||
auto it = table_.find(string); | ||
DCHECK(it != table_.end()); | ||
table_.erase(it); | ||
} | ||
|
||
void ParkableStringManager::ParkAllIfRendererBackgrounded() { | ||
DCHECK(IsMainThread()); | ||
if (!IsRendererBackgrounded()) | ||
return; | ||
|
||
size_t total_size = 0, count = 0; | ||
for (ParkableStringImpl* str : table_.Values()) { | ||
str->Park(); | ||
total_size += str->CharactersSizeInBytes(); | ||
count += 1; | ||
} | ||
|
||
size_t total_size_kb = total_size / 1000; | ||
UMA_HISTOGRAM_COUNTS_100000("Memory.MovableStringsTotalSizeKb", | ||
total_size_kb); | ||
UMA_HISTOGRAM_COUNTS_1000("Memory.MovableStringsCount", table_.size()); | ||
} | ||
|
||
ParkableStringManager::ParkableStringManager() | ||
: backgrounded_(false), table_() {} | ||
|
||
} // namespace blink |
59 changes: 59 additions & 0 deletions
59
third_party/blink/renderer/platform/bindings/parkable_string_manager.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_PARKABLE_STRING_MANAGER_H_ | ||
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_PARKABLE_STRING_MANAGER_H_ | ||
|
||
#include "base/macros.h" | ||
#include "base/memory/scoped_refptr.h" | ||
#include "base/single_thread_task_runner.h" | ||
#include "third_party/blink/renderer/platform/platform_export.h" | ||
#include "third_party/blink/renderer/platform/wtf/allocator.h" | ||
#include "third_party/blink/renderer/platform/wtf/hash_functions.h" | ||
#include "third_party/blink/renderer/platform/wtf/hash_map.h" | ||
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h" | ||
|
||
namespace blink { | ||
|
||
class ParkableString; | ||
class ParkableStringImpl; | ||
|
||
// Manages all the ParkableStrings, and parks eligible strings after the | ||
// renderer has been backgrounded. | ||
// Main Thread only. | ||
class PLATFORM_EXPORT ParkableStringManager { | ||
USING_FAST_MALLOC(ParkableStringManager); | ||
|
||
public: | ||
static ParkableStringManager& Instance(); | ||
~ParkableStringManager(); | ||
|
||
void SetRendererBackgrounded(bool backgrounded); | ||
bool IsRendererBackgrounded() const; | ||
|
||
// Whether a string is parkable or not. Can be called from any thread. | ||
static bool ShouldPark(const StringImpl* string); | ||
|
||
private: | ||
friend class ParkableString; | ||
friend class ParkableStringImpl; | ||
|
||
scoped_refptr<ParkableStringImpl> Add(scoped_refptr<StringImpl>&&); | ||
void Remove(StringImpl*); | ||
|
||
void ParkAllIfRendererBackgrounded(); | ||
|
||
ParkableStringManager(); | ||
|
||
bool backgrounded_; | ||
HashMap<StringImpl*, ParkableStringImpl*, PtrHash<StringImpl>> table_; | ||
|
||
FRIEND_TEST_ALL_PREFIXES(ParkableStringTest, TableSimple); | ||
FRIEND_TEST_ALL_PREFIXES(ParkableStringTest, TableMultiple); | ||
DISALLOW_COPY_AND_ASSIGN(ParkableStringManager); | ||
}; | ||
|
||
} // namespace blink | ||
|
||
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_PARKABLE_STRING_MANAGER_H_ |
Oops, something went wrong.