Author: Remi Meier Branch: c7-weakref Changeset: r994:f52b09faef51 Date: 2014-03-13 10:11 +0100 http://bitbucket.org/pypy/stmgc/changeset/f52b09faef51/
Log: simplify code by assuming sizeof(weakref) == 16 diff --git a/c7/stm/weakref.c b/c7/stm/weakref.c --- a/c7/stm/weakref.c +++ b/c7/stm/weakref.c @@ -2,21 +2,24 @@ # error "must be compiled via stmgc.c" #endif +#define WEAKREF_PTR(wr, sz) ((object_t * TLPREFIX *)(((stm_char *)(wr)) + (sz) - sizeof(void*))) object_t *stm_allocate_weakref(ssize_t size_rounded_up) { OPT_ASSERT(size_rounded_up > sizeof(struct object_s)); + OPT_ASSERT(size_rounded_up == 16); /* no reason for it to be anything else */ + object_t *obj = stm_allocate(size_rounded_up); + assert(_is_in_nursery(obj)); /* because it's so small */ LIST_APPEND(STM_PSEGMENT->young_weakrefs, obj); return obj; } -void _set_weakref_in_all_segments(char* base, object_t *weakref, object_t *value) +void _set_weakref_in_all_segments(object_t *weakref, object_t *value) { - char *realobj = REAL_ADDRESS(base, weakref); - ssize_t size = stmcb_size_rounded_up((struct object_s *)realobj); + ssize_t size = 16; stm_char *point_to_loc = (stm_char*)WEAKREF_PTR(weakref, size); if (flag_page_private[(uintptr_t)point_to_loc / 4096UL] == PRIVATE_PAGE) { @@ -45,32 +48,23 @@ STM_PSEGMENT->young_weakrefs, object_t * /*item*/, ({ - if (_is_in_nursery(item)) { - object_t *TLPREFIX *pforwarded_array = (object_t *TLPREFIX *)item; + /* weakrefs are so small, they always are in the nursery. Never + a young outside nursery object. */ + assert(_is_in_nursery(item)); + object_t *TLPREFIX *pforwarded_array = (object_t *TLPREFIX *)item; - /* the following checks are done like in nursery.c: */ - if (!(item->stm_flags & GCFLAG_HAS_SHADOW) - || (pforwarded_array[0] != GCWORD_MOVED)) { - /* weakref dies */ - continue; - } + /* the following checks are done like in nursery.c: */ + if (!(item->stm_flags & GCFLAG_HAS_SHADOW) + || (pforwarded_array[0] != GCWORD_MOVED)) { + /* weakref dies */ + continue; + } - item = pforwarded_array[1]; /* moved location */ - } - else { - /* young outside nursery object */ - if (tree_contains(STM_PSEGMENT->young_outside_nursery, - (uintptr_t)item)) { - /* still in the tree -> wasn't seen by the minor collection, - so it doesn't survive */ - continue; - } - } + item = pforwarded_array[1]; /* moved location */ + assert(!_is_young(item)); - char *base = STM_SEGMENT->segment_base; - char *realobj = REAL_ADDRESS(base, item); - ssize_t size = stmcb_size_rounded_up((struct object_s *)realobj); + ssize_t size = 16; object_t *pointing_to = *WEAKREF_PTR(item, size); assert(pointing_to != NULL); @@ -80,12 +74,12 @@ if (!(pointing_to->stm_flags & GCFLAG_HAS_SHADOW) || (pforwarded_array[0] != GCWORD_MOVED)) { /* pointing_to dies */ - _set_weakref_in_all_segments(base, item, NULL); + _set_weakref_in_all_segments(item, NULL); continue; /* no need to remember in old_weakrefs */ } else { /* moved location */ - _set_weakref_in_all_segments(base, item, pforwarded_array[1]); + _set_weakref_in_all_segments(item, pforwarded_array[1]); } } else { @@ -94,7 +88,7 @@ (uintptr_t)pointing_to)) { /* still in the tree -> wasn't seen by the minor collection, so it doesn't survive */ - _set_weakref_in_all_segments(base, item, NULL); + _set_weakref_in_all_segments(item, NULL); continue; /* no need to remember in old_weakrefs */ } /* pointing_to was already old */ @@ -125,13 +119,12 @@ continue; } - char *realobj = REAL_ADDRESS(pseg->pub.segment_base, weakref); - ssize_t size = stmcb_size_rounded_up((struct object_s *)realobj); + ssize_t size = 16; object_t *pointing_to = *WEAKREF_PTR(weakref, size); assert(pointing_to != NULL); if (!mark_visited_test(pointing_to)) { //assert(flag_page_private[(uintptr_t)weakref / 4096UL] != PRIVATE_PAGE); - _set_weakref_in_all_segments(pseg->pub.segment_base, weakref, NULL); + _set_weakref_in_all_segments(weakref, NULL); /* we don't need it in this list anymore */ list_set_item(lst, n, list_pop_item(lst)); diff --git a/c7/stm/weakref.h b/c7/stm/weakref.h --- a/c7/stm/weakref.h +++ b/c7/stm/weakref.h @@ -2,8 +2,6 @@ #define _SRCSTM_WEAKREF_H -#define WEAKREF_PTR(wr, sz) ((object_t * TLPREFIX *)(((stm_char *)(wr)) + (sz) - sizeof(void*))) - void stm_move_young_weakrefs(void); void stm_visit_old_weakrefs(void); diff --git a/c7/stmgc.h b/c7/stmgc.h --- a/c7/stmgc.h +++ b/c7/stmgc.h @@ -200,7 +200,9 @@ You must assign the reference before the next collection may happen. After that, you must not mutate the reference anymore. However, it can become NULL after any GC if the reference dies during that - collection. */ + collection. + NOTE: For performance, we assume stmcb_size_rounded_up(weakref)==16 +*/ object_t *stm_allocate_weakref(ssize_t size_rounded_up); diff --git a/c7/test/support.py b/c7/test/support.py --- a/c7/test/support.py +++ b/c7/test/support.py @@ -287,11 +287,8 @@ return o def stm_allocate_weakref(point_to_obj, size=None): - if size is None: - o = lib.stm_allocate_weakref(HDR + WORD) - else: - assert size >= HDR + WORD - o = lib.stm_allocate_weakref(size) + assert HDR+WORD == 16 + o = lib.stm_allocate_weakref(HDR + WORD) tid = 421420 lib._set_type_id(o, tid) diff --git a/c7/test/test_weakref.py b/c7/test/test_weakref.py --- a/c7/test/test_weakref.py +++ b/c7/test/test_weakref.py @@ -143,37 +143,6 @@ self.start_transaction() assert stm_get_weakref(lp1) == ffi.NULL - def test_multiple_threads_w_big_weakref(self): - self.start_transaction() - lp0 = stm_allocate(1024) - self.push_root(lp0) - self.commit_transaction() - - self.start_transaction() - lp0 = self.pop_root() - self.push_root(lp0) - stm_write(lp0) # privatize page - - self.push_root_no_gc() - lp2 = stm_allocate(48) - lp1 = stm_allocate_weakref( - lp2, size=lib._STM_FAST_ALLOC + 16) # no collection here - self.pop_root() - - self.push_root(lp0) - self.push_root(lp1) - self.commit_transaction() - # lp2 dies - lp1 = self.pop_root() - self.push_root(lp1) - - assert stm_get_weakref(lp1) == ffi.NULL - - self.switch(1) - - self.start_transaction() - assert stm_get_weakref(lp1) == ffi.NULL - _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit