[#119000] [Ruby master Bug#20710] Reducing Hash allocation introduces large performance degradation (probably related to VWA) — "pocke (Masataka Kuwabara) via ruby-core" <ruby-core@...>

Issue #20710 has been reported by pocke (Masataka Kuwabara).

6 messages 2024/09/02

[#119033] [Ruby master Bug#20713] Ruby 3.3.5 triggers a deprecation warning with `require "json"` — "Bo98 (Bo Anderson) via ruby-core" <ruby-core@...>

Issue #20713 has been reported by Bo98 (Bo Anderson).

7 messages 2024/09/04

[#119041] [Ruby master Bug#20714] Handle optional dependencies in `bundled_gems.rb` — "Earlopain (A S) via ruby-core" <ruby-core@...>

Issue #20714 has been reported by Earlopain (A S).

31 messages 2024/09/04

[#119074] [Ruby master Bug#20716] Different instance_method behavior in Ruby 2.7 and Ruby 3.x — "natton (Tien Truong) via ruby-core" <ruby-core@...>

Issue #20716 has been reported by natton (Tien Truong).

13 messages 2024/09/06

[#119145] [Ruby master Misc#20728] Propose Eileen Uchitelle as a core committer — "kddnewton (Kevin Newton) via ruby-core" <ruby-core@...>

Issue #20728 has been reported by kddnewton (Kevin Newton).

14 messages 2024/09/12

[#119168] [Ruby master Feature#20738] Removing a specific entry from a hash literal — "ursm (Keita Urashima) via ruby-core" <ruby-core@...>

Issue #20738 has been reported by ursm (Keita Urashima).

16 messages 2024/09/13

[#119199] [Ruby master Bug#20742] Trying to assign to a variable in statement modifier should emit a warning — "esad (Esad Hajdarevic) via ruby-core" <ruby-core@...>

SXNzdWUgIzIwNzQyIGhhcyBiZWVuIHJlcG9ydGVkIGJ5IGVzYWQgKEVzYWQgSGFqZGFyZXZpYyku

7 messages 2024/09/15

[#119208] [Ruby master Bug#20745] IO::Buffer#copy triggers UB when src/dest buffers overlap — "hanazuki (Kasumi Hanazuki) via ruby-core" <ruby-core@...>

Issue #20745 has been reported by hanazuki (Kasumi Hanazuki).

8 messages 2024/09/16

[#119239] [Ruby master Feature#20750] Expose ruby_thread_has_gvl_p in ruby/thread.h — "kbrock (Keenan Brock) via ruby-core" <ruby-core@...>

Issue #20750 has been reported by kbrock (Keenan Brock).

8 messages 2024/09/17

[#119248] [Ruby master Bug#20752] IO::Buffer#slice fails to copy readonly flag, allowing writes into frozen String — "hanazuki (Kasumi Hanazuki) via ruby-core" <ruby-core@...>

Issue #20752 has been reported by hanazuki (Kasumi Hanazuki).

7 messages 2024/09/18

[#119301] [Ruby master Bug#20761] [DOC] `RubyVM::AbstractSyntaxTree.of` examples raise because parser is prism by default — "Earlopain (A S) via ruby-core" <ruby-core@...>

Issue #20761 has been reported by Earlopain (A S).

11 messages 2024/09/26

[#119335] [Ruby master Bug#20770] A *new* pipe operator proposal — "AlexandreMagro (Alexandre Magro) via ruby-core" <ruby-core@...>

Issue #20770 has been reported by AlexandreMagro (Alexandre Magro).

56 messages 2024/09/29

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

From: "byroot (Jean Boussier) via ruby-core" <ruby-core@...>
Date: 2024-09-05 06:08:20 UTC
List: ruby-core #119055
Issue #20684 has been updated by byroot (Jean Boussier).


Makes sense, thank you Matz!

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

* 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: https://github.com/ruby/ruby/pull/11406



--=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