From 36632f55f418bd711112b72d7fe1b6b1745058e5 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 20 Aug 2021 16:57:33 +0900 Subject: boxing_word.h: embed `mrb_float` in `mrb_value` if possible. Embedding reduce memory consumption, sacrificing precision. It clips least significant 2 bits from `mrb_float`, so if you need to keep float precision, define `MRB_USE_FLOAT_FULL_PRECISION`. `MRB_WORD_BOXING` and `MRB_INT64`: `mrb_float` (`double`) is embedded in `mrb_value` clipped last 2 bits. `MRB_WORD_BOXING` and `MRB_INT64` and `MRB_USE_FLOAT_FULL_PRECISION`: `mrb_float` is allocated in the heaps wrapped by `struct RFloat`. `MRB_WORD_BOXING` and `MRB_INT32` and `MRB_USE_FLOAT32`: `mrb_float` (`float`) is embedded in `mrb_value` clipped last 2 bits. In addition, to reserve bit space in the `mrb_value`, maximum inline symbol length become 4 (instead of 5) in the configuration. `MRB_WORD_BOXING` and `MRB_INT32`: Assume `MRB_USE_FLOAT_FULL_PRECISION` and allocate Float values in heap. --- src/etc.c | 17 +++++++++++++++++ src/symbol.c | 4 ++++ 2 files changed, 21 insertions(+) (limited to 'src') diff --git a/src/etc.c b/src/etc.c index a5c48bbe3..4d08ba400 100644 --- a/src/etc.c +++ b/src/etc.c @@ -158,11 +158,28 @@ mrb_word_boxing_float_value(mrb_state *mrb, mrb_float f) { union mrb_value_ v; +#ifndef MRB_USE_FLOAT_FULL_PRECISION + v.f = f; + v.w = (v.w & ~3) | 2; +#else v.p = mrb_obj_alloc(mrb, MRB_TT_FLOAT, mrb->float_class); v.fp->f = f; MRB_SET_FROZEN_FLAG(v.bp); +#endif return v.value; } + + +#ifndef MRB_USE_FLOAT_FULL_PRECISION +MRB_API mrb_float +mrb_word_boxing_value_float(mrb_value v) +{ + union mrb_value_ u; + u.value = v; + u.w = u.w & ~3; + return u.f; +} +#endif #endif /* MRB_NO_FLOAT */ MRB_API mrb_value diff --git a/src/symbol.c b/src/symbol.c index 9d68b5cf3..dbdeca459 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -73,7 +73,11 @@ static const char pack_table[] = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS static mrb_sym sym_inline_pack(const char *name, size_t len) { +#if defined(MRB_WORD_BOXING) && defined(MRB_32BIT) && !defined(MRB_USE_FLOAT_FULL_PRECISION) + const size_t pack_length_max = 4; +#else const size_t pack_length_max = 5; +#endif char c; const char *p; -- cgit v1.2.3