Skip to content

Commit

Permalink
Merge pull request #21 from CDEApp/feat/memory
Browse files Browse the repository at this point in the history
feat(memory): Reduce memory by string intern ing filepaths
  • Loading branch information
jafin authored Jul 9, 2023
2 parents ef1755b + 6a223e7 commit 9e1e197
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 79 deletions.
73 changes: 29 additions & 44 deletions src/cdeLib/Entities/DirEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace cdeLib.Entities;
[MessagePackObject]
public class DirEntry : ICommonEntry
{
private string _path;

[IgnoreMember]
public virtual DateTime Modified
{
Expand All @@ -42,8 +44,6 @@ public virtual DateTime Modified
[IgnoreMember]
public bool HashSpecified => IsHashDone;

//public string HashAsString { get { return ByteArrayHelper.ByteArrayToString(Hash); } }

[ProtoMember(6, IsRequired = false)] // is there a better default value than 0 here
[FlatBufferItem(6)]
[Key(6)]
Expand Down Expand Up @@ -206,8 +206,7 @@ public DirEntry(bool isDirectory)

public void SetPath(string path)
{
this.Path = path;
PathProblem = IsBadPath();
Path = path;
}

public DirEntry(FileSystemInfo fs) : this()
Expand All @@ -219,7 +218,7 @@ public DirEntry(FileSystemInfo fs) : this()
}
catch (ArgumentOutOfRangeException ex)
{
Log.Logger.Error(ex, "Error getting WriteTime for {0}, using CreationTime instead.", fs.Name);
Log.Logger.Error(ex, "Error getting WriteTime for {Filename}, using CreationTime instead", fs.Name);
Modified = fs.CreationTime;
}

Expand Down Expand Up @@ -273,22 +272,13 @@ public int ModifiedCompareTo(ICommonEntry de)
return -1; // this before de
}

if (IsModifiedBad && !de.IsModifiedBad)
{
return -1; // this before de
}

if (!IsModifiedBad && de.IsModifiedBad)
{
return 1; // this after de
}

if (IsModifiedBad && de.IsModifiedBad)
return IsModifiedBad switch
{
return 0;
}

return DateTime.Compare(Modified, de.Modified);
true when !de.IsModifiedBad => -1,
false when de.IsModifiedBad => 1,
true when de.IsModifiedBad => 0,
_ => DateTime.Compare(Modified, de.Modified)
};
}

// is this right ? for the simple compareResult invert we do in caller ? - maybe not ? keep dirs at top anyway ?
Expand All @@ -299,17 +289,12 @@ public int PathCompareWithDirTo(ICommonEntry de)
return -1; // this before de
}

if (IsDirectory && !de.IsDirectory)
return IsDirectory switch
{
return -1; // this before de
}

if (!IsDirectory && de.IsDirectory)
{
return 1; // this after de
}

return DirEntryConsts.MyCompareInfo.Compare(Path, de.Path, DirEntryConsts.MyCompareOptions);
true when !de.IsDirectory => -1,
false when de.IsDirectory => 1,
_ => DirEntryConsts.MyCompareInfo.Compare(Path, de.Path, DirEntryConsts.MyCompareOptions)
};
}

public int PathCompareTo(ICommonEntry de)
Expand Down Expand Up @@ -337,11 +322,6 @@ public void SetSummaryFields()
if (dirEntry.IsDirectory)
{
dirEntry.SetSummaryFields();
if (PathProblem) // infects child entries
{
dirEntry.PathProblem = PathProblem;
}

++dirEntryCount;
}

Expand All @@ -368,7 +348,7 @@ public void SetSummaryFields()

