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

Inefficient AA value setting #18706

Open
dlangBugzillaToGithub opened this issue Nov 2, 2013 · 0 comments
Open

Inefficient AA value setting #18706

dlangBugzillaToGithub opened this issue Nov 2, 2013 · 0 comments
Labels
Druntime:AA Specific to Associative Arrays P2 Severity:major

Comments

@dlangBugzillaToGithub
Copy link

Kenji Hara (@9rnsr) reported this on 2013-11-02T05:51:21Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=11420

Description

This is both dmd and druntime issue.

By fixing issue 6178, AA value setting with opAssign behavior has been properly fixed in 2.064.

struct S
{
    int val;
    this(int v) { val = 1; }
    void opAssign(S) { val = 2; }
}
void main()
{
    S[int] aa;
    aa[1] = S(1);   // S(1) is moved in the newly allocated AA slot
    aa[1] = S(2);   // opAssign(S1) is called on existing aa[1] value
    assert(aa.length == 1 && aa[1].val == 2);
}

But, the generated code is a little inefficient because it would search the given key twice.

// aa[1] = S(1); is lowered to:
1 in aa ? aa[1].opAssign(S(1)) : aa[1].__ctor(1);
// --> 1 in aa is the first key search.
// --> aa[1]   is the second key search.

To fix the issue, we need to add a new internal function in druntime, and compiler should generate code which uses the function.

The new function's spec is:
- it takes at least two arguments, the AA and assigned value.
- it should return two values, one is the allocated/found slot, and the other is a boolean flag which the returned slot is newly allocated or not.
@thewilsonator thewilsonator added the Druntime:AA Specific to Associative Arrays label Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Druntime:AA Specific to Associative Arrays P2 Severity:major
Projects
None yet
Development

No branches or pull requests

2 participants