Only trim JIT metadata if there is no following compilation task.

This reduces the number of trims by 10x on app startup and a compose
scrolling scenario.

Test: test.py
Bug: 313040662
Change-Id: If4c4e451da115848d0cd3a61f184f35f180b7a03
diff --git a/compiler/jit/jit_compiler.cc b/compiler/jit/jit_compiler.cc
index 1d9cd57..523a666 100644
--- a/compiler/jit/jit_compiler.cc
+++ b/compiler/jit/jit_compiler.cc
@@ -191,6 +191,7 @@
 
   // Do the compilation.
   bool success = false;
+  Jit* jit = runtime->GetJit();
   {
     TimingLogger::ScopedTiming t2(compilation_kind == CompilationKind::kOsr
                                       ? "Compiling OSR"
@@ -198,7 +199,7 @@
                                           ? "Compiling optimized"
                                           : "Compiling baseline",
                                   &logger);
-    JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
+    JitCodeCache* const code_cache = jit->GetCodeCache();
     metrics::AutoTimer timer{runtime->GetMetrics()->JitMethodCompileTotalTime()};
     success = compiler_->JitCompile(
         self, code_cache, region, method, compilation_kind, jit_logger_.get());
@@ -210,14 +211,14 @@
     runtime->GetMetrics()->JitMethodCompileCountDelta()->AddOne();
   }
 
-  // Trim maps to reduce memory usage.
-  // TODO: move this to an idle phase.
-  {
+  // If we don't have a new task following this compile,
+  // trim maps to reduce memory usage.
+  if (jit->GetThreadPool() == nullptr || jit->GetThreadPool()->GetTaskCount(self) == 0) {
     TimingLogger::ScopedTiming t2("TrimMaps", &logger);
     runtime->GetJitArenaPool()->TrimMaps();
   }
 
-  runtime->GetJit()->AddTimingLogger(logger);
+  jit->AddTimingLogger(logger);
   return success;
 }