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

use enum instead of immutable for CTFE-only literals #16670

Merged
merged 2 commits into from
Jul 13, 2024
Merged

Conversation

0-v-0
Copy link
Contributor

@0-v-0 0-v-0 commented Jul 7, 2024

No description provided.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @0-v-0! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

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

dub run digger -- build "master + dmd#16670"

@dkorpel
Copy link
Contributor

dkorpel commented Jul 7, 2024

For array literals, immutable is preferred in general because enums can turn into GC-allocated array literals. There's even a D-scanner warning for this. Is there any reason to prefer enum here?

@0-v-0
Copy link
Contributor Author

0-v-0 commented Jul 7, 2024

These array literals are only used in CTFE, so using enum can avoid emitting to binary target

Geod24
Geod24 previously requested changes Jul 7, 2024
Copy link
Member

@Geod24 Geod24 left a comment

Choose a reason for hiding this comment

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

This is making the code worse, not better. It risks memory allocation, as already mentioned, for what benefit ?

@0-v-0
Copy link
Contributor Author

0-v-0 commented Jul 7, 2024

To clarify, could you specify whether the memory allocation occurs at runtime or compile time when using enum?

@0-v-0 0-v-0 changed the title use enum instead of immutable use enum instead of immutable for CTFE-only literals Jul 7, 2024
@Geod24
Copy link
Member

Geod24 commented Jul 7, 2024

To clarify, could you specify whether the memory allocation occurs at runtime or compile time when using enum?

Depends on the call site, but usually at runtime.

private enum Literal = [1, 2, 3, 4];
private immutable Constant = [1, 2, 3, 4];

void main()
{
    const int[] foo = Literal; // Works, but allocates
    const int[] bar = Literal; // Allocates again!
    const int[] foobar = Constant; // Does not allocate
    const int[] barfoo = Constant; // Also does not allocate
    assert(foobar is barfoo); // Pass
    assert(foo is bar); // Fails
}

@dkorpel
Copy link
Contributor

dkorpel commented Jul 7, 2024

for what benefit ?

I tested it, and dmd's executable becomes about 5 Kb smaller by not emitting these arrays into the binary.

size dmd
   text    data     bss     dec     hex filename
8566262  916832   53520 9536614  918466 dmd (before)
8564746  914256   53520 9532522  91746a dmd (after)

The arrays are only used to populate exptab, optab1 and optab2 at compile time, so this doesn't introduce new run-time allocations. As an alternative, the arrays can be defined inside the CTFE function that generates these tables, but I don't think that's required for this PR.

@dkorpel
Copy link
Contributor

dkorpel commented Jul 12, 2024

Since this fails the buildkite style check, can you either disable that warning, or turn the arrays back into static immutable variables, but inside the CTFE function literals that use them?

@dkorpel dkorpel dismissed Geod24’s stale review July 13, 2024 16:45

There's no memory allocation, and the benefit has been shown

@dkorpel dkorpel merged commit e669167 into dlang:master Jul 13, 2024
41 checks passed
thewilsonator pushed a commit to thewilsonator/dmd that referenced this pull request Oct 7, 2024
* use enum instead of immutable

* disable enum_array_literal_check for dmd.astbase
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.

4 participants