From 93a3b053c9e43b3bfb4086082d9a3951aee2554c Mon Sep 17 00:00:00 2001 From: Vadim Pestryanin Date: Sat, 18 Mar 2023 00:10:25 +0700 Subject: [PATCH 1/4] optimizations --- .idea/.idea.OptimizeMePlease/.idea/.gitignore | 13 ++++++ BenchmarkService.cs | 46 +++++++++++++++++++ DTOs.cs | 24 ++++++++++ 3 files changed, 83 insertions(+) create mode 100644 .idea/.idea.OptimizeMePlease/.idea/.gitignore create mode 100644 DTOs.cs diff --git a/.idea/.idea.OptimizeMePlease/.idea/.gitignore b/.idea/.idea.OptimizeMePlease/.idea/.gitignore new file mode 100644 index 0000000..243280d --- /dev/null +++ b/.idea/.idea.OptimizeMePlease/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/contentModel.xml +/.idea.OptimizeMePlease.iml +/modules.xml +/projectSettingsUpdater.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/BenchmarkService.cs b/BenchmarkService.cs index a32a5c8..25884c3 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -97,6 +97,52 @@ public List GetAuthors() return finalAuthors; } + + /// Get top 2 Authors (FirstName, LastName, UserName, Email, Age, Country) + /// from country Serbia aged 27, with the highest BooksCount + /// and all his/her books (Book Name/Title and Publishment Year) published before 1900 + [Benchmark] + public List GetAuthors_Optimized_() + { + using var dbContext = new AppDbContext(); + + var orderedAuthors = dbContext.Authors.AsNoTracking() + .Include(x => x.User) + .OrderByDescending(x=>x.BooksCount) + .Select(x => new AuthorDto_Optimized + { + + Id = x.Id, + FirstName = x.User.FirstName, + LastName = x.User.LastName, + Email = x.User.Email, + UserName = x.User.UserName, + UserId = x.UserId, + Age = x.Age, + Country = x.Country + }) + + .Where(x => x.Country == "Serbia" && x.Age == 27) + .Take(2).ToList(); + + + var userIdArray = orderedAuthors.Select(x => x.UserId).ToArray(); + var dt = new DateTime(1900, 1, 1); + var books = dbContext.Books.Where(x => userIdArray.Contains(x.AuthorId) && x.Published < dt).Select(x => new BookStruct() + { + AuthorId = x.AuthorId, + Title = x.Name, + PublishedYear = x.Published.Year + }).ToList(); + + foreach (var auth in orderedAuthors) + { + auth.Books = books.Where(b => b.AuthorId == auth.Id).ToList(); + } + + return orderedAuthors; + } + //[Benchmark] //public List GetAuthors_Optimized() diff --git a/DTOs.cs b/DTOs.cs new file mode 100644 index 0000000..7aca585 --- /dev/null +++ b/DTOs.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; + +namespace OptimizeMePlease +{ + public class AuthorDto_Optimized + { + public int Id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string UserName { get; set; } + public string Email { get; set; } + public int UserId { get; set; } + public int Age { get; set; } + public string Country { get; set; } + public ICollection Books { get; set; } + } + + public struct BookStruct + { + public int AuthorId { get; set; } + public string Title { get; set; } + public int PublishedYear { get; set; } + } +} \ No newline at end of file From 5ac42cf2e203e3d67db7e36f45afa58c5660d26f Mon Sep 17 00:00:00 2001 From: Vadim Pestryanin Date: Sat, 18 Mar 2023 00:26:16 +0700 Subject: [PATCH 2/4] not sure what can be optimized for speed else --- BenchmarkService.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/BenchmarkService.cs b/BenchmarkService.cs index 25884c3..f53c90f 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -108,6 +108,7 @@ public List GetAuthors_Optimized_() var orderedAuthors = dbContext.Authors.AsNoTracking() .Include(x => x.User) + .Where(x => x.Country == "Serbia" && x.Age == 27) .OrderByDescending(x=>x.BooksCount) .Select(x => new AuthorDto_Optimized { @@ -121,12 +122,10 @@ public List GetAuthors_Optimized_() Age = x.Age, Country = x.Country }) - - .Where(x => x.Country == "Serbia" && x.Age == 27) .Take(2).ToList(); - var userIdArray = orderedAuthors.Select(x => x.UserId).ToArray(); + var userIdArray = orderedAuthors.Select(x => x.Id).ToArray(); var dt = new DateTime(1900, 1, 1); var books = dbContext.Books.Where(x => userIdArray.Contains(x.AuthorId) && x.Published < dt).Select(x => new BookStruct() { @@ -174,7 +173,7 @@ public List GetAuthors_Optimized_() // .ToList(); //} - //[Benchmark] + [Benchmark] public List GetAuthors_Optimized_Struct() { using var dbContext = new AppDbContext(); From 376dc2cce8267928dd05e630b8e7103af6e5dea8 Mon Sep 17 00:00:00 2001 From: Vadim Pestryanin Date: Sat, 18 Mar 2023 00:40:12 +0700 Subject: [PATCH 3/4] .net 7 optimizations --- BenchmarkService.cs | 4 ++-- OptimizeMePlease.csproj | 3 ++- global.json | 7 +++++++ 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 global.json diff --git a/BenchmarkService.cs b/BenchmarkService.cs index f53c90f..94271e2 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -32,7 +32,7 @@ public BenchmarkService() /// and all his/her books (Book Name/Title and Publishment Year) published before 1900 /// /// - [Benchmark] + [Benchmark(Baseline = true)] public List GetAuthors() { using var dbContext = new AppDbContext(); @@ -173,7 +173,7 @@ public List GetAuthors_Optimized_() // .ToList(); //} - [Benchmark] + //[Benchmark] public List GetAuthors_Optimized_Struct() { using var dbContext = new AppDbContext(); diff --git a/OptimizeMePlease.csproj b/OptimizeMePlease.csproj index f603ecd..00b1727 100644 --- a/OptimizeMePlease.csproj +++ b/OptimizeMePlease.csproj @@ -2,7 +2,8 @@ Exe - netcoreapp3.1 + net7.0 + 11 diff --git a/global.json b/global.json new file mode 100644 index 0000000..7cd6a1f --- /dev/null +++ b/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "7.0.0", + "rollForward": "latestMajor", + "allowPrerelease": true + } +} \ No newline at end of file From 0cede1a1f2342dc5e62e99ed984b1f5d8b9772a8 Mon Sep 17 00:00:00 2001 From: Vadim Pestryanin Date: Sat, 18 Mar 2023 00:54:56 +0700 Subject: [PATCH 4/4] looks this can improve a bit --- BenchmarkService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BenchmarkService.cs b/BenchmarkService.cs index 94271e2..9bdb4c6 100644 --- a/BenchmarkService.cs +++ b/BenchmarkService.cs @@ -107,8 +107,8 @@ public List GetAuthors_Optimized_() using var dbContext = new AppDbContext(); var orderedAuthors = dbContext.Authors.AsNoTracking() - .Include(x => x.User) .Where(x => x.Country == "Serbia" && x.Age == 27) + .Include(x => x.User) .OrderByDescending(x=>x.BooksCount) .Select(x => new AuthorDto_Optimized {