Commit Graph
509 Commits
Author SHA1 Message Date
Shourya Singh cfa50b0093 [DOC] Fix outdated results in examples
Some example results in the docs no longer match what Ruby prints:

* Array#flatten and Array#flatten!: Set#inspect now shows Set[6, 7]
* Module: Mod.constants also includes Math::DomainError
* Module#const_get and Module#const_set: Floats are shown with full
  precision
* Complex#imag: sin(PI/4) is 0.7071067811865475
* String#<<: missing # in #<Encoding:UTF-8>
* Enumerable#inject: typo #=>>
2026-10-21 17:52:28 +09:00
Hiroshi SHIBATAandClaude Opus 5.5 cedc07cc8d Count Enumerable#count with an unsigned member of MEMO
Only imemo_count_up and imemo_count_value treat the count as unsigned, so keep cnt as rb_len_t for the other users of MEMO and give them their own member.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 16:45:25 +09:00
Hiroshi SHIBATAandClaude Opus 5.5 ee3d6c987b Use rb_len_t for the lengths and indices left in long
These still held String and Array lengths and indices in long, or cast them to long. rb_len_t is still long everywhere, so the generated code does not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 16:45:25 +09:00
Hiroshi SHIBATAandClaude Opus 5 ac09093b91 Use rb_len_t for element counts in enum.c
The MEMO counter becomes rb_len_t, which is wider than `unsigned long` once rb_len_t widens, so the Enumerable#count overflow builds the next value from RB_ULEN_MAX instead of assuming `unsigned long` words.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-09 14:00:15 +09:00
Nobuyoshi Nakada 3ac822c7e1 [Bug #22388] Fix use-after-free in Enumerable#sum 2026-09-29 19:26:51 +09:00
Nobuyoshi Nakada c861619e89 [Bug #22388] Fix use-after-free in some Enumerable methods 2026-09-29 19:26:22 +09:00
Hiroshi SHIBATAandClaude Opus 5.5 177587f5e1 Fix cycle(n).size for n beyond the Fixnum range
LONG2FIX does not check the range, so `[1].cycle(2**30).size` returned -1073741824 on mswin. 64-bit platforms hit the same with 2**62.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 16:20:31 +09:00
Nobuyoshi Nakada 22d22128c8 Clear temporary containers in Enumerable#uniq
Clear temporary containers after extracting the result to avoid
retaining their contents through a captured block.
2026-09-24 19:22:12 +09:00
Nobuyoshi Nakada 34d2105de0 Fix escaped blocks in Enumerable#uniq
Keep the state for block-based `Enumerable#uniq` alive after it returns,
as `each` can retain and later invoke the internal block, making the
stack-allocated memo introduced in 29c669bb56 invalid.
2026-09-24 19:22:12 +09:00
Peter Zhu 29c669bb56 Use Set instead of Hash for Enumerable#uniq
This commit changes Enumerable#uniq to use a temporary Set instead of Hash
for performance and memory savings. We can see in the following benchmark:

    a = ((0...10_000).to_a + (0...10_000).to_a).each
    1_000.times { a.uniq }

Results:

    Benchmark 1: master
      Time (mean ± σ):     222.4 ms ±   3.7 ms    [User: 212.4 ms, System: 7.6 ms]
      Range (min … max):   216.9 ms … 227.6 ms    13 runs

    Benchmark 2: branch
      Time (mean ± σ):     189.7 ms ±   4.0 ms    [User: 177.7 ms, System: 9.2 ms]
      Range (min … max):   182.8 ms … 198.6 ms    15 runs

    Summary
      branch ran
        1.17 ± 0.03 times faster than master
2026-09-13 14:34:33 +09:00
Earlopain 5ec634bb4e [DOC] nodoc various classes under Enumerator
They are implementation details:
* https://docs.ruby-lang.org/en/4.0/Enumerator/Yielder.html
* https://docs.ruby-lang.org/en/4.0/Enumerator/Producer.html
* https://docs.ruby-lang.org/en/4.0/Enumerator/Generator.html
2026-05-19 23:08:23 +02:00
John Hawthorn 096c9bb9e0 Use flag to determine when MEMO.u3 is a VALUE
We always know when this contains a VALUE and even fire a write barrier
for it. We should use this when marking, support compaction, and no
longer need conservative marking.
2026-04-30 11:20:26 -07:00
Peter Zhu 56147001ec Move MEMO_NEW to imemo.c and rename to rb_imemo_memo_new 2025-12-29 09:03:31 -05:00
Brandon Zylstra a7f948c32c [DOC] Fix a typo in enum.c documentation
fix a minor misspelling in the documentation comments
2025-11-23 11:31:55 +09:00
John Hawthorn a7dc515c1d Fix too early writebarrier in tally_up
After returning from the callback in st_update is the point that the
hash table may be resized, which could trigger a GC and mark the table
being used for the tally.

    RUBY_GC_LIBRARY=wbcheck WBCHECK_VERIFY_AFTER_WB=1 ./miniruby -e '(0...100).map(&:to_s).tally'
2025-06-17 15:32:53 -07:00
Tanaka Akira fa17cb75a1 [doc] update Enumerable document.
- Enhance document about #size method for Enumerable and Enumerator
- add an Enumerator example for Enumerable#drop_while
2025-04-22 01:49:41 +09:00
Nobuyoshi Nakada c218862d3c Fix style [ci skip] 2025-04-19 22:02:10 +09:00
Tanaka Akira ad69f4ddff [doc] update {Enumerator,Enumerable}#size document.
at Code Party RubyKaigi 2025.
2025-04-18 21:10:18 +09:00
NARUSE, Yui bbf873521a Add description about edge cases of Enumerable#size
[Bug #21152]
2025-04-16 16:55:38 +09:00
Vincent Ollivier f719b889a1 [DOC] Fix grep_v description 2025-01-31 15:41:56 +09:00
Nobuyoshi Nakada b176d4f52e [Bug #21008] Normalize before sum to float
After switching to `Float`-mode when summing `Numeric` objects,
normalization for `Float` is still needed.
2025-01-07 11:38:51 +09:00
Nobuyoshi Nakada e433e6515e [DOC] Exclude 'Class' and 'Module' from RDoc's autolinking 2025-01-02 12:36:06 +09:00
Nobuyoshi Nakada b4ec22fe6c [DOC] Exclude 'Method' from RDoc's autolinking 2025-01-02 12:23:49 +09:00
Alan Wu 11e7ab79de Remove 1 allocation in Enumerable#each_with_index (#11868)
* Remove 1 allocation in Enumerable#each_with_index

Previously, each call to Enumerable#each_with_index allocates 2
objects, one for the counting index, the other an imemo_ifunc passed
to `self.each` as a block.

Use `struct vm_ifunc::data` to hold the counting index directly to
remove 1 allocation.

* [DOC] Brief summary for usages of `struct vm_ifunc`
2024-10-11 10:22:44 -04:00
Alan Wu 5f3e9f185d [DOC] Mention that Enumerable#each_with_index calls self.each 2024-10-10 12:32:47 -04:00
Peter Zhu f56be4286f [DOC] Add pound sign before all <=> methods 2024-10-03 09:28:13 -04:00
BurdetteLamar f1a7966187 [DOC] Rationalize aliases in What's Here 2024-08-30 22:26:50 -04:00
BurdetteLamar 76ccd1df37 [DOC] Tweaks for Enum#tally 2024-08-21 15:56:59 -04:00
Yusuke EndohandAaron Patterson e1238a1fab Enumerable#all?: Stop optimizing when a given block is not optimizable
This is a follow up to 182822683f.

Co-Authored-By: Aaron Patterson <tenderlove@ruby-lang.org>
2024-07-11 12:28:23 +09:00
Yusuke Endoh 182822683f Use rb_block_call2 for some Enumerable methods
Enumerable#all?, #any?, #one?, and #none? do not use yielded arguments
as an Array. So they can use rb_block_call2 to omit array allocatoin.

Enumerable#find does not have to immediately accept yielded arguments as
an Array. It can delay array allocation until the predicate block
returns truthy.

(TODO: Enumerable#count and #find_all seem to be optimizable as well.)
2024-07-10 13:00:47 +09:00
a5cb8c8c5b [DOC] rewrite inject doc (#10009)
Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
Co-authored-by: Dennis Dashkevich <dskecse@gmail.com>
2024-05-06 13:51:44 +09:00
Peter Zhu 436191abe0 Replace assert with RUBY_ASSERT in enum.c
assert does not print the bug report, only the file and line number of
the assertion that failed. RUBY_ASSERT prints the full bug report, which
makes it much easier to debug.
2024-02-12 15:07:47 -05:00
Nobuyoshi Nakada 50520cc193 [DOC] Missing comment markers 2023-09-27 16:18:05 +09:00
Burdette Lamar 8c5b9ebf71 [DOC] Improve doc guide compliance (#8221) 2023-08-15 14:43:58 -04:00
Joshua Young 4bdb61b665 [DOC] Fix missing word in a comment 2023-07-30 01:02:20 +09:00
Nobuyoshi Nakada a5e1d549b5 [DOC] Mention the edge case of any?/all? 2023-06-01 21:44:53 +09:00
Burdette Lamar d62ae18797 [DOC] Link fix (#7862) 2023-05-26 09:20:18 -04:00
nekoyama32767 87217f26f1 [Feature #19643] Direct primitive compare sort for Array#sort_by
In most of case `sort_by` works on primitive type.
Using `qsort_r` with function pointer is much slower than compare data directly.

I implement an intro sort which compare primitive data directly for `sort_by`.
We can even afford an O(n) type check before primitive data sort.
It still go faster.
2023-05-20 19:40:27 +09:00
BurdetteLamar 3b239d2480 Remove (newly unneeded) remarks about aliases 2023-02-19 14:26:34 -08:00
zverok 976cc3852b [DOC] Return *args to Enumerable method definitions 2023-02-19 22:32:52 +02:00
Daniel ColsonandJohn Hawthorn e69b91fae4 Introduce BOP_CMP for optimized comparison
Prior to this commit the `OPTIMIZED_CMP` macro relied on a method lookup
to determine whether `<=>` was overridden. The result of the lookup was
cached, but only for the duration of the specific method that
initialized the cmp_opt_data cache structure.

With this method lookup, `[x,y].max` is slower than doing `x > y ?
x : y` even though there's an optimized instruction for "new array max".
(John noticed somebody a proposed micro-optimization based on this fact
in https://github.com/mastodon/mastodon/pull/19903.)

```rb
a, b = 1, 2
Benchmark.ips do |bm|
  bm.report('conditional') { a > b ? a : b }
  bm.report('method') { [a, b].max }
  bm.compare!
end
```

Before:

```
Comparison:
         conditional: 22603733.2 i/s
              method: 19820412.7 i/s - 1.14x  (± 0.00) slower
```

This commit replaces the method lookup with a new CMP basic op, which
gives the examples above equivalent performance.

After:

```
Comparison:
              method: 24022466.5 i/s
         conditional: 23851094.2 i/s - same-ish: difference falls within
error
```

Relevant benchmarks show an improvement to Array#max and Array#min when
not using the optimized newarray_max instruction as well. They are
noticeably faster for small arrays with the relevant types, and the same
or maybe a touch faster on larger arrays.

```
$ make benchmark COMPARE_RUBY=<master@5958c305> ITEM=array_min
$ make benchmark COMPARE_RUBY=<master@5958c305> ITEM=array_max
```

The benchmarks added in this commit also look generally improved.

Co-authored-by: John Hawthorn <jhawthorn@github.com>
2022-12-06 12:37:23 -08:00
S-H-GAMELINKS 1f4f6c9832 Using UNDEF_P macro 2022-11-16 18:58:33 +09:00
Kouhei Yanagita f982a26374 [DOC] Fix typo in Enumerable#slice_before 2022-10-19 09:14:50 +09:00
Peter Zhu a24c607e30 [DOC] Fix formatting issue in Enumerable 2022-08-08 09:26:07 -04:00
Peter Zhu efb91ff19b Rename rb_ary_tmp_new to rb_ary_hidden_new
rb_ary_tmp_new suggests that the array is temporary in some way, but
that's not true, it just creates an array that's hidden and not on the
transient heap. This commit renames it to rb_ary_hidden_new.
2022-07-26 09:12:09 -04:00
Takashi Kokubun 5b21e94beb Expand tabs [ci skip]
[Misc #18891]
2022-07-21 09:42:04 -07:00
Burdette Lamar 8038d5e40a Revert flawed doc for slice_after, slice_when, and chunk_while (#5952)
Restores doc for the methods that were cited in https://bugs.ruby-lang.org/issues/18765.
2022-05-28 14:20:00 -05:00
Colin Hart 9e8841e592 Simplify example code for Enumerable#each_with_object 2022-04-25 12:32:29 -07:00
Burdette Lamar f918f6e4e7 [DOC] Repair format and links in What's Here sections (#5711)
* Repair format and links in What's Here for Comparable and Array

* Repair format for What's Here in enum.c
2022-03-25 10:52:06 -05:00
Jeremy Evans 8f1c69f27c Raise ArgumentError when calling Enumberable#inject without block or arguments
Previously, this would work as expected if the enumerable contained
0 or 1 element, and would raise LocalJumpError otherwise. That
inconsistent behavior is likely to lead to bugs.

Fixes [Bug #18635]
2022-03-23 07:55:49 -07:00