* struct.c: members memory can use theap.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65452 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ko1 2018-10-30 22:03:42 +00:00
Родитель 873d57347f
Коммит 198ff42258
6 изменённых файлов: 84 добавлений и 26 удалений

Просмотреть файл

@ -182,8 +182,9 @@ RB_DEBUG_COUNTER(obj_hash_under4)
RB_DEBUG_COUNTER(obj_hash_ge4) RB_DEBUG_COUNTER(obj_hash_ge4)
RB_DEBUG_COUNTER(obj_hash_ge8) RB_DEBUG_COUNTER(obj_hash_ge8)
RB_DEBUG_COUNTER(obj_struct_ptr)
RB_DEBUG_COUNTER(obj_struct_embed) RB_DEBUG_COUNTER(obj_struct_embed)
RB_DEBUG_COUNTER(obj_struct_transient)
RB_DEBUG_COUNTER(obj_struct_ptr)
RB_DEBUG_COUNTER(obj_regexp_ptr) RB_DEBUG_COUNTER(obj_regexp_ptr)

31
gc.c
Просмотреть файл

@ -2383,14 +2383,17 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
break; break;
case T_STRUCT: case T_STRUCT:
if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 && if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) ||
RANY(obj)->as.rstruct.as.heap.ptr) { RANY(obj)->as.rstruct.as.heap.ptr == NULL) {
xfree((void *)RANY(obj)->as.rstruct.as.heap.ptr);
RB_DEBUG_COUNTER_INC(obj_struct_ptr);
}
else {
RB_DEBUG_COUNTER_INC(obj_struct_embed); RB_DEBUG_COUNTER_INC(obj_struct_embed);
} }
else if (RSTRUCT_TRANSIENT_P(obj)) {
RB_DEBUG_COUNTER_INC(obj_struct_transient);
}
else {
xfree((void *)RANY(obj)->as.rstruct.as.heap.ptr);
RB_DEBUG_COUNTER_INC(obj_struct_ptr);
}
break; break;
case T_SYMBOL: case T_SYMBOL:
@ -4775,12 +4778,18 @@ gc_mark_children(rb_objspace_t *objspace, VALUE obj)
case T_STRUCT: case T_STRUCT:
{ {
long len = RSTRUCT_LEN(obj); long i;
const VALUE *ptr = RSTRUCT_CONST_PTR(obj); const long len = RSTRUCT_LEN(obj);
const VALUE * const ptr = RSTRUCT_CONST_PTR(obj);
while (len--) { for (i=0; i<len; i++) {
gc_mark(objspace, *ptr++); gc_mark(objspace, ptr[i]);
} }
if (objspace->mark_func_data == NULL &&
RSTRUCT_TRANSIENT_P(obj)) {
rb_transient_heap_mark(obj, ptr);
}
} }
break; break;

Просмотреть файл

