From dec5f7268021b88aa973806e2b93a41d61c84684 Mon Sep 17 00:00:00 2001 From: Renaud Paquay Date: Sat, 3 Feb 2018 15:41:08 -0800 Subject: [PATCH] Perf: Lazy compute total byte count of all searchable files This avoids accessing FileContentsPieces --- src/Server/FileSystemDatabase/FileDatabaseSnapshot.cs | 7 +++++++ src/Server/FileSystemDatabase/IFileDatabaseSnapshot.cs | 2 ++ .../GetDatabaseStatisticsRequestHandler.cs | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Server/FileSystemDatabase/FileDatabaseSnapshot.cs b/src/Server/FileSystemDatabase/FileDatabaseSnapshot.cs index d5bd3155..92435acc 100644 --- a/src/Server/FileSystemDatabase/FileDatabaseSnapshot.cs +++ b/src/Server/FileSystemDatabase/FileDatabaseSnapshot.cs @@ -25,6 +25,7 @@ public class FileDatabaseSnapshot : IFileDatabaseSnapshot { private readonly Lazy> _fileNames; private readonly Lazy> _fileContentsPieces; private readonly Lazy _searchableFileCount; + private readonly Lazy _totalFileContentsLength; public FileDatabaseSnapshot(IDictionary projectHashes, IDictionary directories, @@ -35,6 +36,7 @@ public FileDatabaseSnapshot(IDictionary projectHashes, _fileNames = new Lazy>(CreateFileNames); _fileContentsPieces = new Lazy>(CreateFilePieces, LazyThreadSafetyMode.ExecutionAndPublication); _searchableFileCount = new Lazy(CountFilesWithContents); + _totalFileContentsLength = new Lazy(ComputeTotalFileContentsLength); } public IDictionary ProjectHashes => _projectHashes; @@ -54,6 +56,7 @@ public FileDatabaseSnapshot(IDictionary projectHashes, public IList FileNames => _fileNames.Value; public IList FileContentsPieces => _fileContentsPieces.Value; public long SearchableFileCount => _searchableFileCount.Value; + public long TotalFileContentsLength => _totalFileContentsLength.Value; public IEnumerable GetFileExtracts(FileName filename, IEnumerable spans, int maxLength) { @@ -113,5 +116,9 @@ private IList CreateFilePieces() { private long CountFilesWithContents() { return _files.Values.Where(FileDatabaseBuilder.FileHasContents).Count(); } + + private long ComputeTotalFileContentsLength() { + return _files.Values.Where(x => x.Contents != null).Aggregate(0L, (acc, x) => acc + x.Contents.ByteLength); + } } } diff --git a/src/Server/FileSystemDatabase/IFileDatabaseSnapshot.cs b/src/Server/FileSystemDatabase/IFileDatabaseSnapshot.cs index 840ef131..2e167b9e 100644 --- a/src/Server/FileSystemDatabase/IFileDatabaseSnapshot.cs +++ b/src/Server/FileSystemDatabase/IFileDatabaseSnapshot.cs @@ -32,6 +32,8 @@ public interface IFileDatabaseSnapshot { /// long SearchableFileCount { get; } + long TotalFileContentsLength { get; } + /// /// Returns the list of text extracts for a given and list of . x + piece.ByteLength), + ServerNativeMemoryUsage = database.TotalFileContentsLength, IndexLastUpdatedUtc = indexingServerState.LastIndexUpdateUtc, ServerGcMemoryUsage = GC.GetTotalMemory(false), ServerStatus = indexingServerState.Status,