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

Question - Can Struct/Tuples be faster for Non Tracked Queries #15223

Closed
ackava opened this issue Apr 1, 2019 · 4 comments
Closed

Question - Can Struct/Tuples be faster for Non Tracked Queries #15223

ackava opened this issue Apr 1, 2019 · 4 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@ackava
Copy link

ackava commented Apr 1, 2019

For non tracked query, with small entity for fairly large result requires unnecessary allocating so many small objects in heap.

Most non tracked objects are anyway readonly, so why force use of class?

Wouldn't it be faster to use struct?

@ajcvickers
Copy link
Contributor

@ackava Can you post some code showing an example of a struct that you want to map in this way, and what the code looks like to query it from the database?

@ackava
Copy link
Author

ackava commented Apr 3, 2019

Consider following query, I need to pull ids with some status and send it to some other service. With tuple, I don't need to create class.

Consider, EmailRecipient entity has lot of fields (10+) and I only need two fields

     var list = await db.EmailRecipients
                      .Where(x => x.Email.Sent == true && x.Processed = false)
                      // tuple syntax..
                      .Select( x => ( x.RecipientID, x.EmailAddress ))
                      .Take(1000)
                      .ToListAsync();
     foreach(var (id, email) in list) {
              ..... send push notification 
     }

// with struct..

    public struct RecipientInfo {
           public string ID;
           public string Email;
    }

     var list = await db.EmailRecipients
                      .Where(x => x.Email.Sent == true && x.Processed = false)
                      .Select( x => new RecipientInfo { ID = x.RecipientID, Email = x.EmailAddress } )
                      .Take(1000)
                      .ToListAsync();
     foreach(var r in list) {
              ..... send push notification 
     }

In above example, I am not using change tracking, even for couple of fields, Tuple will only hold references to string and copying few bytes on stack will be lot faster compared to allocating and releasing these objects.

@smitpatel
Copy link
Contributor

Those queries should be working already. Did you try running them? Did you run into any issues?

@ackava
Copy link
Author

ackava commented Apr 3, 2019

I got error which is already reported at, dotnet/roslyn#12897

Sorry I thought it was related to EF, but now I understand it is roslyn issue.

Struct and Tuple are working fine if I use ValueTuple.Create.

@ackava ackava closed this as completed Apr 3, 2019
@ajcvickers ajcvickers added closed-no-further-action The issue is closed and no further action is planned. customer-reported labels Apr 3, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants