Experimental (somewhat): Force GCs when disposing contexts, but
authorkasperl@chromium.org <kasperl@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Thu, 26 Feb 2009 12:40:50 +0000 (12:40 +0000)
committerkasperl@chromium.org <kasperl@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Thu, 26 Feb 2009 12:40:50 +0000 (12:40 +0000)
make sure not to do it repeatedly for bursts of context
disposals.
Review URL: http://codereview.chromium.org/27201

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1375 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/api.cc
src/heap.cc
src/heap.h
src/v8-counters.h

index 46b8051..c8ee133 100644 (file)
@@ -376,7 +376,9 @@ bool V8::IsGlobalWeak(void** obj) {
 void V8::DisposeGlobal(void** obj) {
   LOG_API("DisposeGlobal");
   if (has_shut_down) return;
-  i::GlobalHandles::Destroy(reinterpret_cast<i::Object**>(obj));
+  i::Object** ptr = reinterpret_cast<i::Object**>(obj);
+  if ((*ptr)->IsGlobalContext()) i::Heap::NotifyContextDisposed();
+  i::GlobalHandles::Destroy(ptr);
 }
 
 // --- H a n d l e s ---
@@ -2207,6 +2209,9 @@ Persistent<Context> v8::Context::New(
   LOG_API("Context::New");
   ON_BAILOUT("v8::Context::New()", return Persistent<Context>());
 
+  // Give the heap a chance to cleanup if we've disposed contexts.
+  i::Heap::CollectAllGarbageIfContextDisposed();
+
   v8::Handle<ObjectTemplate> proxy_template = global_template;
   i::Handle<i::FunctionTemplateInfo> proxy_constructor;
   i::Handle<i::FunctionTemplateInfo> global_constructor;
index d3651b9..ce722f0 100644 (file)
@@ -97,6 +97,7 @@ int Heap::mc_count_ = 0;
 int Heap::gc_count_ = 0;
 
 int Heap::always_allocate_scope_depth_ = 0;
+bool Heap::context_disposed_pending_ = false;
 
 #ifdef DEBUG
 bool Heap::allocation_allowed_ = true;
@@ -293,6 +294,20 @@ void Heap::CollectAllGarbage() {
 }
 
 
+void Heap::CollectAllGarbageIfContextDisposed() {
+  if (context_disposed_pending_) {
+    StatsRateScope scope(&Counters::gc_context);
+    CollectAllGarbage();
+    context_disposed_pending_ = false;
+  }
+}
+
+
+void Heap::NotifyContextDisposed() {
+  context_disposed_pending_ = true;
+}
+
+
 bool Heap::CollectGarbage(int requested_size, AllocationSpace space) {
   // The VM is in the GC state until exiting this function.
   VMState state(GC);
@@ -436,6 +451,7 @@ void Heap::MarkCompact(GCTracer* tracer) {
   Shrink();
 
   Counters::objs_since_last_full.Set(0);
+  context_disposed_pending_ = false;
 }
 
 
index b3cd4d2..712efb9 100644 (file)
@@ -606,6 +606,13 @@ class Heap : public AllStatic {
   // Performs a full garbage collection.
   static void CollectAllGarbage();
 
+  // Performs a full garbage collection if a context has been disposed
+  // since the last time the check was performed.
+  static void CollectAllGarbageIfContextDisposed();
+
+  // Notify the heap that a context has been disposed.
+  static void NotifyContextDisposed();
+
   // Utility to invoke the scavenger. This is needed in test code to
   // ensure correct callback for weak global handles.
   static void PerformScavenge();
@@ -808,6 +815,7 @@ class Heap : public AllStatic {
   static int scavenge_count_;
 
   static int always_allocate_scope_depth_;
+  static bool context_disposed_pending_;
 
   static const int kMaxMapSpaceSize = 8*MB;
 
index 93e8970..acd3b23 100644 (file)
 
 namespace v8 { namespace internal {
 
-#define STATS_RATE_LIST(SR)                                \
-  SR(gc_compactor, V8.GCCompactor) /* GC Compactor time */ \
-  SR(gc_scavenger, V8.GCScavenger) /* GC Scavenger time */ \
-  SR(compile, V8.Compile)          /* Compile time*/       \
-  SR(compile_eval, V8.CompileEval) /* Eval compile time */ \
-  SR(compile_lazy, V8.CompileLazy) /* Lazy compile time */ \
-  SR(parse, V8.Parse)              /* Parse time */        \
-  SR(parse_lazy, V8.ParseLazy)     /* Lazy parse time */   \
+#define STATS_RATE_LIST(SR)                                             \
+  SR(gc_compactor, V8.GCCompactor) /* GC Compactor time */              \
+  SR(gc_scavenger, V8.GCScavenger) /* GC Scavenger time */              \
+  SR(gc_context, V8.GCContext)     /* GC context cleanup time */        \
+  SR(compile, V8.Compile)          /* Compile time*/                    \
+  SR(compile_eval, V8.CompileEval) /* Eval compile time */              \
+  SR(compile_lazy, V8.CompileLazy) /* Lazy compile time */              \
+  SR(parse, V8.Parse)              /* Parse time */                     \
+  SR(parse_lazy, V8.ParseLazy)     /* Lazy parse time */                \
   SR(pre_parse, V8.PreParse)       /* Pre-parse time */
 
 // WARNING: STATS_COUNTER_LIST_* is a very large macro that is causing MSVC