[go: up one dir, main page]

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

Minor changes to reduce allocations #848

Merged
merged 12 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tuple -> ValueTuple
  • Loading branch information
Bond-009 committed Feb 12, 2019
commit 41fb1e510616e42490354b8912d2117b836ab822
21 changes: 2 additions & 19 deletions Emby.Server.Implementations/Data/BaseSqliteRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,31 +224,14 @@ protected void RunDefaultInitialization(ManagedConnection db)
});
}

db.ExecuteAll(string.Join(";", queries.ToArray()));
db.ExecuteAll(string.Join(";", queries));
Logger.LogInformation("PRAGMA synchronous=" + db.Query("PRAGMA synchronous").SelectScalarString().First());
}

protected virtual bool EnableTempStoreMemory => false;

protected virtual int? CacheSize => null;

internal static void CheckOk(int rc)
{
string msg = "";

if (raw.SQLITE_OK != rc)
{
throw CreateException((ErrorCode)rc, msg);
}
}

internal static Exception CreateException(ErrorCode rc, string msg)
{
var exp = new Exception(msg);

return exp;
}

private bool _disposed;
protected void CheckDisposed()
{
Expand Down Expand Up @@ -381,7 +364,7 @@ public static IDisposable Read(this ReaderWriterLockSlim obj)
//{
// return new DummyToken();
//}
return new ReadLockToken(obj);
return new WriteLockToken(obj); // TODO: fix segfault
Copy link
Contributor

Choose a reason for hiding this comment

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

this message should be expanded, something like "fix segfault if using Read/Write lock"

Copy link
Member Author

Choose a reason for hiding this comment

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

I took a closer look at the db code and figured out the reason, I'll try and restructure all databases when my current big PRs are in.

Copy link
Contributor

Choose a reason for hiding this comment

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

Still this TODO should be either reformulated to be clear or removed altogether.

}

public static IDisposable Write(this ReaderWriterLockSlim obj)
Expand Down
35 changes: 15 additions & 20 deletions Emby.Server.Implementations/Data/SqliteItemRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2301,7 +2301,7 @@ private bool HasSeriesFields(InternalItemsQuery query)
return query.IncludeItemTypes.Any(x => _seriesTypes.Contains(x));
}

private string[] GetFinalColumnsToSelect(InternalItemsQuery query, string[] startColumns)
private List<string> GetFinalColumnsToSelect(InternalItemsQuery query, IEnumerable<string> startColumns)
{
var list = startColumns.ToList();

Expand Down Expand Up @@ -2431,7 +2431,7 @@ private string[] GetFinalColumnsToSelect(InternalItemsQuery query, string[] star
list.Add(builder.ToString());
}

return list.ToArray();
return list;
}

private void BindSearchParams(InternalItemsQuery query, IStatement statement)
Expand Down Expand Up @@ -5208,32 +5208,32 @@ private void UpdateAncestors(Guid itemId, List<Guid> ancestorIds, IDatabaseConne
}
}

public QueryResult<Tuple<BaseItem, ItemCounts>> GetAllArtists(InternalItemsQuery query)
public QueryResult<(BaseItem, ItemCounts)> GetAllArtists(InternalItemsQuery query)
{
return GetItemValues(query, new[] { 0, 1 }, typeof(MusicArtist).FullName);
}

public QueryResult<Tuple<BaseItem, ItemCounts>> GetArtists(InternalItemsQuery query)
public QueryResult<(BaseItem, ItemCounts)> GetArtists(InternalItemsQuery query)
{
return GetItemValues(query, new[] { 0 }, typeof(MusicArtist).FullName);
}

public QueryResult<Tuple<BaseItem, ItemCounts>> GetAlbumArtists(InternalItemsQuery query)
public QueryResult<(BaseItem, ItemCounts)> GetAlbumArtists(InternalItemsQuery query)
{
return GetItemValues(query, new[] { 1 }, typeof(MusicArtist).FullName);
}

public QueryResult<Tuple<BaseItem, ItemCounts>> GetStudios(InternalItemsQuery query)
public QueryResult<(BaseItem, ItemCounts)> GetStudios(InternalItemsQuery query)
{
return GetItemValues(query, new[] { 3 }, typeof(Studio).FullName);
}

public QueryResult<Tuple<BaseItem, ItemCounts>> GetGenres(InternalItemsQuery query)
public QueryResult<(BaseItem, ItemCounts)> GetGenres(InternalItemsQuery query)
{
return GetItemValues(query, new[] { 2 }, typeof(Genre).FullName);
}

