Ruby,Block,CPS 和 call/cc

本文探讨了Ruby中块的性能损耗、CPS模块构建及Kernel.callcc的使用情况。作者试图理解块传递与直接执行代码的区别,以及不同环境下块的实现机制。

原文:Ruby, Blocks, CPS and call/cc

这是一个在Chez Box有关Ruby的周末。

这个夜晚我一直搞不懂,各种各样的原理是如何被应用得那么广泛,Ruby运行库又如何处理它们?

下面,我希望了解:
 传递/调用一个块与在当前的frame上"直接"执行代码,相比之下,性能损耗何在?如果块在frame结束扩如中不引用任何符号,消耗会上升还是降低?对于CLR和C#,我虽然知道这是怎么回事,但是重这里应用的那个环境来看,我却不那么肯定我的自觉。
 
 有人开始使用块来构建简单的,类似于CPS的模块吗?如果有,那么运行时栈管理器是如何支持的?
 
 广泛使用Kenel.callcc的人多吗?或者将来某个时候使用它的人多吗?在各种操作系统上,它是如何执行的?对于在栈上可能发生的extern "C"调用,运行时库又会如何处理?  



Trackback: http://tb.blog.youkuaiyun.com/TrackBack.aspx?PostId=1448574


(process:19720): VIPS-WARNING **: 00:21:43.293: unable to load "C:\Ruby34-x64\msys64\ucrt64/lib/vips-modules-8.17\vips-heif.dll" -- 'C:\Ruby34-x64\msys64\ucrt64/lib/vips-modules-8.17\vips-heif.dll': 找不到指定的模块。 (process:19720): VIPS-WARNING **: 00:21:43.293: unable to load "C:\Ruby34-x64\msys64\ucrt64/lib/vips-modules-8.17\vips-jxl.dll" -- 'C:\Ruby34-x64\msys64\ucrt64/lib/vips-modules-8.17\vips-jxl.dll': 找不到指定的模块。 (process:19720): VIPS-WARNING **: 00:21:43.296: unable to load "C:\Ruby34-x64\msys64\ucrt64/lib/vips-modules-8.17\vips-magick.dll" -- 'C:\Ruby34-x64\msys64\ucrt64/lib/vips-modules-8.17\vips-magick.dll': 找不到指定的模块。 (process:19720): VIPS-WARNING **: 00:21:43.296: unable to load "C:\Ruby34-x64\msys64\ucrt64/lib/vips-modules-8.17\vips-openslide.dll" -- 'C:\Ruby34-x64\msys64\ucrt64/lib/vips-modules-8.17\vips-openslide.dll': 找不到指定的模块。 (process:19720): VIPS-WARNING **: 00:21:43.296: unable to load "C:\Ruby34-x64\msys64\ucrt64/lib/vips-modules-8.17\vips-poppler.dll" -- 'C:\Ruby34-x64\msys64\ucrt64/lib/vips-modules-8.17\vips-poppler.dll': 找不到指定的模块。 C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:71:in 'block (2 levels) in Bundler::Runtime#require': There was an error while trying to load the gem 'sqlite3'. (Bundler::GemRequireError) Gem Load Error is: cannot load such file -- sqlite3/sqlite3_native Backtrace for gem load error is: C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'Kernel.require' C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'block (2 levels) in Kernel#replace_require' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bootsnap-1.18.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in 'Kernel#require' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/zeitwerk-2.7.3/lib/zeitwerk/core_ext/kernel.rb:34:in 'Kernel#require' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/sqlite3-2.8.0-x64-mingw-ucrt/lib/sqlite3.rb:6:in '<main>' <internal:C:/Ruby34-x64/lib/ruby/3.4.0/rubygems/core_ext/kernel_require.rb>:37:in 'Kernel#require' <internal:C:/Ruby34-x64/lib/ruby/3.4.0/rubygems/core_ext/kernel_require.rb>:37:in 'Kernel#require' C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'block (2 levels) in Kernel.replace_require' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/zeitwerk-2.7.3/lib/zeitwerk/core_ext/kernel.rb:34:in 'Kernel.require' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:63:in 'block (2 levels) in Bundler::Runtime#require' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:58:in 'Array#each' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:58:in 'block in Bundler::Runtime#require' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:52:in 'Array#each' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:52:in 'Bundler::Runtime#require' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler.rb:214:in 'Bundler.require' D:/1111/blog/config/application.rb:7:in '<main>' C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'Kernel.require' C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'block (2 levels) in Kernel#replace_require' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bootsnap-1.18.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in 'Kernel#require' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/commands/server/server_command.rb:139:in 'block in Rails::Command::ServerCommand#perform' <internal:kernel>:91:in 'Kernel#tap' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/commands/server/server_command.rb:136:in 'Rails::Command::ServerCommand#perform' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/thor-1.4.0/lib/thor/command.rb:28:in 'Thor::Command#run' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/thor-1.4.0/lib/thor/invocation.rb:127:in 'Thor::Invocation#invoke_command' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command/base.rb:176:in 'Rails::Command::Base#invoke_command' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/thor-1.4.0/lib/thor.rb:538:in 'Thor.dispatch' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command/base.rb:71:in 'Rails::Command::Base.perform' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command.rb:65:in 'block in Rails::Command.invoke' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command.rb:143:in 'Rails::Command.with_argv' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command.rb:63:in 'Rails::Command.invoke' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/commands.rb:18:in '<main>' C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'Kernel.require' C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'block (2 levels) in Kernel#replace_require' C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bootsnap-1.18.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in 'Kernel#require' bin/rails:4:in '<main>' Bundler Error Backtrace: from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:58:in 'Array#each' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:58:in 'block in Bundler::Runtime#require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:52:in 'Array#each' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:52:in 'Bundler::Runtime#require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler.rb:214:in 'Bundler.require' from D:/1111/blog/config/application.rb:7:in '<main>' from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'Kernel.require' from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'block (2 levels) in Kernel#replace_require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bootsnap-1.18.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in 'Kernel#require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/commands/server/server_command.rb:139:in 'block in Rails::Command::ServerCommand#perform' from <internal:kernel>:91:in 'Kernel#tap' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/commands/server/server_command.rb:136:in 'Rails::Command::ServerCommand#perform' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/thor-1.4.0/lib/thor/command.rb:28:in 'Thor::Command#run' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/thor-1.4.0/lib/thor/invocation.rb:127:in 'Thor::Invocation#invoke_command' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command/base.rb:176:in 'Rails::Command::Base#invoke_command' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/thor-1.4.0/lib/thor.rb:538:in 'Thor.dispatch' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command/base.rb:71:in 'Rails::Command::Base.perform' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command.rb:65:in 'block in Rails::Command.invoke' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command.rb:143:in 'Rails::Command.with_argv' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command.rb:63:in 'Rails::Command.invoke' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/commands.rb:18:in '<main>' from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'Kernel.require' from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'block (2 levels) in Kernel#replace_require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bootsnap-1.18.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in 'Kernel#require' from bin/rails:4:in '<main>' C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'Kernel.require': cannot load such file -- sqlite3/sqlite3_native (LoadError) from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'block (2 levels) in Kernel#replace_require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bootsnap-1.18.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:17:in 'Kernel#require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/zeitwerk-2.7.3/lib/zeitwerk/core_ext/kernel.rb:34:in 'Kernel#require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/sqlite3-2.8.0-x64-mingw-ucrt/lib/sqlite3.rb:6:in '<main>' from <internal:C:/Ruby34-x64/lib/ruby/3.4.0/rubygems/core_ext/kernel_require.rb>:37:in 'Kernel#require' from <internal:C:/Ruby34-x64/lib/ruby/3.4.0/rubygems/core_ext/kernel_require.rb>:37:in 'Kernel#require' from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'block (2 levels) in Kernel.replace_require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/zeitwerk-2.7.3/lib/zeitwerk/core_ext/kernel.rb:34:in 'Kernel.require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:63:in 'block (2 levels) in Bundler::Runtime#require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:58:in 'Array#each' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:58:in 'block in Bundler::Runtime#require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:52:in 'Array#each' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:52:in 'Bundler::Runtime#require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler.rb:214:in 'Bundler.require' from D:/1111/blog/config/application.rb:7:in '<main>' from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'Kernel.require' from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'block (2 levels) in Kernel#replace_require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bootsnap-1.18.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in 'Kernel#require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/commands/server/server_command.rb:139:in 'block in Rails::Command::ServerCommand#perform' from <internal:kernel>:91:in 'Kernel#tap' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/commands/server/server_command.rb:136:in 'Rails::Command::ServerCommand#perform' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/thor-1.4.0/lib/thor/command.rb:28:in 'Thor::Command#run' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/thor-1.4.0/lib/thor/invocation.rb:127:in 'Thor::Invocation#invoke_command' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command/base.rb:176:in 'Rails::Command::Base#invoke_command' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/thor-1.4.0/lib/thor.rb:538:in 'Thor.dispatch' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command/base.rb:71:in 'Rails::Command::Base.perform' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command.rb:65:in 'block in Rails::Command.invoke' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command.rb:143:in 'Rails::Command.with_argv' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command.rb:63:in 'Rails::Command.invoke' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/commands.rb:18:in '<main>' from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'Kernel.require' from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'block (2 levels) in Kernel#replace_require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bootsnap-1.18.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in 'Kernel#require' from bin/rails:4:in '<main>' C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'Kernel.require': 127: 找不到指定的程序。 - C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/sqlite3-2.8.0-x64-mingw-ucrt/lib/sqlite3/3.4/sqlite3_native.so (LoadError) from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'block (2 levels) in Kernel#replace_require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bootsnap-1.18.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in 'Kernel#require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/zeitwerk-2.7.3/lib/zeitwerk/core_ext/kernel.rb:34:in 'Kernel#require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/sqlite3-2.8.0-x64-mingw-ucrt/lib/sqlite3.rb:4:in '<main>' from <internal:C:/Ruby34-x64/lib/ruby/3.4.0/rubygems/core_ext/kernel_require.rb>:37:in 'Kernel#require' from <internal:C:/Ruby34-x64/lib/ruby/3.4.0/rubygems/core_ext/kernel_require.rb>:37:in 'Kernel#require' from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'block (2 levels) in Kernel.replace_require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/zeitwerk-2.7.3/lib/zeitwerk/core_ext/kernel.rb:34:in 'Kernel.require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:63:in 'block (2 levels) in Bundler::Runtime#require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:58:in 'Array#each' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:58:in 'block in Bundler::Runtime#require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:52:in 'Array#each' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler/runtime.rb:52:in 'Bundler::Runtime#require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bundler-2.7.2/lib/bundler.rb:214:in 'Bundler.require' from D:/1111/blog/config/application.rb:7:in '<main>' from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'Kernel.require' from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'block (2 levels) in Kernel#replace_require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bootsnap-1.18.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in 'Kernel#require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/commands/server/server_command.rb:139:in 'block in Rails::Command::ServerCommand#perform' from <internal:kernel>:91:in 'Kernel#tap' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/commands/server/server_command.rb:136:in 'Rails::Command::ServerCommand#perform' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/thor-1.4.0/lib/thor/command.rb:28:in 'Thor::Command#run' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/thor-1.4.0/lib/thor/invocation.rb:127:in 'Thor::Invocation#invoke_command' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command/base.rb:176:in 'Rails::Command::Base#invoke_command' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/thor-1.4.0/lib/thor.rb:538:in 'Thor.dispatch' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command/base.rb:71:in 'Rails::Command::Base.perform' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command.rb:65:in 'block in Rails::Command.invoke' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command.rb:143:in 'Rails::Command.with_argv' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/command.rb:63:in 'Rails::Command.invoke' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/railties-8.1.1/lib/rails/commands.rb:18:in '<main>' from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'Kernel.require' from C:/Ruby34-x64/lib/ruby/3.4.0/bundled_gems.rb:82:in 'block (2 levels) in Kernel#replace_require' from C:/Ruby34-x64/lib/ruby/gems/3.4.0/gems/bootsnap-1.18.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in 'Kernel#require' from bin/rails:4:in '<main>'
最新发布
11-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值