Project

General

Profile

Feature #12005

Unify Fixnum and Bignum into Integer

Added by Yui NARUSE about 1 year ago. Updated 7 months ago.

Status:
Closed
Priority:
Normal
Assignee:
Target version:
-
[ruby-core:72918]

Description

CRuby has two Integer classes, Fixnum and Bignum.
But it is implementation detail.
They should be seen as a single class Integer like Flonum.

Compatibility note

  • Q: How do I check whether Fixnum and Bignum are unified or not?
  • A: check RUBY_INTEGER_UNIFICATION macro

unify-fixnum-and-bignum.patch View (30.5 KB) Akira Tanaka, 05/03/2016 10:18 AM


Related issues

Related to Ruby trunk - Bug #12427: Defining methods with the same name to both Fixnum and Bignum classes could cause SEGV in C extensions since Feature #12005 Closed
Related to Ruby trunk - Feature #12739: deprecate_constant :Fixnum, :Bignum Closed

Associated revisions

Revision 55024
Added by Akira Tanaka 8 months ago

[Feature #12005] Unify Fixnum and Bignum into Integer

  • [Feature #12005] Unify Fixnum and Bignum into Integer

  • include/ruby/ruby.h (rb_class_of): Return rb_cInteger for fixnums.

  • insns.def (INTEGER_REDEFINED_OP_FLAG): Unified from
    FIXNUM_REDEFINED_OP_FLAG and BIGNUM_REDEFINED_OP_FLAG.

  • vm_core.h: Ditto.

  • vm_insnhelper.c (opt_eq_func): Use INTEGER_REDEFINED_OP_FLAG instead
    of FIXNUM_REDEFINED_OP_FLAG.

  • vm.c (vm_redefinition_check_flag): Use rb_cInteger instead of
    rb_cFixnum and rb_cBignum.
    (C): Use Integer instead of Fixnum and Bignum.

  • numeric.c (fix_succ): Removed.
    (Init_Numeric): Define Fixnum as Integer.

  • bignum.c (bignew): Use rb_cInteger instead of Rb_cBignum.
    (rb_int_coerce): replaced from rb_big_coerce and return fixnums
    as-is.
    (Init_Bignum): Define Bignum as Integer.
    Don't define ===.

  • error.c (builtin_class_name): Return "Integer" for fixnums.

  • sprintf.c (ruby_sfvextra): Use rb_cInteger instead of rbcFixnum.

  • ext/-test-/testutil: New directory to test.
    Currently it provides utilities for fixnum and bignum.

  • ext/json/generator/generator.c: Define mInteger_to_json.

  • lib/mathn.rb (Fixnum#/): Redefinition removed.

History

#1 [ruby-core:72924] Updated by Nobuyoshi Nakada about 1 year ago

  • Description updated (diff)

Could you elaborate the road map?

  1. move methods from Fixnum and Bignum to Integer, with code to dispatch by its range
  2. deprecate direct use of Fixnum and Bignum
  3. remove them

Please correct it if something is missing.

#2 [ruby-core:73309] Updated by Robert A. Heiler 12 months ago

I like the idea behind it. \o/

Would probably make more sense too if the defining difference for it
used to be due to performance reasons, as Yui NARUSE wrote.

#3 [ruby-core:73911] Updated by Jörg W Mittag 11 months ago

Yui NARUSE wrote:

CRuby has two Integer classes, Fixnum and Bignum.
But it is implementation detail.
They should be seen as a single class Integer like Flonum.

I like this very much! It always struck me as odd that Integer is special-cased like that. Some implementations have special-cased optimizations for small arrays or hashes (e.g. less than 6 elements), yet they don't have have a separate SmallArray or SmallHash class. Some implementations have special-cased optimizations for small objects (e.g. less than 3 instance variables), yet they don't have a separate SmallObject inheritance hierarchy. YARV has flonums, yet no separate Flonum class. And so on …

The ISO Ruby Language Specification also explicitly treats them as private internal implementation details. It only specifies the Integer class and then allows for "implementation-defined subclasses". (And in fact, I suspect that sentence was only put in there because Fixnum/Bignum already existed when the spec was written.)

Treating it as a private internal implementation detail would also give more freedom to implementors to choose different, more aggressive, or maybe just no optimizations at all. (The latter might be useful for very small implementations intended for embedded use, or very simple implementations intended for educational use.)

#4 [ruby-core:74364] Updated by Shyouhei Urabe 10 months ago

  • Status changed from Open to Assigned
  • Assignee set to Kenta Murata

#5 [ruby-core:75325] Updated by Akira Tanaka 9 months ago

I made a patch to unify Fixnum and Bignum:
unify-fixnum-and-bignum.patch

For rough compatibility, I defined Fixnum and Bignum as Integer.
So, foo.kind_of?(Fixnum) works as foo.kind_of?(Integer).
This works mostly because most application doesn't have interest
on the boundary between Fixnum and Bignum which vary on platforms.

#6 [ruby-core:75349] Updated by Akira Tanaka 9 months ago

I feel that unifying Fixnum and Bignum is simple and beautiful.

However I'm not sure that there are enough concrete benefits over
the incompatibility.

For example, when I made the patch, I need to fix complex.c and
ext/json because they used Fixnum and Bignum class to dispatch.

mathn.rb is another example. mathn.rb modifies methods in Fixnum
and Bignum.
I changed mathn.rb to modify methods in Integer.

Concrete benefits I know is that less code for mathn.rb like library.
It needs to modify only one class, Integer, instead of two, Fixnum and Bignum.

Other benefits?

Are they enough to justify the incompatibility?
I hope enough benefits to justify this issue.

#7 [ruby-core:75362] Updated by Martin Dürst 9 months ago

On 2016/05/04 23:54, akr@fsij.org wrote:

I feel that unifying Fixnum and Bignum is simple and beautiful.

However I'm not sure that there are enough concrete benefits over
the incompatibility.

Concrete benefits I know is that less code for mathn.rb like library.
It needs to modify only one class, Integer, instead of two, Fixnum and Bignum.

Other benefits?

Are they enough to justify the incompatibility?
I hope enough benefits to justify this issue.

I think there are some serious benefits in documentation and for
learning Ruby. But these are not so concrete, and more long term.

Regards, Martin.

#8 [ruby-core:75374] Updated by Yui NARUSE 9 months ago

Akira Tanaka wrote:

Other benefits?

As you show people write Fixnum case and Bignum case.
But sometimes they write only Fixnum even though they must write Bignum case.
Unified Integer avoids such pitfall.

#9 [ruby-core:75542] Updated by Yukihiro Matsumoto 8 months ago

I should have unified those classes at the first hand. It's much cleaner & simpler.
Try unifying them and see if it would go well.

Matz.

#10 Updated by Akira Tanaka 8 months ago

  • Status changed from Assigned to Closed

Applied in changeset r55024.


[Feature #12005] Unify Fixnum and Bignum into Integer

  • [Feature #12005] Unify Fixnum and Bignum into Integer

  • include/ruby/ruby.h (rb_class_of): Return rb_cInteger for fixnums.

  • insns.def (INTEGER_REDEFINED_OP_FLAG): Unified from
    FIXNUM_REDEFINED_OP_FLAG and BIGNUM_REDEFINED_OP_FLAG.

  • vm_core.h: Ditto.

  • vm_insnhelper.c (opt_eq_func): Use INTEGER_REDEFINED_OP_FLAG instead
    of FIXNUM_REDEFINED_OP_FLAG.

  • vm.c (vm_redefinition_check_flag): Use rb_cInteger instead of
    rb_cFixnum and rb_cBignum.
    (C): Use Integer instead of Fixnum and Bignum.

  • numeric.c (fix_succ): Removed.
    (Init_Numeric): Define Fixnum as Integer.

  • bignum.c (bignew): Use rb_cInteger instead of Rb_cBignum.
    (rb_int_coerce): replaced from rb_big_coerce and return fixnums
    as-is.
    (Init_Bignum): Define Bignum as Integer.
    Don't define ===.

  • error.c (builtin_class_name): Return "Integer" for fixnums.

  • sprintf.c (ruby_sfvextra): Use rb_cInteger instead of rbcFixnum.

  • ext/-test-/testutil: New directory to test.
    Currently it provides utilities for fixnum and bignum.

  • ext/json/generator/generator.c: Define mInteger_to_json.

  • lib/mathn.rb (Fixnum#/): Redefinition removed.

#11 Updated by Yui NARUSE 7 months ago

  • Related to Bug #12427: Defining methods with the same name to both Fixnum and Bignum classes could cause SEGV in C extensions since Feature #12005 added

#12 [ruby-core:76168] Updated by Yui NARUSE 7 months ago

  • Description updated (diff)

#13 Updated by Kazuhiro NISHIYAMA 5 months ago

  • Related to Feature #12739: deprecate_constant :Fixnum, :Bignum added

Also available in: Atom PDF