Introducing 

Prezi AI.

Your new presentation assistant.

Refine, enhance, and tailor your content, source relevant images, and edit visuals quicker than ever before.

Loading content…
Loading…
Transcript

你不需要知道的

各種Ruby實現

關於我

莫樞 Kris Mok

  • 目前在淘寶就職,參與後台系統的開發。
  • 對編程語言的設計與實現非常感興趣,
  • 主要在編譯器與虚擬機方面
  • 接觸Ruby開始就對其愛不釋手

希望能與大家共同學習進 ^_^

  • 博客:

http://rednaxelafx.javaeye.com

  • 高級語言虚擬機圈子

http://hllvm.group.javaeye.com

@rednaxelafx

毎種實現都有自己的特點

各自都有獨特的擴展

不要擔心“社區分裂”“標準不一”

核心語言都是一樣的

  • RubySpec
  • Ruby語言規範

但種實現通過獨特的擴展能為各自的場景提供最好的服務

1 + 2 * 3

Ruby 1.8: MRI

  • 遍抽象語法樹的解釋器
  • 保守式、標記-清掃式GC
  • 綠色線程

irb(main):001:0> ISeq = RubyVM::InstructionSequence

=> RubyVM::InstructionSequence

irb(main):002:0> iseq = ISeq.compile '1 + 2 * 3'

=> <RubyVM::InstructionSequence:<compiled>@<compiled>>

irb(main):003:0> puts iseq.disassemble

== disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==

0000 trace 1 ( 1)

0002 putobject 1

0004 putobject 2

0006 putobject 3

0008 opt_mult

0009 opt_plus

0010 leave

=> nil

Ruby 1.9: YARV

  • “字節碼”解釋器
  • 保守式、標記-清掃式GC
  • 操作系統的原生線程或綠色線程
  • 有GVL
  • 改良的正則表達式引擎

主要用C語言實現

Re: Ruby GC question (MRI, JRuby, etc)

Posted by Yukihiro Matsumoto (Guest) on 2010-08-21 02:15

Hi,

In message "Re: Ruby GC question (MRI, JRuby, etc)"

on Sat, 21 Aug 2010 08:15:15 +0900, Jörg W Mittag

<JoergWMittag+Ruby@GoogleMail.Com> writes:

|In particular, the GC algorithms in MRI and YARV are specifically

|designed with the assumption that they will never actually run in

|99.999% of all cases. They are designed for scripting, where a script

|doesn't even allocate enough memory to trigger a collection, runs for

|a couple of seconds and then exits, after which the OS simply reclaims

|the memory: no GC needed.

99.999% is a bit over-exaggerated, but it is true that garbage

collection algorithm of YARV and MRI focus for throughput on

non-memory extensive short-running programs, and GC of REE is not

suitable for those programs.

matz.

IronRuby

  • 建立于DLR与CLI平台上
  • 充分利用底層平台已有的功能,如解釋器/JIT編譯器/GC/線程
  • 与.NET/Silverlight

/Mono/Moonlight無縫整合

  • 兼容1.8.6(1.0/1.1)与1.9.2(1.1.1)

主要用C#語言實現

Where is my Ruby?

Rubinius

  • 全新的Ruby實現
  • 兼容1.8.7
  • 先進的虚擬機
  • 解釋器與JIT編譯器組成的混合執行模式
  • JIT編譯器基於LLVM
  • 先進的GC
  • 本地線程
  • 即將削除GIL
  • 強大的調試器

字節碼編譯器與Ruby核心庫

與標準庫都主要用Ruby實現

可用于帮助理解Ruby核心的行為

class Array

include Enumerable

# Passes each index of the Array to the given block

# and returns self. We re-evaluate @total each time

# through the loop in case the array has changed.

def each_index

return to_enum(:each_index) unless block_given?

i = @start

total = i + @total

while i < total

yield i

i += 1

end

self

end

end

static VALUE

rb_ary_each_index(VALUE ary)

