- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
Relative Performance of Symbol#to_proc in Popular Ruby Implementations声明在 MRI Ruby 1.8.7 中,Symbol#to_proc
在其基准测试中比替代方案慢 30% 到 130%,但在 YARV Ruby 1.9.2 中并非如此。
为什么会这样? 1.8.7 的创建者没有用纯 Ruby 编写 Symbol#to_proc
。
此外,是否有任何 gem 可以为 1.8 提供更快的 Symbol#to_proc 性能?
(符号#to_proc 在我使用 ruby-prof 时开始出现,所以我不认为我犯了过早优化的罪)
最佳答案
1.8.7 中的to_proc
实现如下所示(参见object.c
):
static VALUE
sym_to_proc(VALUE sym)
{
return rb_proc_new(sym_call, (VALUE)SYM2ID(sym));
}
而 1.9.2 实现(参见 string.c
)如下所示:
static VALUE
sym_to_proc(VALUE sym)
{
static VALUE sym_proc_cache = Qfalse;
enum {SYM_PROC_CACHE_SIZE = 67};
VALUE proc;
long id, index;
VALUE *aryp;
if (!sym_proc_cache) {
sym_proc_cache = rb_ary_tmp_new(SYM_PROC_CACHE_SIZE * 2);
rb_gc_register_mark_object(sym_proc_cache);
rb_ary_store(sym_proc_cache, SYM_PROC_CACHE_SIZE*2 - 1, Qnil);
}
id = SYM2ID(sym);
index = (id % SYM_PROC_CACHE_SIZE) << 1;
aryp = RARRAY_PTR(sym_proc_cache);
if (aryp[index] == sym) {
return aryp[index + 1];
}
else {
proc = rb_proc_new(sym_call, (VALUE)id);
aryp[index] = sym;
aryp[index + 1] = proc;
return proc;
}
}
如果您剥离所有初始化 sym_proc_cache
的繁琐工作,那么您将(或多或少)剩下这个:
aryp = RARRAY_PTR(sym_proc_cache);
if (aryp[index] == sym) {
return aryp[index + 1];
}
else {
proc = rb_proc_new(sym_call, (VALUE)id);
aryp[index] = sym;
aryp[index + 1] = proc;
return proc;
}
所以真正的区别是 1.9.2 的 to_proc
缓存生成的 Proc,而 1.8.7 每次调用 to_proc
时都会生成一个全新的 Proc。除非每次迭代都在单独的过程中完成,否则这两者之间的性能差异将被您所做的任何基准测试放大;但是,每个流程的一次迭代会掩盖您尝试用启动成本进行基准测试的内容。
rb_proc_new
的内容看起来几乎相同(参见 1.8.7 的 eval.c
或 1.9.2 的 proc.c
) 但 1.9.2 可能会从 rb_iterate
中的任何性能改进中略微受益。缓存可能是最大的性能差异。
值得注意的是,symbol-to-hash 缓存是固定大小的(67 个条目,但我不确定 67 是从哪里来的,可能与 symbol-to 常用的运算符数量有关-proc 转换):
id = SYM2ID(sym);
index = (id % SYM_PROC_CACHE_SIZE) << 1;
/* ... */
if (aryp[index] == sym) {
如果您使用超过 67 个符号作为 proc,或者如果您的符号 ID 重叠(mod 67),那么您将无法获得缓存的全部好处。
Rails 和 1.9 编程风格涉及很多简写形式,例如:
id = SYM2ID(sym);
index = (id % SYM_PROC_CACHE_SIZE) << 1;
而不是更长的显式 block 形式:
ints = strings.collect { |s| s.to_i }
sum = ints.inject(0) { |s,i| s += i }
鉴于这种(流行的)编程风格,通过缓存查找来以内存换取速度是有意义的。
您不太可能从 gem 获得更快的实现,因为 gem 必须替换一部分核心 Ruby 功能。不过,您可以将 1.9.2 缓存修补到 1.8.7 源代码中。
关于c - 为什么 Ruby 1.8.7 中的 Symbol#to_proc 变慢了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6501030/
Rubyist 众所周知,& 会在符号上调用 to_proc,所以 [:a, :b, :c].map(&:to_s) 相当于 [:a, :b, :c].map { |e| e.to_s } # =>
所以我正在尝试弄清楚 sym.to_proc 方法是如何工作的,假设任何实现 to_proc 的对象都有一个速记,它只是一个 & 在对象之前,但是这个速记的行为似乎不稳定: 这行得通 irb(main
documentation是空的,但我想知道如何使用它才能知道它什么时候有用。 最佳答案 这样你就可以将哈希过程传递给 map 之类的东西。 https://bugs.ruby-lang.org/is
我喜欢在 Ruby 中如何将方法作为 block 传递,就像这样使用 Symbol#to_proc: [1.0, 2.0, 3.0].map(&:to_i) #=> [1, 2, 3] 我还可以定义自
a = (1..10).to_a a.map(&:even?) => [false, true, false, true, false, true, false, true, false, true]
我找到了这个在 Ruby 中实现自定义 Symbol#to_proc 的示例代码: class Symbol def to_proc puts "In the new Symbol#to_
我正在学习 Rails 并关注 this thread .我坚持使用 to_proc 方法。我只将符号视为字符串的替代品(它们类似于字符串,但在内存方面更便宜)。如果还有什么我缺少的符号,请告诉我。请
假设我有类(class): class Car attr_accessor :brand, :color def initialize(br, col) @brand = br
我discovered tonight Ruby 的 Symbol#to_proc处理第一个以外的其他产生值作为方法调用的参数。换句话说,这两个方法调用是等价的: yields_three{ |a,b
这个有效: strings = ["1", "2", "3"] nums = strings.map(&:to_i) 我们看到应用于 Symbol 对象的 & (to_proc 方法) 变成了一个 b
to_proc 似乎不适用于细化中定义的方法: module ArrayExtensions refine Array do def sum reduce(0, :+)
我正在尝试创建一个小的 Ruby hack 来制作类似于 Symbol#to_proc hack 的反向操作。而 Symbol#to_proc hack 使这成为可能: some_array.each
如何使用 Symbol#to_proc 语法编写以下内容? [[a,1],[b,2],[c,3]].map { |r| r[0] } 这给出了参数数量错误的错误。但是我不知道在哪里给出索引参数。 [[
您不能使用 Enumerable#map 从数组的每个元素中查找相同的值使用 &:method_name 快捷方式的散列: # INVALID: [{a:'bar', b:'world'}, {a:'
非常简短的问题:是否可以使用 Symbol#to_proc 简写(例如 lambda(&:upcase) 和 Ruby 中的 stabby lambda 语法?例如,我可以这样说: p = lambd
我想知道是否有一种方法可以使用 (&:method) 链接一个方法 例如: array.reject { |x| x.strip.empty? } 把它变成: array.reject(&:strip
我正在学习一元运算符 &。 关于在方法调用的参数中使用 & 有一些很好的问题。通常格式类似于 some_obj.some_method(&:symbol): Ruby unary operator &
好的,这是我第二次尝试调试 Sinatra 应用程序的内存问题。我相信这次我已经将它确定为简单的示例代码。 似乎当我通过 .map(&:some_method) 过滤数组时,它会导致该数组中的项目无法
给定以下采用一个参数的方法: def foo(arg); p arg; end 我可以用一个空数组来调用它: foo([]) # prints [] 我也可以将它保存为一个 Method 对象并用一个
Relative Performance of Symbol#to_proc in Popular Ruby Implementations声明在 MRI Ruby 1.8.7 中,Symbol#to
我是一名优秀的程序员,十分优秀!