Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hSATAC/5733786 to your computer and use it in GitHub Desktop.
Save hSATAC/5733786 to your computer and use it in GitHub Desktop.
diff --git a/gc.c b/gc.c
index ace7a08..07b7521 100644
--- a/gc.c
+++ b/gc.c
@@ -253,6 +253,7 @@ typedef struct rb_objspace {
size_t total_allocated_object_num;
size_t total_freed_object_num;
int gc_stress;
+ int gc_disable_lazy_sweep;
struct mark_func_data_struct {
void *data;
@@ -284,6 +285,7 @@ int *ruby_initial_gc_stress_ptr = &rb_objspace.gc_stress;
#define deferred_final_list objspace->final.deferred
#define global_List objspace->global_list
#define ruby_gc_stress objspace->gc_stress
+#define ruby_gc_disable_lazy_sweep objspace->gc_disable_lazy_sweep
#define initial_malloc_limit initial_params.initial_malloc_limit
#define initial_heap_min_slots initial_params.initial_heap_min_slots
#define initial_free_min initial_params.initial_free_min
@@ -2039,7 +2041,7 @@ gc_prepare_free_objects(rb_objspace_t *objspace)
{
int res;
- if (objspace->flags.dont_lazy_sweep)
+ if (ruby_gc_disable_lazy_sweep || objspace->flags.dont_lazy_sweep)
return garbage_collect(objspace);
@@ -3287,10 +3289,56 @@ rb_gc_disable(void)
return old ? Qtrue : Qfalse;
}
+/*
+ * call-seq:
+ * GC.enable_lazy_sweep -> true or false
+ *
+ * Enables lazy sweep algorithm, returning <code>true</code> if lazy
+ * sweep was previously disabled.
+ *
+ * GC.disable_lazy_sweep #=> false
+ * GC.enable_lazy_sweep #=> true
+ * GC.enable_lazy_sweep #=> false
+ *
+ */
+
+VALUE
+rb_gc_enable_lazy_sweep(void)
+{
+ rb_objspace_t *objspace = &rb_objspace;
+ int old = ruby_gc_disable_lazy_sweep;
+
+ ruby_gc_disable_lazy_sweep = FALSE;
+ return old ? Qtrue : Qfalse;
+}
+
+/*
+ * call-seq:
+ * GC.disable_lazy_sweep -> true or false
+ *
+ * Disables lazy sweep algorithm, returning <code>true</code> if lazy
+ * sweep was already disabled.
+ *
+ * GC.disable_lazy_sweep #=> false
+ * GC.disable_lazy_sweep #=> true
+ *
+ */
+
+VALUE
+rb_gc_disable_lazy_sweep(void)
+{
+ rb_objspace_t *objspace = &rb_objspace;
+ int old = ruby_gc_disable_lazy_sweep;
+
+ ruby_gc_disable_lazy_sweep = TRUE;
+ return old ? Qtrue : Qfalse;
+}
+
void
rb_gc_set_params(void)
{
char *malloc_limit_ptr, *heap_min_slots_ptr, *free_min_ptr;
+ char *disable_lazy_sweep_ptr;
if (rb_safe_level() > 0) return;
@@ -3326,6 +3374,15 @@ rb_gc_set_params(void)
initial_free_min = free_min_i;
}
}
+
+ disable_lazy_sweep_ptr = getenv("RUBY_GC_DISABLE_LAZY_SWEEP");
+ if (disable_lazy_sweep_ptr != NULL) {
+ int disable_lazy_sweep_i = atoi(disable_lazy_sweep_ptr);
+ if (RTEST(ruby_verbose))
+ fprintf(stderr, "disable_lazy_sweep=%d (%d)\n",
+ disable_lazy_sweep_i, rb_objspace.gc_disable_lazy_sweep);
+ rb_objspace.gc_disable_lazy_sweep = disable_lazy_sweep_i;
+ }
}
void
@@ -4487,6 +4544,8 @@ Init_GC(void)
rb_define_singleton_method(rb_mGC, "start", rb_gc_start, 0);
rb_define_singleton_method(rb_mGC, "enable", rb_gc_enable, 0);
rb_define_singleton_method(rb_mGC, "disable", rb_gc_disable, 0);
+ rb_define_singleton_method(rb_mGC, "enable_lazy_sweep", rb_gc_enable_lazy_sweep, 0);
+ rb_define_singleton_method(rb_mGC, "disable_lazy_sweep", rb_gc_disable_lazy_sweep, 0);
rb_define_singleton_method(rb_mGC, "stress", gc_stress_get, 0);
rb_define_singleton_method(rb_mGC, "stress=", gc_stress_set, 1);
rb_define_singleton_method(rb_mGC, "count", gc_count, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment