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

No scratch for absPath? #9775

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Ericson2314
Copy link
Member

Motivation

I found a way to avoid the scratch variable I just introduced in #9759, but I am not sure whether this makes this code easier or harder to read 😅.

Context

See conversation in #9759

Priorities and Process

Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@Ericson2314 Ericson2314 requested a review from edolstra as a code owner January 15, 2024 15:26
@Ericson2314 Ericson2314 force-pushed the abs-path-string-view branch 2 times, most recently from fe178aa to 966b3e3 Compare January 15, 2024 16:07
@github-actions github-actions bot added the new-cli Relating to the "nix" command label Jan 15, 2024
@Ericson2314 Ericson2314 force-pushed the abs-path-string-view branch 5 times, most recently from 561ba93 to 6caad76 Compare January 17, 2024 00:40
I found a way to avoid the `scratch` variable I just introduced in NixOS#9759,
but I am not sure whether this makes this code easier or harder to read
😅.
@Ericson2314 Ericson2314 changed the title No scratch for absPath No scratch for absPath? Jan 17, 2024
@Ericson2314 Ericson2314 marked this pull request as draft January 17, 2024 01:25
@Ericson2314
Copy link
Member Author

@tfc I am curious whether you think this scratch* variables are necessary. I think they are because const std::string & would make the rvalues last long enough, but std::string would not.

Copy link
Contributor

@tfc tfc left a comment

Choose a reason for hiding this comment

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

It feels like creating one central function std::string getCwd that hides all the legacy mem management issues would be nicer.

return canonPath(path, resolveSymlinks);
"/",
path)),
resolveSymlinks);
Copy link
Contributor

Choose a reason for hiding this comment

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

i see the elegance in point-free programming in general, but the if-branched ersion that we had before was more readable.

{
std::string scratch;
return std::unique_ptr<char, FreeDeleter> { getcwd(NULL, 0) };
Copy link
Contributor

Choose a reason for hiding this comment

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

is this called so often that it must be really fast and small, so that it is worth spilling this memory management over the code that wants the cwd string?

If no, then please create a function like std::string getCwd() that copies the content and then frees the original string. this way we don't need anyone to know about custom deleters for this purpose.

free(buf);
getcwd(buf, sizeof(buf))
? buf
: throw SysError("cannot get cwd"),
Copy link
Contributor

Choose a reason for hiding this comment

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

can't we encapsulate these two branches and the whole legacy mem management issue simply into a function std::string getCwd() that does it all automagically (with the performance-question yes/no from earler in mind)?

this seems like an unnecessary dance for a function whose concern is making paths absolute.

@Ericson2314
Copy link
Member Author

@tfc and I discussed this. Yes I'll stop being a weenie and do std::string getCwd().

@roberth and I further discussed @tfc's caching idea, and @roberth had the code idea that we can wrap chdir to invalid the cache so it's safe. (I am not worried about forgetting to wrap chdirs because Windows support will guarantee that that such raw Unix things are caught 😀)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-cli Relating to the "nix" command
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants