[#118784] [Ruby master Feature#20664] Add `before` and `until` options to Enumerator.produce — "knu (Akinori MUSHA) via ruby-core" <ruby-core@...>

Issue #20664 has been reported by knu (Akinori MUSHA).

12 messages 2024/08/03

[#118791] [Ruby master Bug#20666] Segmentation fault instead of LoadError exception — "ErezGeva2@... (Erez Geva) via ruby-core" <ruby-core@...>

Issue #20666 has been reported by ErezGeva2@gmail.com (Erez Geva).

9 messages 2024/08/04

[#118811] [Ruby master Feature#20669] Add error classes to differentiate Marshal ArgumentErrors — "olleolleolle (Olle Jonsson) via ruby-core" <ruby-core@...>

Issue #20669 has been reported by olleolleolle (Olle Jonsson).

7 messages 2024/08/08

[#118844] [Ruby master Feature#20676] Pathnames aren't Comparable — "gmcgibbon (Gannon McGibbon) via ruby-core" <ruby-core@...>

SXNzdWUgIzIwNjc2IGhhcyBiZWVuIHJlcG9ydGVkIGJ5IGdtY2dpYmJvbiAoR2Fubm9uIE1jR2li

8 messages 2024/08/13

[#118879] [Ruby master Bug#20682] Slave PTY output is lost after a child process exits in macOS — "ono-max (Naoto Ono) via ruby-core" <ruby-core@...>

Issue #20682 has been reported by ono-max (Naoto Ono).

9 messages 2024/08/19

[#118932] [Ruby master Bug#20693] Dir.tmpdir should perform a real access check before warning about writability — "kjtsanaktsidis (KJ Tsanaktsidis) via ruby-core" <ruby-core@...>

Issue #20693 has been reported by kjtsanaktsidis (KJ Tsanaktsidis).

9 messages 2024/08/22

[#118979] [Ruby master Feature#20705] Should "0.E-9" be a valid float value? — "kou (Kouhei Sutou) via ruby-core" <ruby-core@...>

Issue #20705 has been reported by kou (Kouhei Sutou).

11 messages 2024/08/29

[#118983] [Ruby master Bug#20706] Can't build Ruby on macOS Sonoma and Sequoia due to: ignoring duplicate libraries, archive member '/' not a mach-o file in libruby.3.3-static.a — "wkoszek (Adam Koszek) via ruby-core" <ruby-core@...>

Issue #20706 has been reported by wkoszek (Adam Koszek).

7 messages 2024/08/29

[ruby-core:118891] [Ruby master Feature#20684] Add optimized instructions for frozen literal Hash and Array

From: etienne via ruby-core <ruby-core@...>
Date: 2024-08-19 09:20:24 UTC
List: ruby-core #118891
Issue #20684 has been reported by etienne (=C9tienne Barri=E9).

----------------------------------------
Feature #20684: Add optimized instructions for frozen literal Hash and Array
https://bugs.ruby-lang.org/issues/20684

* Author: etienne (=C9tienne Barri=E9)
* Status: Open
----------------------------------------
# Context

Methods that take empty arrays or empty hashes as default values allocate a=
 new object each time the method is called without the argument. Often they=
 don't mutate the parameter. To prevent an allocation, in performance criti=
cal sections, a constant is defined that holds a frozen hash or array, and =
the constant is defined as the default value for the parameter.

Here are some examples:
Rails: https://github.com/rails/rails/blob/607d61e884237c223c24c6f47efa0b56=
1dd8b637/activerecord/lib/active_record/relation/query_methods.rb#L159-L160
Roda: https://github.com/jeremyevans/roda/blob/102926a02dcabc9a31674e3cf98f=
049139c31492/lib/roda/plugins.rb#L9-L10
dry-rb: https://github.com/dry-rb/dry-container/blob/1ee41bb109455d06bf22eb=
cbd94b050cc4773733/lib/dry/container/mixin.rb#L68C5-L68C15
and many other gems: https://gist.github.com/casperisfine/47f22243d4ad20385=
5256ef5bfae7979

Additionally when defining a frozen literal constant, we're currently ineff=
icient because we store the literal in the bytecode, we dup it just to free=
ze it again. It doesn't amount to much but would be nice to avoid.

# Proposal

Introduce 2 new optimized instructions `opt_ary_freeze` and `opt_hash_freez=
e` that behave like `opt_str_freeze` for their respective types. If the fre=
eze method hasn't been redefined, they simply push the frozen literal value=
 on the stack. Like for `opt_str_freeze`, these instructions are added by t=
he peephole optimizer when applicable.

In the specific case of empty array and empty hash, we use a pre-allocated =
global empty frozen object to avoid retaining a distinct empty object each =
time.

This will allow code like this: https://github.com/ruby/ruby/blob/566f2eb50=
1d94d4047a9aad4af0d74c6a96f34a9/lib/rubygems/resolver/api_set/gem_parser.rb=
 to be shortened and simplified like this:

```diff
diff --git i/lib/rubygems/resolver/api_set/gem_parser.rb w/lib/rubygems/res=
olver/api_set/gem_parser.rb
index 643b857107..34146fd426 100644
--- i/lib/rubygems/resolver/api_set/gem_parser.rb
+++ w/lib/rubygems/resolver/api_set/gem_parser.rb
@@ -1,15 +1,12 @@
 # frozen_string_literal: true
=20
 class Gem::Resolver::APISet::GemParser
-  EMPTY_ARRAY =3D [].freeze
-  private_constant :EMPTY_ARRAY
-
   def parse(line)
     version_and_platform, rest =3D line.split(" ", 2)
     version, platform =3D version_and_platform.split("-", 2)
     dependencies, requirements =3D rest.split("|", 2).map! {|s| s.split(",=
") } if rest
-    dependencies =3D dependencies ? dependencies.map! {|d| parse_dependenc=
y(d) } : EMPTY_ARRAY
-    requirements =3D requirements ? requirements.map! {|d| parse_dependenc=
y(d) } : EMPTY_ARRAY
+    dependencies =3D dependencies ? dependencies.map! {|d| parse_dependenc=
y(d) } : [].freeze
+    requirements =3D requirements ? requirements.map! {|d| parse_dependenc=
y(d) } : [].freeze
     [version, platform, dependencies, requirements]
   end
```

Overall it's a minor optimization but also a very simple patch and makes co=
de nicer.

PR pending.



--=20
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.rub=
y-lang.org/


In This Thread

Prev Next