Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-allocate result array in getAddress #2351

Merged
merged 1 commit into from
Jul 20, 2014

Conversation

JakobOvrum
Copy link
Member

Fixes issue #13159.

@quickfur
Copy link
Member

I think we should also improve getAddressInfoImpl to reduce allocations as well. Currently it's still doing an allocation per address returned. Perhaps using std.array.appender might be appropriate here?

On a longer term basis, we might want to consider changing the implementation to a lazy one so that we only need to allocate once for storing the final result.

@JakobOvrum
Copy link
Member Author

Changed getAddressInfoImpl to use Appender; would be dishonest to close the issue after changing just getAddress when it uses getAddressInfo internally.

Since getaddrinfo usually only returns a handful of addresses it would probably be more efficient in the vast majority of cases to traverse it first to measure the length, then allocate an array of that size then traverse it again to fill the array, but worst case complexity is probably more important...

On a longer term basis, we might want to consider changing the implementation to a lazy one so that we only need to allocate once for storing the final result.

Yeah, that would be best.

@DmitryOlshansky
Copy link
Member

LGTM

1 similar comment
@burner
Copy link
Member

burner commented Jul 20, 2014

LGTM

@mihails-strasuns
Copy link

Auto-merge toggled on

mihails-strasuns pushed a commit that referenced this pull request Jul 20, 2014
Pre-allocate result array in getAddress
@mihails-strasuns mihails-strasuns merged commit 122f507 into dlang:master Jul 20, 2014
@@ -1012,6 +1012,8 @@ AddressInfo[] getAddressInfo(T...)(in char[] node, T options)

private AddressInfo[] getAddressInfoImpl(in char[] node, in char[] service, addrinfo* hints)
{
import std.array : appender;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation -.-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants