* Added `Ractor::Port`
* `Ractor::Port#receive` (support multi-threads)
* `Rcator::Port#close`
* `Ractor::Port#closed?`
* Added some methods
* `Ractor#join`
* `Ractor#value`
* `Ractor#monitor`
* `Ractor#unmonitor`
* Removed some methods
* `Ractor#take`
* `Ractor.yield`
* Change the spec
* `Racotr.select`
You can wait for multiple sequences of messages with `Ractor::Port`.
```ruby
ports = 3.times.map{ Ractor::Port.new }
ports.map.with_index do |port, ri|
Ractor.new port,ri do |port, ri|
3.times{|i| port << "r#{ri}-#{i}"}
end
end
p ports.each{|port| pp 3.times.map{port.receive}}
```
In this example, we use 3 ports, and 3 Ractors send messages to them respectively.
We can receive a series of messages from each port.
You can use `Ractor#value` to get the last value of a Ractor's block:
```ruby
result = Ractor.new do
heavy_task()
end.value
```
You can wait for the termination of a Ractor with `Ractor#join` like this:
```ruby
Ractor.new do
some_task()
end.join
```
`#value` and `#join` are similar to `Thread#value` and `Thread#join`.
To implement `#join`, `Ractor#monitor` (and `Ractor#unmonitor`) is introduced.
This commit changes `Ractor.select()` method.
It now only accepts ports or Ractors, and returns when a port receives a message or a Ractor terminates.
We removes `Ractor.yield` and `Ractor#take` because:
* `Ractor::Port` supports most of similar use cases in a simpler manner.
* Removing them significantly simplifies the code.
We also change the internal thread scheduler code (thread_pthread.c):
* During barrier synchronization, we keep the `ractor_sched` lock to avoid deadlocks.
This lock is released by `rb_ractor_sched_barrier_end()`
which is called at the end of operations that require the barrier.
* fix potential deadlock issues by checking interrupts just before setting UBF.
https://bugs.ruby-lang.org/issues/21262
Followup: https://github.com/ruby/strscan/pull/115
`scan_integer` is now implemented in Ruby as to efficiently handle
keyword arguments without allocating a Hash. Given the goal of
`scan_integer` is to more effciently parse integers without having to
allocate an intermediary object, using `rb_scan_args` would defeat the
purpose.
Additionally, the C implementation now uses `rb_isdigit` and
`rb_isxdigit`, because on Windows `isdigit` is locale dependent.
test
(https://github.com/ruby/strscan/pull/118)
20241128T153002Z.log.html.gz
```
/home/chkbuild/chkbuild/tmp/build/20241128T153002Z/ruby/test/strscan/test_stringscanner.rb:908: warning: ambiguous first argument; put parentheses or a space even after `-` operator
```
af3fd2f045
(restLen() < patternsize()) return context.nil;` checks in
`!headonly`.
(https://github.com/ruby/strscan/pull/110)
- before: #109
## Why?
d31274f41b/ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java (L371-L373)
This means the following :
`if (str.size() - curr < pattern.size()) return context.nil;`
A similar check is made within `StringSupport#index()` within
`!headonly`.
be7815ec02/core/src/main/java/org/jruby/util/StringSupport.java (L1706-L1720)
```Java
public static int index(ByteList source, ByteList other, int offset, Encoding enc) {
int sourceLen = source.realSize();
int sourceBegin = source.begin();
int otherLen = other.realSize();
if (otherLen == 0) return offset;
if (sourceLen - offset < otherLen) return -1;
```
- source = `strBL`
- other = `patternBL`
- offset = `strBeg + curr`
This means the following :
`if (strBL.realSize() - (strBeg + curr) < patternBL.realSize()) return
-1;`
Both checks are the same.
## Benchmark
It shows String as a pattern is 2.40x faster than Regexp as a pattern.
```
$ benchmark-driver benchmark/check_until.yaml
Warming up --------------------------------------
regexp 7.613M i/s - 7.593M times in 0.997350s (131.35ns/i)
regexp_var 7.793M i/s - 7.772M times in 0.997364s (128.32ns/i)
string 13.222M i/s - 13.199M times in 0.998297s (75.63ns/i)
string_var 15.283M i/s - 15.216M times in 0.995667s (65.43ns/i)
Calculating -------------------------------------
regexp 10.003M i/s - 22.840M times in 2.283361s (99.97ns/i)
regexp_var 9.991M i/s - 23.378M times in 2.340019s (100.09ns/i)
string 23.454M i/s - 39.666M times in 1.691221s (42.64ns/i)
string_var 23.998M i/s - 45.848M times in 1.910447s (41.67ns/i)
Comparison:
string_var: 23998466.3 i/s
string: 23453777.5 i/s - 1.02x slower
regexp: 10002809.4 i/s - 2.40x slower
regexp_var: 9990580.1 i/s - 2.40x slower
```
843e931d13
(https://github.com/ruby/strscan/pull/106)
It supports non-head match cases such as StringScanner#scan_until.
If we use a String as a pattern, we can improve match performance.
Here is a result of the including benchmark.
## CRuby
It shows String as a pattern is 1.18x faster than Regexp as a pattern.
```
$ benchmark-driver benchmark/check_until.yaml
Warming up --------------------------------------
regexp 9.403M i/s - 9.548M times in 1.015459s (106.35ns/i)
regexp_var 9.162M i/s - 9.248M times in 1.009479s (109.15ns/i)
string 8.966M i/s - 9.274M times in 1.034343s (111.54ns/i)
string_var 11.051M i/s - 11.190M times in 1.012538s (90.49ns/i)
Calculating -------------------------------------
regexp 10.319M i/s - 28.209M times in 2.733707s (96.91ns/i)
regexp_var 10.032M i/s - 27.485M times in 2.739807s (99.68ns/i)
string 9.681M i/s - 26.897M times in 2.778397s (103.30ns/i)
string_var 12.162M i/s - 33.154M times in 2.726046s (82.22ns/i)
Comparison:
string_var: 12161920.6 i/s
regexp: 10318949.7 i/s - 1.18x slower
regexp_var: 10031617.6 i/s - 1.21x slower
string: 9680843.7 i/s - 1.26x slower
```
## JRuby
It shows String as a pattern is 2.11x faster than Regexp as a pattern.
```
$ benchmark-driver benchmark/check_until.yaml
Warming up --------------------------------------
regexp 7.591M i/s - 7.544M times in 0.993780s (131.74ns/i)
regexp_var 6.143M i/s - 6.125M times in 0.997038s (162.77ns/i)
string 14.135M i/s - 14.079M times in 0.996067s (70.75ns/i)
string_var 14.079M i/s - 14.057M times in 0.998420s (71.03ns/i)
Calculating -------------------------------------
regexp 9.409M i/s - 22.773M times in 2.420268s (106.28ns/i)
regexp_var 10.116M i/s - 18.430M times in 1.821820s (98.85ns/i)
string 21.389M i/s - 42.404M times in 1.982519s (46.75ns/i)
string_var 20.897M i/s - 42.237M times in 2.021187s (47.85ns/i)
Comparison:
string: 21389191.1 i/s
string_var: 20897327.5 i/s - 1.02x slower
regexp_var: 10116464.7 i/s - 2.11x slower
regexp: 9409222.3 i/s - 2.27x slower
```
See:
be7815ec02/core/src/main/java/org/jruby/util/StringSupport.java (L1706-L1736)
---------
f9d96c446a
Co-authored-by: Sutou Kouhei <kou@clear-code.com>
integers
(https://github.com/ruby/strscan/pull/89)
This commit adds `scan_byte` and `peek_byte`. `scan_byte` will scan the
current byte, return it as an integer, and advance the cursor.
`peek_byte` will return the current byte as an integer without advancing
the cursor.
Currently `StringScanner#get_byte` returns a string, but I want to get
the current byte without allocating a string. I think this will help
with writing high performance lexers.
---------
873aba2e5d
Co-authored-by: Sutou Kouhei <kou@clear-code.com>
(https://github.com/ruby/strscan/pull/58)
`string` returns the original string after `scan` is called. Current
test doesn't check this behavior and now it's covered.
This removes the related tests, and puts the related specs behind
version guards. This affects all code in lib, including some
libraries that may want to support older versions of Ruby.
* ext/strscan/strscan.c: added `size`, `captures` and `values_at`
to StringScanner, shorthands of accessing the matched data.
based on the patch by apeiros (Stefan Rusterholz) at
[ruby-core:20412]. [Feature #836]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60929 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
* ext/strscan/strscan.c (strscan_aref): fix segfault after
get_byte or getch which do not apply regexp.
[ruby-core:82116] [Bug #13759]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59384 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
When you change this to true, you may need to add more tests.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53141 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
patched by Konstantin Haase [ruby-core:54664] [Feature #8343]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40881 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
dupped or frozen, because freezing it causes #concat method failure,
and unnecessary to dup without freezing. a patch from Aaron
Patterson at [ruby-core:25145].
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24679 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
external local variable in the block parameters. [ruby-dev:32251]
* test/strscan/test_stringscanner.rb: avoid $KCODE, and use
String#force_encoding(). [ruby-dev:32251]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13922 b2dd03c8-39d4-4d8f-98ff-823fe69b080e