-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Impeller] Started using a pool for HostBuffers. (#44081)
This removes ~792 MB/s of allocation when scrolling around the Gallery ((358 MB - 226 MB) * 6). ## before <img width="1337" alt="Screenshot 2023-07-27 at 1 48 57 PM" src="https://github.com/flutter/engine/assets/30870216/d320c2da-c333-40b7-9326-c9b69e5ff462"> ## after <img width="1391" alt="Screenshot 2023-07-27 at 1 36 35 PM" src="https://github.com/flutter/engine/assets/30870216/e3314a03-8691-4766-9989-aef112703384"> ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See [testing the engine] for instructions on writing and running engine tests. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I signed the [CLA]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --------- Co-authored-by: Jonah Williams <jonahwilliams@google.com>
- Loading branch information
1 parent
1aadc75
commit 10a1f9c
Showing
10 changed files
with
162 additions
and
3 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <mutex> | ||
|
||
namespace impeller { | ||
|
||
/// @brief A thread-safe pool with a limited byte size. | ||
/// @tparam T The type that the pool will contain. | ||
template <typename T> | ||
class Pool { | ||
public: | ||
Pool(uint32_t limit_bytes) : limit_bytes_(limit_bytes), size_(0) {} | ||
|
||
std::shared_ptr<T> Grab() { | ||
std::scoped_lock lock(mutex_); | ||
if (pool_.empty()) { | ||
return T::Create(); | ||
} | ||
std::shared_ptr<T> result = std::move(pool_.back()); | ||
pool_.pop_back(); | ||
size_ -= result->GetSize(); | ||
return result; | ||
} | ||
|
||
void Recycle(std::shared_ptr<T> object) { | ||
std::scoped_lock lock(mutex_); | ||
size_t object_size = object->GetSize(); | ||
if (size_ + object_size <= limit_bytes_ && | ||
object_size < (limit_bytes_ / 2)) { | ||
object->Reset(); | ||
size_ += object_size; | ||
pool_.emplace_back(std::move(object)); | ||
} | ||
} | ||
|
||
uint32_t GetSize() const { | ||
std::scoped_lock lock(mutex_); | ||
return size_; | ||
} | ||
|
||
private: | ||
std::vector<std::shared_ptr<T>> pool_; | ||
const uint32_t limit_bytes_; | ||
uint32_t size_; | ||
// Note: This would perform better as a lockless ring buffer. | ||
mutable std::mutex mutex_; | ||
}; | ||
|
||
} // namespace impeller |
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,63 @@ | ||
// Copyright 2013 The Flutter 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 "gtest/gtest.h" | ||
|
||
#include "impeller/renderer/pool.h" | ||
|
||
namespace impeller { | ||
namespace testing { | ||
|
||
namespace { | ||
class Foobar { | ||
public: | ||
static std::shared_ptr<Foobar> Create() { return std::make_shared<Foobar>(); } | ||
|
||
size_t GetSize() const { return size_; } | ||
|
||
void SetSize(size_t size) { size_ = size; } | ||
|
||
void Reset() { is_reset_ = true; } | ||
|
||
bool GetIsReset() const { return is_reset_; } | ||
|
||
void SetIsReset(bool is_reset) { is_reset_ = is_reset; } | ||
|
||
private: | ||
size_t size_; | ||
bool is_reset_ = false; | ||
}; | ||
} // namespace | ||
|
||
TEST(PoolTest, Simple) { | ||
Pool<Foobar> pool(1'000); | ||
{ | ||
auto grabbed = pool.Grab(); | ||
grabbed->SetSize(123); | ||
pool.Recycle(grabbed); | ||
EXPECT_EQ(pool.GetSize(), 123u); | ||
} | ||
auto grabbed = pool.Grab(); | ||
EXPECT_EQ(grabbed->GetSize(), 123u); | ||
EXPECT_TRUE(grabbed->GetIsReset()); | ||
EXPECT_EQ(pool.GetSize(), 0u); | ||
} | ||
|
||
TEST(PoolTest, Overload) { | ||
Pool<Foobar> pool(1'000); | ||
{ | ||
std::vector<std::shared_ptr<Foobar>> values; | ||
for (int i = 0; i < 20; i++) { | ||
values.push_back(pool.Grab()); | ||
} | ||
for (auto value : values) { | ||
value->SetSize(100); | ||
pool.Recycle(value); | ||
} | ||
} | ||
EXPECT_EQ(pool.GetSize(), 1'000u); | ||
} | ||
|
||
} // namespace testing | ||
} // namespace impeller |
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