From 474d172b5fa26992a90e63474787166eaca247de Mon Sep 17 00:00:00 2001 From: Konstantin Baladurin Date: Thu, 26 Apr 2018 17:34:58 +0300 Subject: Add memory optimization patches from upstream Change-Id: Ie8ea75fa60184b77135289c8fdc0f49d40b49d87 (cherry picked from commit 371df401e4d8c9639035d164710d1246e0cb8548) --- ...mage-CreateLayoutMapped-and-PEImage-Creat.patch | 190 +++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 packaging/0001-Extract-PEImage-CreateLayoutMapped-and-PEImage-Creat.patch (limited to 'packaging/0001-Extract-PEImage-CreateLayoutMapped-and-PEImage-Creat.patch') diff --git a/packaging/0001-Extract-PEImage-CreateLayoutMapped-and-PEImage-Creat.patch b/packaging/0001-Extract-PEImage-CreateLayoutMapped-and-PEImage-Creat.patch new file mode 100644 index 0000000000..d51706bbcc --- /dev/null +++ b/packaging/0001-Extract-PEImage-CreateLayoutMapped-and-PEImage-Creat.patch @@ -0,0 +1,190 @@ +From 550c59a96baa6714a32dd649795eb4e5294df18b Mon Sep 17 00:00:00 2001 +From: Ruben Ayrapetyan +Date: Fri, 7 Apr 2017 13:25:42 +0300 +Subject: [PATCH 01/32] Extract PEImage::CreateLayoutMapped and + PEImage::CreateLayoutFlat from PEImage::GetLayoutInternal. + +--- + src/vm/peimage.cpp | 138 +++++++++++++++++++++++++++++++++-------------------- + src/vm/peimage.h | 6 +++ + 2 files changed, 92 insertions(+), 52 deletions(-) + +diff --git a/src/vm/peimage.cpp b/src/vm/peimage.cpp +index 3367ef9..1462c94 100644 +--- a/src/vm/peimage.cpp ++++ b/src/vm/peimage.cpp +@@ -911,68 +911,102 @@ PTR_PEImageLayout PEImage::GetLayoutInternal(DWORD imageLayoutMask,DWORD flags) + + if (imageLayoutMask&PEImageLayout::LAYOUT_MAPPED) + { +- PEImageLayout * pLoadLayout = NULL; ++ pRetVal = PEImage::CreateLayoutMapped(); ++ } ++ else ++ { ++ pRetVal = PEImage::CreateLayoutFlat(); ++ } ++ } + +- if (m_bIsTrustedNativeImage || IsFile()) +- { +- // For CoreCLR, try to load all files via LoadLibrary first. If LoadLibrary did not work, retry using +- // regular mapping - but not for native images. +- pLoadLayout = PEImageLayout::Load(this, TRUE /* bNTSafeLoad */, m_bIsTrustedNativeImage /* bThrowOnError */); +- } ++ if (pRetVal != NULL) ++ { ++ pRetVal->AddRef(); ++ } + +- if (pLoadLayout != NULL) +- { +- SetLayout(IMAGE_MAPPED,pLoadLayout); +- pLoadLayout->AddRef(); +- SetLayout(IMAGE_LOADED,pLoadLayout); +- pRetVal=pLoadLayout; +- } +- else +- if (IsFile()) +- { +- PEImageLayoutHolder pLayout(PEImageLayout::Map(GetFileHandle(),this)); +- +- bool fMarkAnyCpuImageAsLoaded = false; +- // Avoid mapping another image if we can. We can only do this for IL-ONLY images +- // since LoadLibrary is needed if we are to actually load code. +- if (pLayout->HasCorHeader() && pLayout->IsILOnly()) +- { +- // For CoreCLR, IL only images will always be mapped. We also dont bother doing the conversion of PE header on 64bit, +- // as done below for the desktop case, as there is no appcompat burden for CoreCLR on 64bit to have that conversion done. +- fMarkAnyCpuImageAsLoaded = true; +- } ++ return pRetVal; ++} + +- pLayout.SuppressRelease(); ++PTR_PEImageLayout PEImage::CreateLayoutMapped() ++{ ++ CONTRACTL ++ { ++ THROWS; ++ GC_TRIGGERS; ++ MODE_ANY; ++ PRECONDITION(m_pLayoutLock->IsWriterLock()); ++ } ++ CONTRACTL_END; + +- SetLayout(IMAGE_MAPPED,pLayout); +- if (fMarkAnyCpuImageAsLoaded) +- { +- pLayout->AddRef(); +- SetLayout(IMAGE_LOADED, pLayout); +- } +- pRetVal=pLayout; +- } +- else +- { +- PEImageLayoutHolder flatPE(GetLayoutInternal(PEImageLayout::LAYOUT_FLAT,LAYOUT_CREATEIFNEEDED)); +- if (!flatPE->CheckFormat()) +- ThrowFormat(COR_E_BADIMAGEFORMAT); +- pRetVal=PEImageLayout::LoadFromFlat(flatPE); +- SetLayout(IMAGE_MAPPED,pRetVal); +- } ++ PTR_PEImageLayout pRetVal; ++ ++ PEImageLayout * pLoadLayout = NULL; ++ ++ if (m_bIsTrustedNativeImage || IsFile()) ++ { ++ // For CoreCLR, try to load all files via LoadLibrary first. If LoadLibrary did not work, retry using ++ // regular mapping - but not for native images. ++ pLoadLayout = PEImageLayout::Load(this, TRUE /* bNTSafeLoad */, m_bIsTrustedNativeImage /* bThrowOnError */); ++ } ++ ++ if (pLoadLayout != NULL) ++ { ++ SetLayout(IMAGE_MAPPED,pLoadLayout); ++ pLoadLayout->AddRef(); ++ SetLayout(IMAGE_LOADED,pLoadLayout); ++ pRetVal=pLoadLayout; ++ } ++ else if (IsFile()) ++ { ++ PEImageLayoutHolder pLayout(PEImageLayout::Map(GetFileHandle(),this)); ++ ++ bool fMarkAnyCpuImageAsLoaded = false; ++ // Avoid mapping another image if we can. We can only do this for IL-ONLY images ++ // since LoadLibrary is needed if we are to actually load code ++ if (pLayout->HasCorHeader() && pLayout->IsILOnly()) ++ { ++ // For CoreCLR, IL only images will always be mapped. We also dont bother doing the conversion of PE header on 64bit, ++ // as done below for the desktop case, as there is no appcompat burden for CoreCLR on 64bit to have that conversion done. ++ fMarkAnyCpuImageAsLoaded = true; + } +- else +- if (imageLayoutMask&PEImageLayout::LAYOUT_FLAT) ++ ++ pLayout.SuppressRelease(); ++ ++ SetLayout(IMAGE_MAPPED,pLayout); ++ if (fMarkAnyCpuImageAsLoaded) + { +- pRetVal=PEImageLayout::LoadFlat(GetFileHandle(),this); +- m_pLayouts[IMAGE_FLAT]=pRetVal; ++ pLayout->AddRef(); ++ SetLayout(IMAGE_LOADED, pLayout); + } +- ++ pRetVal=pLayout; + } +- if (pRetVal) ++ else + { +- pRetVal->AddRef(); ++ PEImageLayoutHolder flatPE(GetLayoutInternal(PEImageLayout::LAYOUT_FLAT,LAYOUT_CREATEIFNEEDED)); ++ if (!flatPE->CheckFormat()) ++ ThrowFormat(COR_E_BADIMAGEFORMAT); ++ pRetVal=PEImageLayout::LoadFromFlat(flatPE); ++ SetLayout(IMAGE_MAPPED,pRetVal); + } ++ ++ return pRetVal; ++} ++ ++PTR_PEImageLayout PEImage::CreateLayoutFlat() ++{ ++ CONTRACTL ++ { ++ GC_TRIGGERS; ++ MODE_ANY; ++ PRECONDITION(m_pLayoutLock->IsWriterLock()); ++ } ++ CONTRACTL_END; ++ ++ PTR_PEImageLayout pRetVal; ++ ++ pRetVal = PEImageLayout::LoadFlat(GetFileHandle(),this); ++ m_pLayouts[IMAGE_FLAT] = pRetVal; ++ + return pRetVal; + } + +diff --git a/src/vm/peimage.h b/src/vm/peimage.h +index 3245621..f61e185 100644 +--- a/src/vm/peimage.h ++++ b/src/vm/peimage.h +@@ -257,6 +257,12 @@ private: + #ifndef DACCESS_COMPILE + // Get or create the layout corresponding to the mask, with an AddRef + PTR_PEImageLayout GetLayoutInternal(DWORD imageLayoutMask, DWORD flags); ++ ++ // Create the mapped layout ++ PTR_PEImageLayout CreateLayoutMapped(); ++ ++ // Create the flat layout ++ PTR_PEImageLayout CreateLayoutFlat(); + #endif + // Get an existing layout corresponding to the mask, no AddRef + PTR_PEImageLayout GetExistingLayoutInternal(DWORD imageLayoutMask); +-- +2.7.4 + -- cgit v1.2.3