Thread overview
[Issue 13159] std.socket.getAddress allocates once per DNS lookup hit
Jul 19, 2014
Orvid King
Jul 19, 2014
Jakob Ovrum
Aug 29, 2015
Vladimir Panteleev
Aug 29, 2015
Vladimir Panteleev
Aug 29, 2015
Vladimir Panteleev
Aug 29, 2015
Jakob Ovrum
Dec 17, 2022
Iain Buclaw
July 19, 2014
https://issues.dlang.org/show_bug.cgi?id=13159

Orvid King <blah38621@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |blah38621@gmail.com

--- Comment #1 from Orvid King <blah38621@gmail.com> ---
Assuming that infos has a length property, it would be better yet to simply do:

auto infos = getAddressInfo(hostname, service);
auto results = new Address[infos.length];

Which would result in exactly the amount of memory needed being allocated, and only 1 allocation being done. I would also suggest the possibility of adding an OutputRange based version.

--
July 19, 2014
https://issues.dlang.org/show_bug.cgi?id=13159

Jakob Ovrum <jakobovrum@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakobovrum@gmail.com

--- Comment #2 from Jakob Ovrum <jakobovrum@gmail.com> ---
I already posted a PR for this BTW:

https://github.com/D-Programming-Language/phobos/pull/2351

(In reply to Orvid King from comment #1)
> Assuming that infos has a length property, it would be better yet to simply do:
> 
> auto infos = getAddressInfo(hostname, service);
> auto results = new Address[infos.length];

This would allocate even when infos.length is 0.

> Which would result in exactly the amount of memory needed being allocated, and only 1 allocation being done.

`getAddressInfo` allocates too, and easily more than once, so that wouldn't be quite true.

> I would also suggest the possibility of adding an OutputRange based version.

We can do better - a version that returns the results as a lazy forward range. The most underlying data structure here is a singly linked list. We can use reference counting to ensure the list is freed (freeaddrinfo). It's a much heavier change though so I'm not going to do it in PR #2351.

--
July 21, 2014
https://issues.dlang.org/show_bug.cgi?id=13159

--- Comment #3 from github-bugzilla@puremagic.com ---
Commit pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/96893cbf467524d05d9f8b0f51398585ca77a423 Pre-allocate result array in getAddress and use Appender in getAddressInfoImpl

Fixes issue #13159

--
July 21, 2014
https://issues.dlang.org/show_bug.cgi?id=13159

github-bugzilla@puremagic.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--
August 29, 2015
https://issues.dlang.org/show_bug.cgi?id=13159

Vladimir Panteleev <thecybershadow@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|FIXED                       |---

--
August 29, 2015
https://issues.dlang.org/show_bug.cgi?id=13159

Vladimir Panteleev <thecybershadow@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |thecybershadow@gmail.com

--- Comment #4 from Vladimir Panteleev <thecybershadow@gmail.com> ---
???

A concatenation is not an allocation! The runtime will effectively increase arrays when they are appended to by powers of two until the GC block size (4096 bytes), because GC object bins have sizes of powers of two (16 to 2048).

Furthermore, didn't recent benchmarks show that std.array.appender performed no better than built-in arrays for concatenation?

--
August 29, 2015
https://issues.dlang.org/show_bug.cgi?id=13159

--- Comment #5 from Vladimir Panteleev <thecybershadow@gmail.com> ---
The getAddress patch is fine. The getAddressInfo patch seems pointless to me, it does not preallocate any memory (but could be made to if the linked list is traversed twice).

--
August 29, 2015
https://issues.dlang.org/show_bug.cgi?id=13159

--- Comment #6 from Jakob Ovrum <jakobovrum@gmail.com> ---
(In reply to Vladimir Panteleev from comment #5)
> The getAddress patch is fine. The getAddressInfo patch seems pointless to me, it does not preallocate any memory (but could be made to if the linked list is traversed twice).

Appender has gone through a lot of revision in recent years. Array appends might be better today.

The difference is probably negligible; the getAddress patch was the main point. The ideal would be a lazy range version of getAddressInfo. With both `getAddress` and `getAddressInfo` taken, bikeshedding ahoy :)

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=13159

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P4

--