Skip to content

Commit

Permalink
Perf: Lazy compute total byte count of all searchable files
Browse files Browse the repository at this point in the history
This avoids accessing FileContentsPieces
  • Loading branch information
rpaquay committed Feb 3, 2018
1 parent 138e7f7 commit dec5f72
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Server/FileSystemDatabase/FileDatabaseSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class FileDatabaseSnapshot : IFileDatabaseSnapshot {
private readonly Lazy<IList<FileName>> _fileNames;
private readonly Lazy<IList<FileContentsPiece>> _fileContentsPieces;
private readonly Lazy<long> _searchableFileCount;
private readonly Lazy<long> _totalFileContentsLength;

public FileDatabaseSnapshot(IDictionary<FullPath, string> projectHashes,
IDictionary<DirectoryName, DirectoryData> directories,
Expand All @@ -35,6 +36,7 @@ public FileDatabaseSnapshot(IDictionary<FullPath, string> projectHashes,
_fileNames = new Lazy<IList<FileName>>(CreateFileNames);
_fileContentsPieces = new Lazy<IList<FileContentsPiece>>(CreateFilePieces, LazyThreadSafetyMode.ExecutionAndPublication);
_searchableFileCount = new Lazy<long>(CountFilesWithContents);
_totalFileContentsLength = new Lazy<long>(ComputeTotalFileContentsLength);
}

public IDictionary<FullPath, string> ProjectHashes => _projectHashes;
Expand All @@ -54,6 +56,7 @@ public FileDatabaseSnapshot(IDictionary<FullPath, string> projectHashes,
public IList<FileName> FileNames => _fileNames.Value;
public IList<FileContentsPiece> FileContentsPieces => _fileContentsPieces.Value;
public long SearchableFileCount => _searchableFileCount.Value;
public long TotalFileContentsLength => _totalFileContentsLength.Value;

public IEnumerable<FileExtract> GetFileExtracts(FileName filename, IEnumerable<FilePositionSpan> spans,
int maxLength) {
Expand Down Expand Up @@ -113,5 +116,9 @@ private IList<FileContentsPiece> 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);
}
}
}
2 changes: 2 additions & 0 deletions src/Server/FileSystemDatabase/IFileDatabaseSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public interface IFileDatabaseSnapshot {
/// </summary>
long SearchableFileCount { get; }

long TotalFileContentsLength { get; }

/// <summary>
/// Returns the list of text extracts for a given <paramref
/// name="fileName"/> and list of <paramref name="spans"/>. <paramref
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override TypedResponse Process(TypedRequest typedRequest) {
ProjectCount = snapshot.ProjectRoots.Count,
FileCount = database.FileNames.Count,
SearchableFileCount = database.SearchableFileCount,
ServerNativeMemoryUsage = database.FileContentsPieces.Aggregate(0L, (x, piece) => x + piece.ByteLength),
ServerNativeMemoryUsage = database.TotalFileContentsLength,
IndexLastUpdatedUtc = indexingServerState.LastIndexUpdateUtc,
ServerGcMemoryUsage = GC.GetTotalMemory(false),
ServerStatus = indexingServerState.Status,
Expand Down

0 comments on commit dec5f72

Please sign in to comment.