Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Always compute Enumerable#count through enumeration for 1.9+ #979

Merged
merged 1 commit into from

2 participants

Daniel Marcotte Charles Oliver Nutter
Daniel Marcotte

Looks like MRI dropped the #size optimization in #count in 1.9.2. Comparing the 1.8 doc to the 2.0 doc also shows this is the intended behavior (interestingly, the 1.9 doc seems to be out of date)

Update to match that behavior (also fixes #922).

Daniel Marcotte dmarcotte Don't use #size for #count in 1.9+
Starting in 1.9, #count is always computed through enumeration
e542157
Charles Oliver Nutter headius merged commit a4f8fca into from
Charles Oliver Nutter
Owner

Thanks! Do you think you'll be able to look into enumerator_with_size logic too?

Daniel Marcotte

Absolutely. Might be a couple of weeks before I can send it, but it's definitely coming.

Daniel Marcotte dmarcotte referenced this pull request
Closed

Fix count_spec (1.7.x) #1231

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 30, 2013
  1. Daniel Marcotte

    Don't use #size for #count in 1.9+

    dmarcotte authored
    Starting in 1.9, #count is always computed through enumeration
This page is out of date. Refresh to see the latest.
Showing with 17 additions and 5 deletions.
  1. +17 −5 core/src/main/java/org/jruby/RubyEnumerable.java
22 core/src/main/java/org/jruby/RubyEnumerable.java
View
@@ -121,7 +121,16 @@ private static void checkContext(ThreadContext firstContext, ThreadContext secon
}
}
- @JRubyMethod
+ @JRubyMethod(name = "count", compat = CompatVersion.RUBY1_8)
+ public static IRubyObject count18(ThreadContext context, IRubyObject self, final Block block) {
+ if (!block.isGiven() && self.respondsTo("size")) {
+ return self.callMethod(context, "size");
+ }
+
+ return count(context, self, block);
+ }
+
+ @JRubyMethod(name = "count", compat = CompatVersion.RUBY1_9)
public static IRubyObject count(ThreadContext context, IRubyObject self, final Block block) {
final Ruby runtime = context.runtime;
final int result[] = new int[] { 0 };
@@ -134,8 +143,6 @@ public IRubyObject yield(ThreadContext context, IRubyObject arg) {
}
});
} else {
- if (self.respondsTo("size")) return self.callMethod(context, "size");
-
each(context, self, new JavaInternalBlockBody(runtime, context, "Enumerable#count", Arity.NO_ARGUMENTS) {
public IRubyObject yield(ThreadContext context, IRubyObject unusedValue) {
result[0]++;
@@ -145,8 +152,13 @@ public IRubyObject yield(ThreadContext context, IRubyObject unusedValue) {
}
return RubyFixnum.newFixnum(runtime, result[0]);
}
-
- @JRubyMethod
+
+ @JRubyMethod(name = "count", compat = CompatVersion.RUBY1_8)
+ public static IRubyObject count18(ThreadContext context, IRubyObject self, final IRubyObject methodArg, final Block block) {
+ return count(context, self, methodArg, block);
+ }
+
+ @JRubyMethod(name = "count", compat = CompatVersion.RUBY1_9)
public static IRubyObject count(ThreadContext context, IRubyObject self, final IRubyObject methodArg, final Block block) {
final Ruby runtime = context.runtime;
final int result[] = new int[] { 0 };
Something went wrong with that request. Please try again.