https://git.reactos.org/?p=reactos.git;a=commitdiff;h=52f103c85bffc21f48ea4…
commit 52f103c85bffc21f48ea4e8e121cbd3163a6f0f8
Author:     Thomas Faber <thomas.faber(a)reactos.org>
AuthorDate: Sun Feb 2 12:54:19 2020 +0100
Commit:     Thomas Faber <thomas.faber(a)reactos.org>
CommitDate: Sun Jun 14 09:10:31 2020 +0200
    [RTL] Use LastEntryInSegment to speed up RtlpFindAndCommitPages. CORE-14588
---
 sdk/lib/rtl/heap.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
diff --git a/sdk/lib/rtl/heap.c b/sdk/lib/rtl/heap.c
index 962d5b079b6..a75c18f5d2a 100644
--- a/sdk/lib/rtl/heap.c
+++ b/sdk/lib/rtl/heap.c
@@ -665,16 +665,21 @@ RtlpFindAndCommitPages(PHEAP Heap,
             /* Calculate first and last entries */
             FirstEntry = (PHEAP_ENTRY)Address;
-            /* Go through the entries to find the last one */
-            if (PreviousUcr)
-                LastEntry = (PHEAP_ENTRY)((ULONG_PTR)PreviousUcr->Address +
PreviousUcr->Size);
-            else
-                LastEntry = &Segment->Entry;
-
-            while (!(LastEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
+            LastEntry = Segment->LastEntryInSegment;
+            if (!(LastEntry->Flags & HEAP_ENTRY_LAST_ENTRY) ||
+                LastEntry + LastEntry->Size != FirstEntry)
             {
-                ASSERT(LastEntry->Size != 0);
-                LastEntry += LastEntry->Size;
+                /* Go through the entries to find the last one */
+                if (PreviousUcr)
+                    LastEntry = (PHEAP_ENTRY)((ULONG_PTR)PreviousUcr->Address +
PreviousUcr->Size);
+                else
+                    LastEntry = &Segment->Entry;
+
+                while (!(LastEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
+                {
+                    ASSERT(LastEntry->Size != 0);
+                    LastEntry += LastEntry->Size;
+                }
             }
             ASSERT((LastEntry + LastEntry->Size) == FirstEntry);