Skip to content

Commit

Permalink
[0.64] Add JSI.MemoryMappedScriptStore runtime option to control usag…
Browse files Browse the repository at this point in the history
…e of MemoryMappedBuffer (#7957)

* Add JSI.MemoryMappedScriptStore runtime option to control usage of MemoryMappedBuffer (#7943)

* Condition using memory mapped buffer to runtime option JSI.MemoryMappedScriptStore

* Change files

* Remove change file

* Change files
  • Loading branch information
JunielKatarn committed Jun 4, 2021
1 parent 3af49ef commit 9c4bfd7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Add JSI.MemoryMappedScriptStore runtime option to control usage of MemoryMappedBuffer (#7943)",
"packageName": "react-native-windows",
"email": "julio.rocha@microsoft.com",
"dependentChangeType": "patch"
}
13 changes: 10 additions & 3 deletions vnext/Desktop.UnitTests/ScriptStoreTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <BaseScriptStoreImpl.h>
#include <CppUnitTest.h>
#include <RuntimeOptions.h>

// Windows API
#include <Windows.h>
Expand All @@ -23,6 +24,10 @@ using winrt::Windows::System::Diagnostics::ProcessDiagnosticInfo;
namespace Microsoft::JSI::Test {

TEST_CLASS (ScriptStoreIntegrationTest) {
TEST_CLASS_INITIALIZE(Init) {
React::SetRuntimeOptionBool("JSI.MemoryMappedScriptStore", true);
}

// Do not run this test in parallel with others.
// It uses process telemetry and should run on isolation.
TEST_METHOD(RetrievePreparedScriptMemoryUsage) {
Expand Down Expand Up @@ -56,9 +61,11 @@ TEST_CLASS (ScriptStoreIntegrationTest) {
auto endWorkingSet =
ProcessDiagnosticInfo::GetForCurrentProcess().MemoryUsage().GetReport().WorkingSetSizeInBytes();

// Without memory mapping: about 4.25 MB (fileSize + overhead)
// Expected working set delta: 0x047000 bytes, about 0.28 MB (6.9% of the 4MB file size).
Assert::IsTrue(endWorkingSet - startWorkingSet < 0.3 * 1024 * 1024);
// Based on recurring local testing:
// Without memory mapping: about 6.11 MB (fileSize + app overhead)
// With memory mapping: about 2.14 MB (view overhead + app overhead)
// Expected working set size should be lower than the actual file size, provided it is larger than the app overhead
Assert::IsTrue(endWorkingSet - startWorkingSet < fileSize);
}
};
} // namespace Microsoft::JSI::Test
23 changes: 22 additions & 1 deletion vnext/Shared/BaseScriptStoreImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "BaseScriptStoreImpl.h"
#include "MemoryMappedBuffer.h"

#include <RuntimeOptions.h>

// C++/WinRT
#include <winrt/base.h>

Expand Down Expand Up @@ -144,7 +146,26 @@ std::unique_ptr<const jsi::Buffer> LocalFileSimpleBufferStore::getBuffer(const s
std::terminate();
}

return Microsoft::JSI::MakeMemoryMappedBuffer(winrt::to_hstring(storeDirectory_ + bufferId).c_str());
if (Microsoft::React::GetRuntimeOptionBool("JSI.MemoryMappedScriptStore")) {
return Microsoft::JSI::MakeMemoryMappedBuffer(winrt::to_hstring(storeDirectory_ + bufferId).c_str());
} else {
// Treat buffer id as the relative path fragment.
std::ifstream file(storeDirectory_ + bufferId, std::ios::binary | std::ios::ate);

if (!file) {
return nullptr;
}

std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);

auto buffer = std::make_unique<ByteArrayBuffer>(static_cast<size_t>(size));
if (!file.read(reinterpret_cast<char *>(buffer->data()), size)) {
return nullptr;
}

return buffer;
}
}

bool LocalFileSimpleBufferStore::persistBuffer(
Expand Down

0 comments on commit 9c4bfd7

Please sign in to comment.