Cache memory mapping within the memory object.
Benchmark results:
Memory size: 1k
Before this CL: 6.959us
After this CL: 0.343us
Memory size: 1M
Before this CL: 1.198ms
After this CL: 0.106ms
Bug: 147777318
Test: NNT_static
Change-Id: Ib006bc044bc2f54c2de08460f3a9a07d6a047b79
Merged-In: Ib006bc044bc2f54c2de08460f3a9a07d6a047b79
Merged-In: Ib006bc044bc2f54c2de08460f3a9a07d6a047b79
(cherry picked from commit 108e2ae59b959e01625267dc718f7bc298ae8a2f)
(cherry picked from commit 659b7e75faa3a838f6d2a93533e1cb9c7d8c99fb)
diff --git a/runtime/Memory.cpp b/runtime/Memory.cpp
index 1f16a28..80a4f99 100644
--- a/runtime/Memory.cpp
+++ b/runtime/Memory.cpp
@@ -197,8 +197,8 @@
}
}
-hal::Request::MemoryPool Memory::getMemoryPool() const {
- hal::Request::MemoryPool pool;
+Request::MemoryPool Memory::getMemoryPool() const {
+ Request::MemoryPool pool;
if (kToken > 0) {
pool.token(kToken);
} else {
@@ -208,8 +208,12 @@
}
std::optional<RunTimePoolInfo> Memory::getRunTimePoolInfo() const {
- // TODO(b/147777318): Cache memory mapping within the memory object.
- return RunTimePoolInfo::createFromHidlMemory(kHidlMemory);
+ std::lock_guard<std::mutex> guard(mMutex);
+ if (!mHasCachedRunTimePoolInfo) {
+ mCachedRunTimePoolInfo = RunTimePoolInfo::createFromHidlMemory(kHidlMemory);
+ mHasCachedRunTimePoolInfo = true;
+ }
+ return mCachedRunTimePoolInfo;
}
intptr_t Memory::getKey() const {
@@ -221,21 +225,20 @@
mUsedBy.emplace(burst.get(), burst);
}
-static int copyHidlMemories(const hidl_memory& src, const hidl_memory& dst) {
- if (src.size() != dst.size()) {
- LOG(ERROR) << "ANeuralNetworksMemory_copy -- incompatible memory size";
- return ANEURALNETWORKS_BAD_DATA;
- }
- auto srcPool = RunTimePoolInfo::createFromHidlMemory(src);
- auto dstPool = RunTimePoolInfo::createFromHidlMemory(dst);
- if (!srcPool.has_value() || !dstPool.has_value()) {
+static int copyHidlMemories(const std::optional<RunTimePoolInfo>& src,
+ const std::optional<RunTimePoolInfo>& dst) {
+ if (!src.has_value() || !dst.has_value()) {
LOG(ERROR) << "ANeuralNetworksMemory_copy -- unable to map memory";
return ANEURALNETWORKS_UNMAPPABLE;
}
- CHECK(srcPool->getBuffer() != nullptr);
- CHECK(dstPool->getBuffer() != nullptr);
- std::copy(srcPool->getBuffer(), srcPool->getBuffer() + src.size(), dstPool->getBuffer());
- dstPool->flush();
+ if (src->getSize() != dst->getSize()) {
+ LOG(ERROR) << "ANeuralNetworksMemory_copy -- incompatible memory size";
+ return ANEURALNETWORKS_BAD_DATA;
+ }
+ CHECK(src->getBuffer() != nullptr);
+ CHECK(dst->getBuffer() != nullptr);
+ std::copy(src->getBuffer(), src->getBuffer() + src->getSize(), dst->getBuffer());
+ dst->flush();
return ANEURALNETWORKS_NO_ERROR;
}
@@ -290,7 +293,7 @@
if (srcHasIBuffer && dstHasIBuffer) {
return copyIBuffers(src.getIBuffer(), dst.getIBuffer(), srcMetadata);
} else if (srcHasHidlMemory && dstHasHidlMemory) {
- return copyHidlMemories(src.getHidlMemory(), dst.getHidlMemory());
+ return copyHidlMemories(src.getRunTimePoolInfo(), dst.getRunTimePoolInfo());
} else if (srcHasHidlMemory && dstHasIBuffer) {
return copyHidlMemoryToIBuffer(src.getHidlMemory(), dst.getIBuffer(),
srcMetadata.dimensions);