From fe5bd9fc551100306540f8c7ebb3c01ea0328b04 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Wed, 31 May 2017 15:00:38 +0900 Subject: [PATCH] switch jl_gc_szclass to a while loop --- src/gc.c | 3 ++- src/julia_internal.h | 39 ++++++++++++++------------------------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/gc.c b/src/gc.c index 9196fd7e67230..4350891243209 100644 --- a/src/gc.c +++ b/src/gc.c @@ -971,7 +971,8 @@ int jl_gc_classify_pools(size_t sz, size_t alignment, int *osize) return -1; size_t allocsz = sz + sizeof(jl_taggedvalue_t); size_t alignsz = jl_gc_alignsz(allocsz, alignment); - int klass = jl_gc_szclass(alignsz); + int klass = jl_gc_szclass(alignsz, alignment); + assert(klass != -1); *osize = jl_gc_sizeclasses[klass]; return (int)(intptr_t)(&((jl_ptls_t)0)->heap.norm_pools[klass]); } diff --git a/src/julia_internal.h b/src/julia_internal.h index e04229b247e90..4c869a51c0185 100644 --- a/src/julia_internal.h +++ b/src/julia_internal.h @@ -190,30 +190,18 @@ STATIC_INLINE int JL_CONST_FUNC jl_gc_alignsz(size_t sz, size_t alignment) } // Use jl_gc_alignsz to obtain the right sz. -STATIC_INLINE int JL_CONST_FUNC jl_gc_szclass(size_t sz) +STATIC_INLINE int JL_CONST_FUNC jl_gc_szclass(size_t sz, size_t alignment) { - // Check that the object fits in the largest pool. - assert(sz <= GC_MAX_SZCLASS + sizeof(jl_taggedvalue_t)); -#ifdef _P64 - if (sz <= 8) - return 0; - const int N = 0; -#elif defined(_CPU_ARM_) || defined(_CPU_PPC_) || defined(_CPU_X86_) - if (sz <= 8) - return (sz + 3) / 4 - 1; - const int N = 1; -#else - if (sz <= 12) - return (sz + 3) / 4 - 1; - const int N = 2; -#endif - if (sz <= 256) - return (sz + 15) / 16 + N; - if (sz <= 496) - return 16 - 16376 / 4 / LLT_ALIGN(sz, 16 * 4) + 16 + N; - if (sz <= 1008) - return 16 - 16376 / 2 / LLT_ALIGN(sz, 16 * 2) + 24 + N; - return 16 - 16376 / 1 / LLT_ALIGN(sz, 16 * 1) + 32 + N; + size_t klass = 0; + while (klass < JL_GC_N_POOLS) { + size_t osize = jl_gc_sizeclasses[klass]; + if (sz <= osize) { + if (alignment == 1 || alignment == 0 || osize % alignment == 0) + return klass; + } + klass++; + } + return -1; } #ifdef __GNUC__ @@ -228,9 +216,10 @@ STATIC_INLINE jl_value_t *jl_gc_alloc_(jl_ptls_t ptls, size_t sz, size_t alignme if (allocsz < sz) // overflow in adding offs, size was "negative" jl_throw(jl_memory_exception); const size_t alignsz = jl_gc_alignsz(allocsz, alignment); + const int klass = jl_gc_szclass(alignsz, alignment); jl_value_t *v; - if (alignsz <= GC_MAX_SZCLASS + sizeof(jl_taggedvalue_t)) { - int pool_id = jl_gc_szclass(alignsz); + if (klass != -1 && alignsz <= GC_MAX_SZCLASS + sizeof(jl_taggedvalue_t)) { + int pool_id = klass; jl_gc_pool_t *p = &ptls->heap.norm_pools[pool_id]; int osize; if (jl_is_constexpr(alignsz)) {