forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for string refcount thrashing in setAttribute and getAttribute
These APIs were previously identified as hot paths that exhibited regressions when attempting to make StringImpl's refcount atomic. We'll use these tests to drive down refcount thrashing on these paths and ensure it stays low. Bug: 1083392 Change-Id: I5e92687b49cbf8da12f1676609e3f7755965530c Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3117006 Reviewed-by: Kentaro Hara <haraken@chromium.org> Commit-Queue: Kevin Babbitt <kbabbitt@microsoft.com> Cr-Commit-Position: refs/heads/main@{#916479}
- Loading branch information
Showing
4 changed files
with
119 additions
and
1 deletion.
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
102 changes: 102 additions & 0 deletions
102
third_party/blink/renderer/bindings/modules/v8/v8_element_test.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,102 @@ | ||
// Copyright (c) 2021 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/bindings/core/v8/script_source_code.h" | ||
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h" | ||
#include "third_party/blink/renderer/core/script/classic_script.h" | ||
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h" | ||
#include "third_party/blink/renderer/platform/wtf/text/atomic_string_table.h" | ||
|
||
namespace blink { | ||
|
||
class V8ElementTest : public BindingTestSupportingGC { | ||
protected: | ||
void SetUp() override { | ||
// Precondition: test strings should not be in the AtomicStringTable yet. | ||
DCHECK(AtomicStringTable::Instance().WeakFind("test-attribute").IsNull()); | ||
DCHECK(AtomicStringTable::Instance().WeakFind("test-value").IsNull()); | ||
} | ||
|
||
void TearDown() override { | ||
PreciselyCollectGarbage(); | ||
|
||
// Postcondition: test strings should have been released from the | ||
// AtomicStringTable | ||
DCHECK(AtomicStringTable::Instance().WeakFind("test-attribute").IsNull()); | ||
DCHECK(AtomicStringTable::Instance().WeakFind("test-value").IsNull()); | ||
} | ||
}; | ||
|
||
v8::Local<v8::Value> Eval(const String& source, V8TestingScope& scope) { | ||
return ClassicScript::CreateUnspecifiedScript(ScriptSourceCode(source)) | ||
->RunScriptAndReturnValue(&scope.GetWindow()); | ||
} | ||
|
||
TEST_F(V8ElementTest, SetAttributeOperationCallback) { | ||
V8TestingScope scope; | ||
|
||
Eval("document.body.setAttribute('test-attribute', 'test-value')", scope); | ||
EXPECT_FALSE( | ||
AtomicStringTable::Instance().WeakFind("test-attribute").IsNull()); | ||
EXPECT_FALSE(AtomicStringTable::Instance().WeakFind("test-value").IsNull()); | ||
|
||
#if DCHECK_IS_ON() | ||
AtomicString test_attribute("test-attribute"); | ||
EXPECT_EQ(test_attribute.Impl()->RefCountChangeCountForTesting(), 27u); | ||
AtomicString test_value("test-value"); | ||
EXPECT_EQ(test_value.Impl()->RefCountChangeCountForTesting(), 15u); | ||
#endif | ||
|
||
// Trigger a low memory notification. This will signal V8 to clear its | ||
// CompilationCache, which is needed because cached compiled code may be | ||
// holding references to externalized AtomicStrings. | ||
scope.GetIsolate()->LowMemoryNotification(); | ||
} | ||
|
||
TEST_F(V8ElementTest, GetAttributeOperationCallback_NonExisting) { | ||
V8TestingScope scope; | ||
|
||
Eval("document.body.getAttribute('test-attribute')", scope); | ||
EXPECT_FALSE( | ||
AtomicStringTable::Instance().WeakFind("test-attribute").IsNull()); | ||
EXPECT_TRUE(AtomicStringTable::Instance().WeakFind("test-value").IsNull()); | ||
|
||
#if DCHECK_IS_ON() | ||
AtomicString test_attribute("test-attribute"); | ||
EXPECT_EQ(test_attribute.Impl()->RefCountChangeCountForTesting(), 14u); | ||
#endif | ||
|
||
// Trigger a low memory notification. This will signal V8 to clear its | ||
// CompilationCache, which is needed because cached compiled code may be | ||
// holding references to externalized AtomicStrings. | ||
scope.GetIsolate()->LowMemoryNotification(); | ||
} | ||
|
||
TEST_F(V8ElementTest, GetAttributeOperationCallback_Existing) { | ||
V8TestingScope scope; | ||
|
||
Eval("document.body.setAttribute('test-attribute', 'test-value')", scope); | ||
EXPECT_FALSE( | ||
AtomicStringTable::Instance().WeakFind("test-attribute").IsNull()); | ||
EXPECT_FALSE(AtomicStringTable::Instance().WeakFind("test-value").IsNull()); | ||
|
||
AtomicString test_attribute("test-attribute"); | ||
test_attribute.Impl()->ResetRefCountChangeCountForTesting(); | ||
AtomicString test_value("test-value"); | ||
test_value.Impl()->ResetRefCountChangeCountForTesting(); | ||
|
||
Eval("document.body.getAttribute('test-attribute')", scope); | ||
|
||
#if DCHECK_IS_ON() | ||
EXPECT_EQ(test_attribute.Impl()->RefCountChangeCountForTesting(), 9u); | ||
EXPECT_EQ(test_value.Impl()->RefCountChangeCountForTesting(), 6u); | ||
#endif | ||
|
||
// Trigger a low memory notification. This will signal V8 to clear its | ||
// CompilationCache, which is needed because cached compiled code may be | ||
// holding references to externalized AtomicStrings. | ||
scope.GetIsolate()->LowMemoryNotification(); | ||
} | ||
|
||
} // namespace blink |
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