{

long i;

RETURN_ENUMERATOR(ary, 0, 0);

for (i=0; i<RARRAY_LEN(ary); i++) {

rb_yield(LONG2NUM(i));

}

return ary;

}

void

Init_Array(void)

{

rb_cArray = rb_define_class("Array", rb_cObject);

rb_include_module(rb_cArray, rb_mEnumerable);

rb_define_method(rb_cArray, "each_index", rb_ary_each_index, 0);

}

class Fixnum < Integer

def +(o)

Ruby.primitive :fixnum_add

redo_coerced :+, o

end

end

class Fixnum : public Integer {

public:

// Ruby.primitive! :fixnum_add

Integer* add(STATE, Fixnum* other) {

native_int r = to_native() + other->to_native();

if(r > FIXNUM_MAX || r < FIXNUM_MIN) {

return Bignum::from(state, r);

} else {

return Fixnum::from(r);

}

}

}

有多先進?

114.8!

Rubinius 1.0RC1的速度是

Ruby 1.8的114.8倍!

  • 在什麼測試上?

為什麼能快那麼多?

JIT編譯器的優化

方法内聯

method inlining

無用代碼削除

dead code elimination

先進的JIT編譯器

提升Ruby代碼性能

Rubinius内核有大量Ruby

因此JIT編譯器帶來

整體平台的性能提升

還有先進的GC

分代式GC

降低GC造成的應用暫停時間

年輕代:成熟的Baker GC

年老代:新鋭的Immix GC

2007年論文:

Immix Garbage Collection:

Fast Collection, Space Efficiency,

and Mutator Locality

module GC

# The Maglev in-memory garbage collector runs

# automatically whenever GC is required.

# It cannot be disabled.

def self.start

# has no effect

end

def self.enable

false # gc is never disabled

end

def self.disable

# has no effect

false

end

def garbage_collect

# has no effect

end

def self.stress# added for 1.8.7

false

end

def self.stress=(bool)# added for 1.8.7

# has no effect

false

end

end

大家熟悉和喜愛的

果咩那塞 >_<

Ruby 1.8: MRI

  • 遍抽象語法樹的解釋器
  • 保守式、標記-清掃式GC
  • 綠色線程

RubyConf 2009上的Rubinius session

(Evan Phoenix)

Ruby程序員的Ruby!

module GC

def self.start

run(false)

end

def self.run(force)

Ruby.primitive :vm_gc_start

raise PrimitiveFailure, "GC.run primitive failed"

end

# Totally fake.

def self.stress

@stress_level ||= false

end

# Totally fake.

def self.stress=(flag)

@stress_level = !!flag

end

# Totally fake.

@enabled = true

def self.enable

# We don't support disable, so sure! enabled!

ret = !@enabled

@enabled = true

return ret

end

# Totally fake.

def self.disable

# Treat this like a request that we don't honor.

ret = !@enabled

@enabled = false

return ret

end

def garbage_collect

GC.start

end

end

module ObjectSpace

def self.find_object(query, callable)

Ruby.primitive :vm_find_object

raise PrimitiveFailure, "vm_each_object failed"

end

end

static VALUE

fix_plus(VALUE x, VALUE y)

{

if (FIXNUM_P(y)) {

long a, b, c;

VALUE r;

a = FIX2LONG(x);

b = FIX2LONG(y);

c = a + b;

r = LONG2NUM(c);

return r;

}

switch (TYPE(y)) {

case T_BIGNUM:

return rb_big_plus(y, x);

case T_FLOAT:

return DBL2NUM((double)FIX2LONG(x) + RFLOAT_VALUE(y));

default:

return rb_num_coerce_bin(x, y, '+');

}

}

void

Init_Numeric(void)

{

rb_cNumeric = rb_define_class("Numeric", rb_cObject);

rb_cInteger = rb_define_class("Integer", rb_cNumeric);

rb_cFixnum = rb_define_class("Fixnum", rb_cInteger);

rb_define_method(rb_cFixnum, "+", fix_plus, 1);

}

typo: it's :* here

What you don't need to know

about Ruby implementations

Learn more about creating dynamic, engaging presentations with Prezi