[backend/core] Improve heuristics query performance, move timeline-related extensions into its own file
This commit is contained in:
parent
2858f66ad4
commit
7a57862048
4 changed files with 54 additions and 41 deletions
|
@ -31,7 +31,7 @@ public class TimelineController(DatabaseContext db, NoteRenderer noteRenderer, C
|
|||
public async Task<IActionResult> GetHomeTimeline(MastodonPaginationQuery query)
|
||||
{
|
||||
var user = HttpContext.GetUserOrFail();
|
||||
var heuristic = await QueryableExtensions.GetHeuristic(user, db, cache);
|
||||
var heuristic = await QueryableTimelineExtensions.GetHeuristic(user, db, cache);
|
||||
|
||||
var res = await db.Notes
|
||||
.IncludeCommonProperties()
|
||||
|
|
|
@ -29,7 +29,7 @@ public class TimelineController(DatabaseContext db, CacheService cache, NoteRend
|
|||
public async Task<IActionResult> GetHomeTimeline(PaginationQuery pq)
|
||||
{
|
||||
var user = HttpContext.GetUserOrFail();
|
||||
var heuristic = await QueryableExtensions.GetHeuristic(user, db, cache);
|
||||
var heuristic = await QueryableTimelineExtensions.GetHeuristic(user, db, cache);
|
||||
var notes = await db.Notes.IncludeCommonProperties()
|
||||
.FilterByFollowingAndOwn(user, db, heuristic)
|
||||
.EnsureVisibleFor(user)
|
||||
|
|
|
@ -259,45 +259,6 @@ public static class QueryableExtensions
|
|||
return query.Where(note => note.Visibility == visibility);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs the most efficient following query for the user in question.
|
||||
/// The different queries are identical but nudge the query planner towards the smartest query plan available.
|
||||
/// </summary>
|
||||
public static IQueryable<Note> FilterByFollowingAndOwn(
|
||||
this IQueryable<Note> query, User user, DatabaseContext db, int heuristic
|
||||
)
|
||||
{
|
||||
// Determined empirically in 2023. Ask zotan for the spreadsheet if you're curious.
|
||||
const int cutoff = 250;
|
||||
|
||||
return heuristic < cutoff
|
||||
? query.Where(note => db.Users
|
||||
.First(p => p == user)
|
||||
.Following
|
||||
.Select(p => p.Id)
|
||||
.Concat(new[] { user.Id })
|
||||
.Contains(note.UserId))
|
||||
: query.Where(note => note.User == user || note.User.IsFollowedBy(user));
|
||||
}
|
||||
|
||||
//TODO: move this into another class where it makes more sense
|
||||
public static async Task<int> GetHeuristic(User user, DatabaseContext db, CacheService cache)
|
||||
{
|
||||
return await cache.FetchValueAsync($"following-query-heuristic:{user.Id}",
|
||||
TimeSpan.FromHours(24), FetchHeuristic);
|
||||
|
||||
[SuppressMessage("ReSharper", "EntityFramework.UnsupportedServerSideFunctionCall")]
|
||||
async Task<int> FetchHeuristic()
|
||||
{
|
||||
var lastDate = await db.Notes.AnyAsync()
|
||||
? await db.Notes.OrderByDescending(p => p.Id).Select(p => p.CreatedAt).FirstOrDefaultAsync()
|
||||
: DateTime.UtcNow;
|
||||
|
||||
return await db.Notes.CountAsync(p => p.CreatedAt > lastDate - TimeSpan.FromDays(7) &&
|
||||
(p.User.IsFollowedBy(user) || p.User == user));
|
||||
}
|
||||
}
|
||||
|
||||
public static IQueryable<Note> FilterByUser(this IQueryable<Note> query, User user)
|
||||
{
|
||||
return query.Where(note => note.User == user);
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using Iceshrimp.Backend.Core.Database;
|
||||
using Iceshrimp.Backend.Core.Database.Tables;
|
||||
using Iceshrimp.Backend.Core.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Iceshrimp.Backend.Core.Extensions;
|
||||
|
||||
public static class QueryableTimelineExtensions
|
||||
{
|
||||
// Determined empirically in 2023. Ask zotan for the spreadsheet if you're curious.
|
||||
private const int Cutoff = 250;
|
||||
|
||||
public static IQueryable<Note> FilterByFollowingAndOwn(
|
||||
this IQueryable<Note> query, User user, DatabaseContext db, int heuristic
|
||||
)
|
||||
{
|
||||
return heuristic < Cutoff
|
||||
? query.FollowingAndOwnLowFreq(user, db)
|
||||
: query.Where(note => note.User == user || note.User.IsFollowedBy(user));
|
||||
}
|
||||
|
||||
private static IQueryable<Note> FollowingAndOwnLowFreq(this IQueryable<Note> query, User user, DatabaseContext db)
|
||||
=> query.Where(note => db.Followings
|
||||
.Where(p => p.Follower == user)
|
||||
.Select(p => p.FolloweeId)
|
||||
.Concat(new[] { user.Id })
|
||||
.Contains(note.UserId));
|
||||
|
||||
public static async Task<int> GetHeuristic(User user, DatabaseContext db, CacheService cache)
|
||||
{
|
||||
return await cache.FetchValueAsync($"following-query-heuristic:{user.Id}",
|
||||
TimeSpan.FromHours(24), FetchHeuristic);
|
||||
|
||||
[SuppressMessage("ReSharper", "EntityFramework.UnsupportedServerSideFunctionCall")]
|
||||
async Task<int> FetchHeuristic()
|
||||
{
|
||||
var latestNote = await db.Notes.OrderByDescending(p => p.Id)
|
||||
.Select(p => new { p.CreatedAt })
|
||||
.FirstOrDefaultAsync() ??
|
||||
new { CreatedAt = DateTime.UtcNow };
|
||||
|
||||
//TODO: maybe we should express this as a ratio between matching and non-matching posts
|
||||
return await db.Notes
|
||||
.Where(p => p.CreatedAt > latestNote.CreatedAt - TimeSpan.FromDays(7))
|
||||
.FollowingAndOwnLowFreq(user, db)
|
||||
//.Select(p => new { })
|
||||
.Take(Cutoff + 1)
|
||||
.CountAsync();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue