ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "Dan0042 (Daniel DeLorme) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "Dan0042 (Daniel DeLorme)" <ruby-core@ml.ruby-lang.org>
Subject: [ruby-core:113434] [Ruby master Feature#19236] Allow to create hashes with a specific capacity from Ruby
Date: Tue, 09 May 2023 12:57:34 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-103007.20230509125734.7941@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-19236.20221215090219.7941@ruby-lang.org

Issue #19236 has been updated by Dan0042 (Daniel DeLorme).


ianks (Ian Ker-Seymer) wrote in #note-8:
> To me `Hash.with_capacity` clearly communicates what’s happening. Anyone can understand it at first glance.

`Hash.with_capacity` is not composable. What should you do if you want a default value/proc AND a capacity?

```ruby
h = Hash.with_capacity(100)
h.default = default_value  #this?? a bit ugly imho
```

`Hash#with_capacity` would be better, then you could do `Hash.new(default_value).with_capacity(400)` similar to `compare_by_identity` usage.
But at that point it's imho better to have `Hash.new(default_value).tap{ _1.capacity = 400 }`
Or the best: `Hash.new(default_value).tap{ .capacity = 400 }`    ;-)




----------------------------------------
Feature #19236: Allow to create hashes with a specific capacity from Ruby
https://bugs.ruby-lang.org/issues/19236#change-103007

* Author: byroot (Jean Boussier)
* Status: Open
* Priority: Normal
* Target version: 3.3
----------------------------------------
Followup on [Feature #18683] which added a C-API for this purpose.

Various protocol parsers such as Redis `RESP3` or `msgpack`, have to create hashes, and they know the size in advance.
For efficiency, it would be preferable if they could directly allocate a Hash of the necessary size, so that large hashes wouldn't cause many re-alloccations and re-hash.

`String` and `Array` both already offer similar APIs:

```ruby
String.new(capacity: XXX)
Array.new(XX) / rb_ary_new_capa(long)
```

However there's no such public API for Hashes in Ruby land.

### Proposal

I think `Hash` should have a way to create a new hash with a `capacity` parameter.


The logical signature of `Hash.new(capacity: 1000)` was deemed too incompatible in [Feature #18683].


@Eregon proposed to add `Hash.create(capacity: 1000)`.





-- 
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/postorius/lists/ruby-core.ml.ruby-lang.org/

  parent reply	other threads:[~2023-05-09 12:57 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-15  9:02 [ruby-core:111306] [Ruby master Feature#19236] Allow to create hashes with a specific capacity from Ruby byroot (Jean Boussier)
2022-12-25 17:16 ` [ruby-core:111425] " janosch-x via ruby-core
2023-01-20  5:31 ` [ruby-core:111919] " mame (Yusuke Endoh) via ruby-core
2023-01-20  8:42 ` [ruby-core:111931] " byroot (Jean Boussier) via ruby-core
2023-01-23  2:47 ` [ruby-core:111983] " mame (Yusuke Endoh) via ruby-core
2023-01-24 14:23 ` [ruby-core:112008] " Eregon (Benoit Daloze) via ruby-core
2023-05-05 23:59 ` [ruby-core:113408] " Dan0042 (Daniel DeLorme) via ruby-core
2023-05-06  5:31 ` [ruby-core:113412] " ianks (Ian Ker-Seymer) via ruby-core
2023-05-06  5:39 ` [ruby-core:113413] " byroot (Jean Boussier) via ruby-core
2023-05-09 12:57 ` Dan0042 (Daniel DeLorme) via ruby-core [this message]
2023-05-19 10:04 ` [ruby-core:113528] " byroot (Jean Boussier) via ruby-core
2023-05-23 10:44 ` [ruby-core:113598] " byroot (Jean Boussier) via ruby-core
2023-05-23 13:53 ` [ruby-core:113600] " byroot (Jean Boussier) via ruby-core
2024-03-26  7:20 ` [ruby-core:117322] " byroot (Jean Boussier) via ruby-core

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-103007.20230509125734.7941@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    --cc=ruby-core@ml.ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).