public QueryResult<Tuple<BaseItem, ItemCounts>> GetMusicGenres(InternalItemsQuery query)
public QueryResult<(BaseItem, ItemCounts)> GetMusicGenres(InternalItemsQuery query)
{
return GetItemValues(query, new[] { 2 }, typeof(MusicGenre).FullName);
}
Expand Down Expand Up @@ -5310,7 +5310,7 @@ private List<string> GetItemValueNames(int[] itemValueTypes, List<string> withIt
}
}

private QueryResult<Tuple<BaseItem, ItemCounts>> GetItemValues(InternalItemsQuery query, int[] itemValueTypes, string returnType)
private QueryResult<(BaseItem, ItemCounts)> GetItemValues(InternalItemsQuery query, int[] itemValueTypes, string returnType)
{
if (query == null)
{
Expand All @@ -5328,7 +5328,7 @@ private QueryResult<Tuple<BaseItem, ItemCounts>> GetItemValues(InternalItemsQuer

var typeClause = itemValueTypes.Length == 1 ?
("Type=" + itemValueTypes[0].ToString(CultureInfo.InvariantCulture)) :
("Type in (" + string.Join(",", itemValueTypes.Select(i => i.ToString(CultureInfo.InvariantCulture)).ToArray()) + ")");
("Type in (" + string.Join(",", itemValueTypes.Select(i => i.ToString(CultureInfo.InvariantCulture))) + ")");

InternalItemsQuery typeSubQuery = null;

Expand Down Expand Up @@ -5356,11 +5356,7 @@ private QueryResult<Tuple<BaseItem, ItemCounts>> GetItemValues(InternalItemsQuer

whereClauses.Add("guid in (select ItemId from ItemValues where ItemValues.CleanValue=A.CleanName AND " + typeClause + ")");

var typeWhereText = whereClauses.Count == 0 ?
string.Empty :
" where " + string.Join(" AND ", whereClauses);

itemCountColumnQuery += typeWhereText;
itemCountColumnQuery += " where " + string.Join(" AND ", whereClauses);
JustAMan marked this conversation as resolved.
Show resolved Hide resolved

itemCountColumns = new Dictionary<string, string>()
{
Expand Down Expand Up @@ -5393,7 +5389,7 @@ private QueryResult<Tuple<BaseItem, ItemCounts>> GetItemValues(InternalItemsQuer
IsSeries = query.IsSeries
};

columns = GetFinalColumnsToSelect(query, columns.ToArray()).ToList();
columns = GetFinalColumnsToSelect(query, columns);

var commandText = "select "
+ string.Join(",", columns)
Expand Down Expand Up @@ -5485,8 +5481,8 @@ private QueryResult<Tuple<BaseItem, ItemCounts>> GetItemValues(InternalItemsQuer
{
return connection.RunInTransaction(db =>
{
var list = new List<Tuple<BaseItem, ItemCounts>>();
var result = new QueryResult<Tuple<BaseItem, ItemCounts>>();
var list = new List<(BaseItem, ItemCounts)>();
var result = new QueryResult<(BaseItem, ItemCounts)>();

var statements = PrepareAllSafe(db, statementTexts);

Expand Down Expand Up @@ -5524,7 +5520,7 @@ private QueryResult<Tuple<BaseItem, ItemCounts>> GetItemValues(InternalItemsQuer
{
var countStartColumn = columns.Count - 1;

list.Add(new Tuple<BaseItem, ItemCounts>(item, GetItemCounts(row, countStartColumn, typesToCount)));
list.Add((item, GetItemCounts(row, countStartColumn, typesToCount)));
}
}

Expand Down Expand Up @@ -6191,6 +6187,5 @@ private MediaStream GetMediaStream(IReadOnlyList<IResultSetValue> reader)

return item;
}

}
}
12 changes: 6 additions & 6 deletions Emby.Server.Implementations/Library/LibraryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1331,7 +1331,7 @@ public List<Guid> GetItemIds(InternalItemsQuery query)
return ItemRepository.GetItemIdsList(query);
}

public QueryResult<Tuple<BaseItem, ItemCounts>> GetStudios(InternalItemsQuery query)
public QueryResult<(BaseItem, ItemCounts)> GetStudios(InternalItemsQuery query)
{
if (query.User != null)
{
Expand All @@ -1342,7 +1342,7 @@ public QueryResult<Tuple<BaseItem, ItemCounts>> GetStudios(InternalItemsQuery qu
return ItemRepository.GetStudios(query);
}

public QueryResult<Tuple<BaseItem, ItemCounts>> GetGenres(InternalItemsQuery query)
public QueryResult<(BaseItem, ItemCounts)> GetGenres(InternalItemsQuery query)
{
if (query.User != null)
{
Expand All @@ -1353,7 +1353,7 @@ public QueryResult<Tuple<BaseItem, ItemCounts>> GetGenres(InternalItemsQuery que
return ItemRepository.GetGenres(query);
}

public QueryResult<Tuple<BaseItem, ItemCounts>> GetMusicGenres(InternalItemsQuery query)
public QueryResult<(BaseItem, ItemCounts)> GetMusicGenres(InternalItemsQuery query)
{
if (query.User != null)
{
Expand All @@ -1364,7 +1364,7 @@ public QueryResult<Tuple<BaseItem, ItemCounts>> GetMusicGenres(InternalItemsQuer
return ItemRepository.GetMusicGenres(query);
}

public QueryResult<Tuple<BaseItem, ItemCounts>> GetAllArtists(InternalItemsQuery query)
public QueryResult<(BaseItem, ItemCounts)> GetAllArtists(InternalItemsQuery query)
{
if (query.User != null)
{
Expand All @@ -1375,7 +1375,7 @@ public QueryResult<Tuple<BaseItem, ItemCounts>> GetAllArtists(InternalItemsQuery
return ItemRepository.GetAllArtists(query);
}

public QueryResult<Tuple<BaseItem, ItemCounts>> GetArtists(InternalItemsQuery query)
public QueryResult<(BaseItem, ItemCounts)> GetArtists(InternalItemsQuery query)
{
if (query.User != null)
{
Expand Down Expand Up @@ -1419,7 +1419,7 @@ private void SetTopParentOrAncestorIds(InternalItemsQuery query)
}
}

public QueryResult<Tuple<BaseItem, ItemCounts>> GetAlbumArtists(InternalItemsQuery query)
public QueryResult<(BaseItem, ItemCounts)> GetAlbumArtists(InternalItemsQuery query)
{
if (query.User != null)
{
Expand Down
2 changes: 1 addition & 1 deletion Jellyfin.Server/Jellyfin.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<AssemblyName>jellyfin</AssemblyName>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion MediaBrowser.Api/UserLibrary/ArtistsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public object Get(GetAlbumArtists request)
return ToOptimizedResult(result);
}

protected override QueryResult<Tuple<BaseItem, ItemCounts>> GetItems(GetItemsByName request, InternalItemsQuery query)
protected override QueryResult<(BaseItem, ItemCounts)> GetItems(GetItemsByName request, InternalItemsQuery query)
{
if (request is GetAlbumArtists)
{
Expand Down
4 changes: 2 additions & 2 deletions MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ protected QueryResult<BaseItemDto> GetResultSlim(GetItemsByName request)
};
}

protected virtual QueryResult<Tuple<BaseItem, ItemCounts>> GetItems(GetItemsByName request, InternalItemsQuery query)
protected virtual QueryResult<(BaseItem, ItemCounts)> GetItems(GetItemsByName request, InternalItemsQuery query)
{
return new QueryResult<Tuple<BaseItem, ItemCounts>>();
return new QueryResult<(BaseItem, ItemCounts)>();
}

private void SetItemCounts(BaseItemDto dto, ItemCounts counts)
Expand Down
2 changes: 1 addition & 1 deletion MediaBrowser.Api/UserLibrary/GenresService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public object Get(GetGenres request)
return ToOptimizedResult(result);
}

protected override QueryResult<Tuple<BaseItem, ItemCounts>> GetItems(GetItemsByName request, InternalItemsQuery query)
protected override QueryResult<(BaseItem, ItemCounts)> GetItems(GetItemsByName request, InternalItemsQuery query)
{
var viewType = GetParentItemViewType(request);

Expand Down
2 changes: 1 addition & 1 deletion MediaBrowser.Api/UserLibrary/MusicGenresService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public object Get(GetMusicGenres request)
return ToOptimizedResult(result);
}

protected override QueryResult<Tuple<BaseItem, ItemCounts>> GetItems(GetItemsByName request, InternalItemsQuery query)
protected override QueryResult<(BaseItem, ItemCounts)> GetItems(GetItemsByName request, InternalItemsQuery query)
{
return LibraryManager.GetMusicGenres(query);
}
Expand Down
6 changes: 3 additions & 3 deletions MediaBrowser.Api/UserLibrary/PersonsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,18 @@ protected override IEnumerable<BaseItem> GetAllItems(GetItemsByName request, ILi
throw new NotImplementedException();
}

protected override QueryResult<Tuple<BaseItem, ItemCounts>> GetItems(GetItemsByName request, InternalItemsQuery query)
protected override QueryResult<(BaseItem, ItemCounts)> GetItems(GetItemsByName request, InternalItemsQuery query)
{
var items = LibraryManager.GetPeopleItems(new InternalPeopleQuery
{
PersonTypes = query.PersonTypes,
NameContains = query.NameContains ?? query.SearchTerm
});

return new QueryResult<Tuple<BaseItem, ItemCounts>>
return new QueryResult<(BaseItem, ItemCounts)>
{
TotalRecordCount = items.Count,
Items = items.Take(query.Limit ?? int.MaxValue).Select(i => new Tuple<BaseItem, ItemCounts>(i, new ItemCounts())).ToArray()
Items = items.Take(query.Limit ?? int.MaxValue).Select(i => (i as BaseItem, new ItemCounts())).ToArray()
};
}

Expand Down
2 changes: 1 addition & 1 deletion MediaBrowser.Api/UserLibrary/StudiosService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public object Get(GetStudios request)
return ToOptimizedResult(result);
}

protected override QueryResult<Tuple<BaseItem, ItemCounts>> GetItems(GetItemsByName request, InternalItemsQuery query)
protected override QueryResult<(BaseItem, ItemCounts)> GetItems(GetItemsByName request, InternalItemsQuery query)
{
return LibraryManager.GetStudios(query);
}
Expand Down
12 changes: 6 additions & 6 deletions MediaBrowser.Controller/Library/ILibraryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,12 @@ IEnumerable<Video> FindExtras(BaseItem owner, List<FileSystemMetadata> fileSyste
void UpdateMediaPath(string virtualFolderName, MediaPathInfo path);
void RemoveMediaPath(string virtualFolderName, string path);

QueryResult<Tuple<BaseItem, ItemCounts>> GetGenres(InternalItemsQuery query);
QueryResult<Tuple<BaseItem, ItemCounts>> GetMusicGenres(InternalItemsQuery query);
QueryResult<Tuple<BaseItem, ItemCounts>> GetStudios(InternalItemsQuery query);
QueryResult<Tuple<BaseItem, ItemCounts>> GetArtists(InternalItemsQuery query);
QueryResult<Tuple<BaseItem, ItemCounts>> GetAlbumArtists(InternalItemsQuery query);
QueryResult<Tuple<BaseItem, ItemCounts>> GetAllArtists(InternalItemsQuery query);
QueryResult<(BaseItem, ItemCounts)> GetGenres(InternalItemsQuery query);
QueryResult<(BaseItem, ItemCounts)> GetMusicGenres(InternalItemsQuery query);
QueryResult<(BaseItem, ItemCounts)> GetStudios(InternalItemsQuery query);
QueryResult<(BaseItem, ItemCounts)> GetArtists(InternalItemsQuery query);
QueryResult<(BaseItem, ItemCounts)> GetAlbumArtists(InternalItemsQuery query);
QueryResult<(BaseItem, ItemCounts)> GetAllArtists(InternalItemsQuery query);

int GetCount(InternalItemsQuery query);

Expand Down
12 changes: 6 additions & 6 deletions MediaBrowser.Controller/Persistence/IItemRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ public interface IItemRepository : IRepository

int GetCount(InternalItemsQuery query);

QueryResult<Tuple<BaseItem, ItemCounts>> GetGenres(InternalItemsQuery query);
QueryResult<Tuple<BaseItem, ItemCounts>> GetMusicGenres(InternalItemsQuery query);
QueryResult<Tuple<BaseItem, ItemCounts>> GetStudios(InternalItemsQuery query);
QueryResult<Tuple<BaseItem, ItemCounts>> GetArtists(InternalItemsQuery query);
QueryResult<Tuple<BaseItem, ItemCounts>> GetAlbumArtists(InternalItemsQuery query);
QueryResult<Tuple<BaseItem, ItemCounts>> GetAllArtists(InternalItemsQuery query);
QueryResult<(BaseItem, ItemCounts)> GetGenres(InternalItemsQuery query);
QueryResult<(BaseItem, ItemCounts)> GetMusicGenres(InternalItemsQuery query);
QueryResult<(BaseItem, ItemCounts)> GetStudios(InternalItemsQuery query);
QueryResult<(BaseItem, ItemCounts)> GetArtists(InternalItemsQuery query);
QueryResult<(BaseItem, ItemCounts)> GetAlbumArtists(InternalItemsQuery query);
QueryResult<(BaseItem, ItemCounts)> GetAllArtists(InternalItemsQuery query);

List<string> GetMusicGenreNames();
List<string> GetStudioNames();
Expand Down