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

Decrease template bloat for string functions #6743

Merged
merged 1 commit into from
Nov 19, 2018

Conversation

n8sh
Copy link
Member

@n8sh n8sh commented Oct 31, 2018

Use const(E)[] instead of E[] when it is valid and doesn't change the return type.

std/range/primitives.d Outdated Show resolved Hide resolved
@dlang-bot
Copy link
Contributor

dlang-bot commented Oct 31, 2018

Thanks for your pull request, @n8sh!

Bugzilla references

Auto-close Bugzilla Severity Description
19364 enhancement Decrease template bloat for string functions

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + phobos#6743"

@n8sh n8sh force-pushed the string-template-antibloat branch 4 times, most recently from f60c1a1 to a80f54f Compare October 31, 2018 08:05
@n8sh n8sh changed the title Decrease template bloat for narrow string templates Decrease template bloat for string template functions Oct 31, 2018
@n8sh n8sh force-pushed the string-template-antibloat branch 2 times, most recently from 87f692e to 8033890 Compare October 31, 2018 08:12
@n8sh n8sh changed the title Decrease template bloat for string template functions Decrease template bloat for string functions Oct 31, 2018
@n8sh n8sh force-pushed the string-template-antibloat branch 3 times, most recently from e1a4c91 to f4b5c98 Compare October 31, 2018 09:48
@burner
Copy link
Member

burner commented Oct 31, 2018

I assumed you grep'ed for the usage of decodeImpl to make sure CastForDecode is used when needed?

@n8sh
Copy link
Member Author

n8sh commented Oct 31, 2018

In effect yes. The only places it's not used are in unittests.

@burner
Copy link
Member

burner commented Oct 31, 2018

Wouldn't it be better if decodeImpl would call CastForDecode, because currently we have to make sure that people remember calling CastForDecode if necessary.

@n8sh
Copy link
Member Author

n8sh commented Oct 31, 2018

Can you suggest a good way to do that? I'm not enthusiastic about adding a wrapper function.

@burner
Copy link
Member

burner commented Oct 31, 2018

sorry, strike my last comment. I misread the call side.

Copy link
Member

@jmdavis jmdavis left a comment

Choose a reason for hiding this comment

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

Have you done anything to measure these changes? Given the size and complexity of decodeImpl, it wouldn't surprise me at all if this does reduce template bloat, but you are actually adding template instantiations into the mix into order to avoid template instantiations, and the vast majority of string code is just string. So, for some programs at least, this is probably adding template bloat, though overall, it probably is reducing it. It also has a number of scope-related changes that seem orthogonal to the main point of the PR, which muddies things a bit.

Regardless, unless there's something that I'm missing here, I think that inout should be completely removed from the PR. It's my understanding that it has no impact on how many templates are instantiated, and it really doesn't provide any benefit, because the code is templated. It just further complicates the code.

std/range/primitives.d Show resolved Hide resolved
std/range/primitives.d Show resolved Hide resolved
std/utf.d Outdated Show resolved Hide resolved
std/utf.d Outdated Show resolved Hide resolved
@n8sh
Copy link
Member Author

n8sh commented Nov 5, 2018

@jmdavis The purpose of this PR is not to improve compile times by reducing the absolute number of template uses, it's to improve the cache performance of the final executable by decreasing the number of distinct functions created by template instantiation. Look at the generated assembly for this code:

https://run.dlang.io/is/fMHThw

void foo(T)(ref T[] a)
{
    a = a[1 .. $];
}

void bar(T)(ref inout(T)[] a)
{
    a = a[1 .. $];
}

void main()
{
    int[] a = [1, 2, 3];
    const(int)[] b = [4, 5, 6];
    immutable(int)[] c = [7, 8, 9];
    foo(a);
    foo(b);
    foo(c);
    bar(a);
    bar(b);
    bar(c);
}

The generated code has 3 separate foo functions that are each called once, while there is 1 single bar function that is called 3 times.

@n8sh n8sh force-pushed the string-template-antibloat branch 2 times, most recently from c9caece to 0f20d87 Compare November 18, 2018 10:48
@thewilsonator
Copy link
Contributor

Yep, that went green.

@thewilsonator
Copy link
Contributor

Since style is guaranteed to fail, feel free to abuse auto-merge to get priority to reduce this.

@thewilsonator
Copy link
Contributor

Ooohh, or not. Why is style passing now?

@n8sh n8sh changed the title Decrease template bloat for string functions Decrease template bloat for string functions [do not merge, isolating Win32_64 error] Nov 18, 2018
@thewilsonator
Copy link
Contributor

And now style fails, WTF?

@n8sh n8sh force-pushed the string-template-antibloat branch 3 times, most recently from a95d31f to 77b15d9 Compare November 18, 2018 19:03
Use const(E)[] instead of E[] when it is valid and doesn't change the
return type.
@n8sh n8sh changed the title Decrease template bloat for string functions [do not merge, isolating Win32_64 error] Decrease template bloat for string functions Nov 18, 2018
// if (/* same constraint */)
// ---
// as that would cause fewer distinct functions to be generated with
// IFTI, but that caused a linker error in the test suite on Win32_64.
Copy link
Contributor

Choose a reason for hiding this comment

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

Its great that you found the source of the error, did you submit a bug for it?

@dlang-bot dlang-bot merged commit 8cef7b5 into dlang:master Nov 19, 2018
n8sh added a commit to n8sh/phobos that referenced this pull request Jun 6, 2019
The Win32_64 linker error that prevented this from being included in
PR dlang#6743 no longer occurs in CI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants