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

[Dev Drive Insights] Move %userprofile$/.rustup and set RUSTUP_HOME as part of optimizing Cargo cache #3827

Merged
merged 4 commits into from
Sep 12, 2024

Conversation

SohamDas2021
Copy link
Contributor

Summary of the pull request

Cargo cache has dependencies on %userprofile$/.rustup. So we move this directory into the dev drive.

References and relevant issues

#2743

Detailed description of the pull request / Additional comments

Cargo cache has dependencies on %userprofile$/.rustup. So we move this directory into the dev drive.

Validation steps performed

Manually validated that the cache move works as expected.

PR checklist

@SohamDas2021 SohamDas2021 changed the title Move %userprofile$/.rustup and set RUSTUP_HOME as part of optimizing Cargo cache [Dev Drive Insights] Move %userprofile$/.rustup and set RUSTUP_HOME as part of optimizing Cargo cache Sep 10, 2024
Comment on lines 268 to 269
if (!MoveDirectory(relatedCacheDirectory, targetDirectory + "\\Related Directories\\" + relatedCacheDirectoryName))
{
Copy link
Contributor

@bbonaby bbonaby Sep 10, 2024

Choose a reason for hiding this comment

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

Suggested change
if (!MoveDirectory(relatedCacheDirectory, targetDirectory + "\\Related Directories\\" + relatedCacheDirectoryName))
{
if (!MoveDirectory(relatedCacheDirectory, $@"{targetDirectory}\Related Directories\{relatedCacheDirectoryName}"))

Just a cleaner way to do it in C# see https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/string-interpolation#how-to-use-escape-sequences-in-an-interpolated-string #Resolved

Copy link
Contributor

Choose a reason for hiding this comment

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

Also are we choosing to call the directory "Related Directories" or is that always the name we expect for a folder containing directories related to the cache. Just wanted to confirm for my future knowledge.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are choosing the name. The dir structure on the dev drive is as follows:

The main would move into the dev drive E: (for example) as E:\packages<cache>.

A related dir to this cache would go into E:\packages<cache>\RelatedDirectories<reldir>

Hope that clears confusion.

Comment on lines 257 to 261
private string GetDirectoryNameFromPath(string path)
{
var lastIndex = path.LastIndexOf('\\');
return path.Substring(lastIndex + 1);
}
Copy link
Contributor

@bbonaby bbonaby Sep 10, 2024

Choose a reason for hiding this comment

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

could we use the static method in .Nets Path class to get the directory name? I mean this one

Then on line 267 below we can do something like:

var relatedCacheDirectoryName = Path.GetDirectoryName(relatedCacheDirectory); 

Then we can leave the edge cases to that. Unless there is a reason we don't want to use this. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Path.GetFileName is what we need here. Thanks for the recommendation. Updated.

var index = 0;
foreach (var relatedEnvironmentVariableToBeSet in relatedEnvironmentVariablesToBeSet)
{
var relatedCacheDirectoryName = GetDirectoryNameFromPath(relatedCacheDirectories[index]);
Copy link
Contributor

@bbonaby bbonaby Sep 10, 2024

Choose a reason for hiding this comment

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

is the length of relatedEnvironmentVariableToBeSet always guaranteed to be the same length of relatedCacheDirectories? #ByDesign

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a good question, for the current case- yes. Generally speaking I would assume the same because it would be through the Env variables that the paths to these dirs would be determined.

foreach (var relatedEnvironmentVariableToBeSet in relatedEnvironmentVariablesToBeSet)
{
var relatedCacheDirectoryName = GetDirectoryNameFromPath(relatedCacheDirectories[index]);
SetEnvironmentVariable(relatedEnvironmentVariableToBeSet, directoryPath + "\\Related Directories\\" + relatedCacheDirectoryName);
Copy link
Contributor

@bbonaby bbonaby Sep 10, 2024

Choose a reason for hiding this comment

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

Same comment about string interpolation above and the naming of "Related Directories" #Resolved

List<string> existingDevDriveLetters);
List<string> existingDevDriveLetters,
List<string> relatedEnvironmentVariablesToBeSet,
List<string> relatedCacheDirectories);
Copy link
Contributor

@bbonaby bbonaby Sep 10, 2024

Choose a reason for hiding this comment

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

In the future we should move this delegate to the OptimizeDevDriveDialogViewModel.cs since its a factory delegate to create it. Would help with readability since we'd be able to see the OptimizeDevDriveDialogViewModel.cs class in the same file when an edit to the factory delegate is made.

But also see comment in the first file. We probably don't need to add these two to the factory delegate if we pass in the cache directly. #Resolved


private bool MoveDirectories(string sourceDirectory, string targetDirectory, List<string> relatedCacheDirectories)
{
foreach (var relatedCacheDirectory in relatedCacheDirectories)
Copy link
Contributor

@bbonaby bbonaby Sep 10, 2024

Choose a reason for hiding this comment

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

Depending on how many directories we get, we might want to think about doing this in parallel with a Parallel.ForEach loop. #WontFix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a great suggestion. Currently we only have single RelatedDirectories. So I am holding off to making the change. I have left a TODO comment to do this in future.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair, we could probably remove the TODO word itself though. I think the comment is good as is, incase a future dev is looking at the code

@@ -21,8 +21,8 @@ public static IServiceCollection AddWindowsCustomization(this IServiceCollection
services.AddTransient<FileExplorerPage>();

services.AddSingleton<OptimizeDevDriveDialogViewModelFactory>(sp =>
(cacheLocation, environmentVariable, exampleDevDriveLocation, existingDevDriveLetters) =>
ActivatorUtilities.CreateInstance<OptimizeDevDriveDialogViewModel>(sp, cacheLocation, environmentVariable, exampleDevDriveLocation, existingDevDriveLetters));
(cacheLocation, environmentVariable, exampleDevDriveLocation, existingDevDriveLetters, relatedEnvironmentVariablesToBeSet, relatedCacheDirectories) =>
Copy link
Contributor

@bbonaby bbonaby Sep 10, 2024

Choose a reason for hiding this comment

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

looking at how we use these and seeing how many we could potentially add now and in the future. What if we passed the DevDriveCacheData directly instead of passing its properties?I believe this would eliminate 4 of these parameters in
OptimizeDevDriveDialogViewModel's constructor. Thoughts? This also saves us in the future if we add more cache properties #WontFix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

While I agree with your comment. I am going to hold off making this change for now since this has a potential to regress existing functionality.

Copy link
Contributor

Choose a reason for hiding this comment

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

fair enough, we should create a refactor issue on GitHub


private bool MoveDirectories(string sourceDirectory, string targetDirectory, List<string> relatedCacheDirectories)
{
foreach (var relatedCacheDirectory in relatedCacheDirectories)
Copy link
Contributor

Choose a reason for hiding this comment

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

Fair, we could probably remove the TODO word itself though. I think the comment is good as is, incase a future dev is looking at the code

@@ -21,8 +21,8 @@ public static IServiceCollection AddWindowsCustomization(this IServiceCollection
services.AddTransient<FileExplorerPage>();

services.AddSingleton<OptimizeDevDriveDialogViewModelFactory>(sp =>
(cacheLocation, environmentVariable, exampleDevDriveLocation, existingDevDriveLetters) =>
ActivatorUtilities.CreateInstance<OptimizeDevDriveDialogViewModel>(sp, cacheLocation, environmentVariable, exampleDevDriveLocation, existingDevDriveLetters));
(cacheLocation, environmentVariable, exampleDevDriveLocation, existingDevDriveLetters, relatedEnvironmentVariablesToBeSet, relatedCacheDirectories) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

fair enough, we should create a refactor issue on GitHub

@SohamDas2021 SohamDas2021 merged commit 92c6eba into main Sep 12, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dev drive optimization suggestion for Rust
3 participants