public void AddChild(DirEntry child)
{
if (this.Children == null)
if (Children == null)
{
Children = new List<DirEntry>();
}
Expand All @@ -387,7 +367,11 @@ public void AddChild(DirEntry child)
[ProtoMember(5, IsRequired = true)]
[FlatBufferItem(5)]
[Key(5)]
public virtual string Path { get; set; }
public virtual string Path
{
get => _path;
set => _path = string.Intern(value);
}

[IgnoreMember]
public ICommonEntry ParentCommonEntry { get; set; }
Expand All @@ -397,18 +381,16 @@ public void AddChild(DirEntry child)
/// </summary>
[IgnoreMember]
//public string FullPath { get; set; }
public string FullPath
{
get => ParentCommonEntry.MakeFullPath(this);
}
public string FullPath => ParentCommonEntry.MakeFullPath(this);

/// <summary>
/// True if entry name ends with Space or Period which is a problem on windows file systems.
/// If this entry is a directory this infects all child entries as well.
/// Populated on load not saved to disk.
/// </summary>
[Key(7)]
public bool PathProblem { get; set; } = false;
//[Key(7)]
[IgnoreMember]
public bool PathProblem => IsBadPath();

public void TraverseTreePair(TraverseFunc func)
{
Expand Down Expand Up @@ -538,6 +520,9 @@ private bool IsBadPath()
{
// This probably needs to check all parent paths if this is a root entry.
// Not high priority as will not generally be able to specify a folder with a problem path at or above root.
return !string.IsNullOrEmpty(Path) && (Path.EndsWith(" ") || Path.EndsWith("."));
return (!string.IsNullOrEmpty(Path) && (Path.EndsWith(" ") || Path.EndsWith("."))) || ParentCommonEntry is
{
PathProblem: true
};
}
}
4 changes: 1 addition & 3 deletions src/cdeLib/Entities/EntryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ public static string MakeFullPath(ICommonEntry parentEntry, ICommonEntry dirEntr
/// </summary>
/// <param name="rootEntries">Entries to traverse</param>
/// <param name="traverseFunc">TraversalFunc</param>
/// <param name="catalogRootEntry">Catalog root entry, show we can bind the catalog name to each entry</param>
public static void TraverseTreePair(IEnumerable<ICommonEntry> rootEntries, TraverseFunc traverseFunc,
RootEntry catalogRootEntry = null)
public static void TraverseTreePair(IEnumerable<ICommonEntry> rootEntries, TraverseFunc traverseFunc)
{
if (traverseFunc == null)
{
Expand Down
25 changes: 11 additions & 14 deletions src/cdeLib/Entities/RootEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ public void SetHash(byte[] hash)
// For testing convenience.
public void SetHash(int hash)
{
Hash.HashB = (ulong) hash;
Hash.HashB = (ulong)hash;
IsHashDone = true;
}

Expand Down Expand Up @@ -613,7 +613,9 @@ public int SizeCompareWithDirTo(ICommonEntry de)
//}
// the cast breaks this.
var sizeCompare = Size.CompareTo(de.Size);
return sizeCompare == 0 ? DirEntryConsts.MyCompareInfo.Compare(Path, de.Path, DirEntryConsts.MyCompareOptions) : sizeCompare;
return sizeCompare == 0
? DirEntryConsts.MyCompareInfo.Compare(Path, de.Path, DirEntryConsts.MyCompareOptions)
: sizeCompare;
}

public int ModifiedCompareTo(ICommonEntry de)
Expand Down Expand Up @@ -677,14 +679,9 @@ public void SetSummaryFields()
if (dirEntry.IsDirectory)
{
dirEntry.SetSummaryFields();
if (PathProblem) // infects child entries
{
dirEntry.PathProblem = PathProblem;
}

++dirEntryCount;
}

size += dirEntry.Size;
fileEntryCount += dirEntry.FileEntryCount;
childrenDirEntryCount += dirEntry.DirEntryCount;
Expand All @@ -694,8 +691,8 @@ public void SetSummaryFields()
dirEntryCount += childrenDirEntryCount;
}

FileEntryCount = (uint) fileEntryCount;
DirEntryCount = (uint) dirEntryCount;
FileEntryCount = (uint)fileEntryCount;
DirEntryCount = (uint)dirEntryCount;
Size = size;
}

Expand Down Expand Up @@ -751,7 +748,7 @@ public void AddChild(DirEntry child)

public void TraverseTreePair(TraverseFunc func)
{
TraverseTreePair(new List<ICommonEntry> {this}, func);
TraverseTreePair(new List<ICommonEntry> { this }, func);
}

/// <summary>
Expand Down Expand Up @@ -817,7 +814,7 @@ public void TraverseTreesCopyHash(ICommonEntry destination)
// traverse every source entry copy across the meta data that matches on destination entry
// if it adds value to destination.
// if destination is not there source not processed.
dirs.Push(Tuple.Create(sourcePath, (ICommonEntry) source, destination));
dirs.Push(Tuple.Create(sourcePath, (ICommonEntry)source, destination));

while (dirs.Count > 0)
{
Expand Down Expand Up @@ -862,8 +859,8 @@ public void TraverseTreesCopyHash(ICommonEntry destination)
{
if (destinationDirEntry.IsDirectory)
{
dirs.Push(Tuple.Create(fullPath, (ICommonEntry) sourceDirEntry,
(ICommonEntry) destinationDirEntry));
dirs.Push(Tuple.Create(fullPath, (ICommonEntry)sourceDirEntry,
(ICommonEntry)destinationDirEntry));
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/cdeLib/FindOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ public void Find(IEnumerable<RootEntry> rootEntries)
var findFunc = GetFindFunc(_progressCount, limitCount);
// ReSharper disable PossibleMultipleEnumeration

var watch = new System.Diagnostics.Stopwatch();
watch.Start();
var watch = Stopwatch.StartNew();
Parallel.ForEach(rootEntries, (rootEntry) =>
{
//TODO: Parallel breaks the progress percentage, need to fix.
Expand Down
32 changes: 16 additions & 16 deletions src/cdeWin/CDEWinForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,28 @@ public partial class CDEWinForm : Form, ICDEWinForm
public event EventAction OnReloadCatalogs;

private readonly ComboBoxItem<int>[] _byteSizeUnits = {
new ComboBoxItem<int>("byte(s)", 1),
new ComboBoxItem<int>("KB(s)", 1000),
new ComboBoxItem<int>("KiB(s)", 1024),
new ComboBoxItem<int>("MB(s)", 1000 * 1000),
new ComboBoxItem<int>("MiB(s)", 1024 * 1024),
new ComboBoxItem<int>("GB(s)", 1000 * 1000 * 1000),
new ComboBoxItem<int>("GIB(s)", 1024 * 1024 * 1024)
new("byte(s)", 1),
new("KB(s)", 1000),
new("KiB(s)", 1024),
new("MB(s)", 1000 * 1000),
new("MiB(s)", 1024 * 1024),
new("GB(s)", 1000 * 1000 * 1000),
new("GIB(s)", 1024 * 1024 * 1024)
};

private readonly ComboBoxItem<AddTimeUnitFunc>[] _durationUnits = {
new ComboBoxItem<AddTimeUnitFunc>("Minute(s)", AddTimeUtil.AddMinute),
new ComboBoxItem<AddTimeUnitFunc>("Hour(s)", AddTimeUtil.AddHour),
new ComboBoxItem<AddTimeUnitFunc>("Day(s)", AddTimeUtil.AddDay),
new ComboBoxItem<AddTimeUnitFunc>("Month(s)", AddTimeUtil.AddMonth),
new ComboBoxItem<AddTimeUnitFunc>("Year(s)", AddTimeUtil.AddYear)
new("Minute(s)", AddTimeUtil.AddMinute),
new("Hour(s)", AddTimeUtil.AddHour),
new("Day(s)", AddTimeUtil.AddDay),
new("Month(s)", AddTimeUtil.AddMonth),
new("Year(s)", AddTimeUtil.AddYear)
};

private readonly ComboBoxItem<int>[] _limitResultValues = {
new ComboBoxItem<int>("Max Results 1000", 1000),
new ComboBoxItem<int>("Max Results 10000", 10000),
new ComboBoxItem<int>("Max Results 100000", 100000),
new ComboBoxItem<int>("Unlimited Results", int.MaxValue)
new("Max Results 1000", 1000),
new("Max Results 10000", 10000),
new("Max Results 100000", 100000),
new("Unlimited Results", int.MaxValue)
};

private readonly IConfig _config;
Expand Down

0 comments on commit 9e1e197

Please sign in to comment.