@ -698,14 +698,18 @@ extern void ruby_init_setproctitle(int argc, char *argv[]);
#define RSTRUCT_EMBED_LEN_MAX RSTRUCT_EMBED_LEN_MAX #define RSTRUCT_EMBED_LEN_MAX RSTRUCT_EMBED_LEN_MAX
#define RSTRUCT_EMBED_LEN_MASK RSTRUCT_EMBED_LEN_MASK #define RSTRUCT_EMBED_LEN_MASK RSTRUCT_EMBED_LEN_MASK
#define RSTRUCT_EMBED_LEN_SHIFT RSTRUCT_EMBED_LEN_SHIFT #define RSTRUCT_EMBED_LEN_SHIFT RSTRUCT_EMBED_LEN_SHIFT
enum { enum {
RSTRUCT_EMBED_LEN_MAX = 3, RSTRUCT_EMBED_LEN_MAX = 3,
RSTRUCT_EMBED_LEN_MASK = (RUBY_FL_USER2|RUBY_FL_USER1), RSTRUCT_EMBED_LEN_MASK = (RUBY_FL_USER2|RUBY_FL_USER1),
RSTRUCT_EMBED_LEN_SHIFT = (RUBY_FL_USHIFT+1), RSTRUCT_EMBED_LEN_SHIFT = (RUBY_FL_USHIFT+1),
RSTRUCT_TRANSIENT_FLAG = FL_USER3,
RSTRUCT_ENUM_END RSTRUCT_ENUM_END
}; };
#define RSTRUCT_TRANSIENT_P(st) FL_TEST_RAW((obj), RSTRUCT_TRANSIENT_FLAG)
struct RStruct { struct RStruct {
struct RBasic basic; struct RBasic basic;
union { union {
@ -746,6 +750,13 @@ rb_struct_const_ptr(VALUE st)
RSTRUCT(st)->as.ary : RSTRUCT(st)->as.heap.ptr); RSTRUCT(st)->as.ary : RSTRUCT(st)->as.heap.ptr);
} }
static inline const VALUE *
rb_struct_const_heap_ptr(VALUE st)
{
/* TODO: check embed on debug mode */
return RSTRUCT(st)->as.heap.ptr;
}
/* class.c */ /* class.c */
struct rb_deprecated_classext_struct { struct rb_deprecated_classext_struct {

Просмотреть файл

@ -12,6 +12,7 @@
#include "internal.h" #include "internal.h"
#include "vm_core.h" #include "vm_core.h"
#include "id.h" #include "id.h"
#include "transient_heap.h"
/* only for struct[:field] access */ /* only for struct[:field] access */
enum { enum {
@ -654,6 +655,41 @@ rb_struct_initialize(VALUE self, VALUE values)
return rb_struct_initialize_m(RARRAY_LENINT(values), RARRAY_CONST_PTR(values), self); return rb_struct_initialize_m(RARRAY_LENINT(values), RARRAY_CONST_PTR(values), self);
} }
static VALUE *
struct_heap_alloc(VALUE st, size_t len)
{
VALUE *ptr = rb_transient_heap_alloc((VALUE)st, sizeof(VALUE) * len);
if (ptr) {
FL_SET_RAW(st, RSTRUCT_TRANSIENT_FLAG);
return ptr;
}
else {
FL_UNSET_RAW(st, RSTRUCT_TRANSIENT_FLAG);
return ALLOC_N(VALUE, len);
}
}
void
rb_struct_transient_heap_evacuate(VALUE obj, int promote)
{
if (RSTRUCT_TRANSIENT_P(obj)) {
const VALUE *old_ptr = rb_struct_const_heap_ptr(obj);
VALUE *new_ptr;
long len = RSTRUCT_LEN(obj);
if (promote) {
new_ptr = ALLOC_N(VALUE, len);
FL_UNSET_RAW(obj, RSTRUCT_TRANSIENT_FLAG);
}
else {
new_ptr = struct_heap_alloc(obj, len);
}
MEMCPY(new_ptr, old_ptr, VALUE, len);
RSTRUCT(obj)->as.heap.ptr = new_ptr;
}
}
static VALUE static VALUE
struct_alloc(VALUE klass) struct_alloc(VALUE klass)
{ {
@ -668,9 +704,9 @@ struct_alloc(VALUE klass)
rb_mem_clear((VALUE *)st->as.ary, n); rb_mem_clear((VALUE *)st->as.ary, n);
} }
else { else {
st->as.heap.ptr = ALLOC_N(VALUE, n); st->as.heap.ptr = struct_heap_alloc((VALUE)st, n);
rb_mem_clear((VALUE *)st->as.heap.ptr, n); rb_mem_clear((VALUE *)st->as.heap.ptr, n);
st->as.heap.len = n; st->as.heap.len = n;
} }
return (VALUE)st; return (VALUE)st;

Просмотреть файл

@ -354,7 +354,8 @@ rb_transient_heap_alloc(VALUE obj, size_t req_size)
size_t size = ROUND_UP(req_size + sizeof(struct transient_alloc_header), TRANSIENT_HEAP_ALLOC_ALIGN); size_t size = ROUND_UP(req_size + sizeof(struct transient_alloc_header), TRANSIENT_HEAP_ALLOC_ALIGN);
TH_ASSERT(RB_TYPE_P(obj, T_ARRAY) || TH_ASSERT(RB_TYPE_P(obj, T_ARRAY) ||
RB_TYPE_P(obj, T_OBJECT)); /* supported types */ RB_TYPE_P(obj, T_OBJECT) ||
RB_TYPE_P(obj, T_STRUCT)); /* supported types */
if (size > TRANSIENT_HEAP_ALLOC_MAX) { if (size > TRANSIENT_HEAP_ALLOC_MAX) {
if (TRANSIENT_HEAP_DEBUG >= 3) fprintf(stderr, "rb_transient_heap_alloc: [too big: %ld] %s\n", (long)size, rb_obj_info(obj)); if (TRANSIENT_HEAP_DEBUG >= 3) fprintf(stderr, "rb_transient_heap_alloc: [too big: %ld] %s\n", (long)size, rb_obj_info(obj));
@ -546,7 +547,7 @@ rb_transient_heap_mark(VALUE obj, const void *ptr)
static const void * static const void *
transient_heap_ptr(VALUE obj, int error) transient_heap_ptr(VALUE obj, int error)
{ {
const void *ptr; const void *ptr = NULL;
switch (BUILTIN_TYPE(obj)) { switch (BUILTIN_TYPE(obj)) {
case T_ARRAY: case T_ARRAY:
@ -554,25 +555,21 @@ transient_heap_ptr(VALUE obj, int error)
TH_ASSERT(!FL_TEST_RAW(obj, RARRAY_EMBED_FLAG)); TH_ASSERT(!FL_TEST_RAW(obj, RARRAY_EMBED_FLAG));
ptr = RARRAY(obj)->as.heap.ptr; ptr = RARRAY(obj)->as.heap.ptr;
} }
else {
ptr = NULL;
}
break; break;
case T_OBJECT: case T_OBJECT:
if (ROBJ_TRANSIENT_P(obj)) { if (ROBJ_TRANSIENT_P(obj)) {
ptr = ROBJECT_IVPTR(obj); ptr = ROBJECT_IVPTR(obj);
} }
else { break;
ptr = NULL; case T_STRUCT:
if (RSTRUCT_TRANSIENT_P(obj)) {
ptr = rb_struct_const_heap_ptr(obj);
} }
break; break;
default: default:
if (error) { if (error) {
rb_bug("transient_heap_ptr: unknown obj %s\n", rb_obj_info(obj)); rb_bug("transient_heap_ptr: unknown obj %s\n", rb_obj_info(obj));
} }
else {
ptr = NULL;
}
} }
return ptr; return ptr;
@ -673,6 +670,9 @@ transient_heap_block_evacuate(struct transient_heap* theap, struct transient_hea
case T_OBJECT: case T_OBJECT:
rb_obj_transient_heap_evacuate(obj, !TRANSIENT_HEAP_DEBUG_DONT_PROMOTE); rb_obj_transient_heap_evacuate(obj, !TRANSIENT_HEAP_DEBUG_DONT_PROMOTE);
break; break;
case T_STRUCT:
rb_struct_transient_heap_evacuate(obj, !TRANSIENT_HEAP_DEBUG_DONT_PROMOTE);
break;
default: default:
rb_bug("unsupporeted"); rb_bug("unsupporeted");
} }

Просмотреть файл

@ -34,6 +34,7 @@ int rb_transient_heap_managed_ptr_p(const void *ptr);
/* evacuate functions */ /* evacuate functions */
void rb_ary_transient_heap_evacuate(VALUE ary, int promote); void rb_ary_transient_heap_evacuate(VALUE ary, int promote);
void rb_obj_transient_heap_evacuate(VALUE ary, int promote); void rb_obj_transient_heap_evacuate(VALUE obj, int promote);
void rb_struct_transient_heap_evacuate(VALUE st, int promote);
#endif #endif