Skip to content

Refactoring: Make dmd.root.file.File stateless#9728

Merged
thewilsonator merged 1 commit intodlang:masterfrom
kinke:file
May 13, 2019
Merged

Refactoring: Make dmd.root.file.File stateless#9728
thewilsonator merged 1 commit intodlang:masterfrom
kinke:file

Conversation

@kinke
Copy link
Contributor

@kinke kinke commented Apr 29, 2019

[I had to look into this, as we get an ICE with -lowmem on Linux/i386, a segfault originating from a File dtor.]

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @kinke! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the annotated coverage diff directly on GitHub with CodeCov's browser extension
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + dmd#9728"

@kinke kinke changed the title Minimally refactor dmd.root.file.File WIP: Minimally refactor dmd.root.file.File Apr 29, 2019
@kinke kinke force-pushed the file branch 4 times, most recently from 5957af8 to 67a890d Compare May 1, 2019 12:15
@thewilsonator
Copy link
Contributor

@kinke is this good to go?

@kinke
Copy link
Contributor Author

kinke commented May 2, 2019

Nope, not ready; e.g., the autotester suffers from the 2.079 DMD host compiler with its size_t C++-mangling regression on OSX64 (fixed in 2.082...); i.e., d_size_t doesn't account for DMD 2.079-2.081 host compilers on OSX. That's most likely the reason for the previous duplicate code (method also defined inline in C++ header) which I don't want to keep just to hack around the C++ frontend tests.

@kinke
Copy link
Contributor Author

kinke commented May 2, 2019

I'm surprised about the review action taken here - don't get me wrong, I appreciate it, just definitely not used to that level of detail. ;)

This started out as a small refactoring to aid me in analysing the ownership of these buffers and how on earth the dtor using mem.xfree wasn't an issue until now (before -lowmem and enabled GC), although read() uses C malloc. I think I found the bug - a File representing an object file assumed ownership of a backend Outbuffer (don't confuse with frontend OutBuffer; the latter using mem, the former C) just before writing it to disk; the instance lived on the heap and so wasn't finalized/destructed without enabled GC. Edit: That was a bug, but cannot be the LDC bug...

I don't have the energy for a proper refactoring - I think there should be no File (filename + buffer) at all. The buffer is only used transiently, either to read an existing file and then taking ownership away, or to set a buffer right before writing it out. The cognitive overhead of the buffer ownership would IMO be best mitigated by simply having something like Array!ubyte readFile(string filename) and void writeFile(string filename, void[] data).

@WalterBright
Copy link
Member

I think this conversation shows a bit of the downside of combining a bug fix with refactoring. They really should be separate ones. It's a little hard to see the bug fix in this refactoring.

As for refactoring, I really wish to raise the bar on what is a good refactoring. I've written a bit of a manifesto on it #9511. I'm not singling you out, consider #9729 where I put @benjones through the wringer. There's a lot of technical debt in dmd, and I don't want to have to refactor the refactors, so they have to be the best we can do. It had better be good if it says "cleanup" in the title :-)

@kinke kinke requested a review from RazvanN7 as a code owner May 3, 2019 21:37
@kinke kinke force-pushed the file branch 3 times, most recently from b4003d4 to 7e2d995 Compare May 4, 2019 14:16
@WebDrake
Copy link
Contributor

WebDrake commented May 9, 2019

Just to give input here -- note that this ICE is blocking an i386 snap package release for LDC 1.15.0, and this could mean that something is broken also for i386 DMD 2.085.1. Is there any chance of the fix making it into a DMD 2.085.2 (which would allow a corresponding LDC 1.15.1)?

@kinke
Copy link
Contributor Author

kinke commented May 9, 2019

DMD got -lowmem with 2.086, so 2.085 isn't affected; and we don't need a fix to land in DMD first to cherry-pick it to LDC.

@WalterBright
Copy link
Member

The trouble with this PR is it combined refactoring with the bug fix. They need to be SEPARATE PRs. The refactoring is what is delaying approving this. The bug fix itself is not controversial.

Please, everyone, do not mix refactoring with bug fixes.

@WebDrake
Copy link
Contributor

The trouble with this PR is it combined refactoring with the bug fix. They need to be SEPARATE PRs.

Completely agree that refactoring and fixes should not be combined. Sorry if my feedback came over as pressure or impatience -- it was related purely to the target branch for the fix, to ensure we got it everywhere that was needed. But @kinke's response allays those concerns.

@kinke
Copy link
Contributor Author

kinke commented May 11, 2019

I split off the fixes to #9771, targeting the stable branch. - I'll rebase this after that is in master.

@thewilsonator
Copy link
Contributor

You can make a stable -> master PR with this script

@kinke kinke force-pushed the file branch 2 times, most recently from 8f959d1 to e10113e Compare May 12, 2019 23:00
@kinke kinke changed the title WIP: Minimally refactor dmd.root.file.File Refactoring: Make dmd.root.file.File stateless May 12, 2019
This avoids buffer ownership complications, as the file contents buffers
now need to be managed by the callers of `File.read()`. A tiny new
helper struct, FileBuffer, makes sure the buffer is free'd via RAII.

File writing is simplified to passing a filename and data slice. No need
to set up a File instance and its buffer (incl. ownership) anymore.
@kinke
Copy link
Contributor Author

kinke commented May 13, 2019

Reduced to just a squashed refactoring. Semaphore failure seems unrelated.

@thewilsonator thewilsonator dismissed stale reviews from andralex and WalterBright May 13, 2019 23:50

comments addressed.

@thewilsonator thewilsonator merged commit 13bde1b into dlang:master May 13, 2019

import core.stdc.string : strcmp;
const(char)* name = this.name.toChars();
if (strcmp(name, "__main.d") == 0)
Copy link
Member

@ibuclaw ibuclaw May 14, 2019

Choose a reason for hiding this comment

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

REGRESSION, you can no longer have a module named __main.d in any project.

This also breaks gdc, where __main.d implementation is not embedded inside the compiler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're saying gdc itself uses a file named __main.d, without any path? My goal here was to move the special files to one place, instead of handling __stdin.d here and setting up __main.d in mars.d (the only place where the file buffer was set manually from outside and then File.read() being a no-op).

Copy link
Member

Choose a reason for hiding this comment

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

$ echo "import std.stdio; int main() { writeln(\"Hello\"); return 42; }" > __main.d
$ ./generated/linux/release/64/dmd __main.d -ofmain
$ ./main 
$ echo $?
0

How exactly is this desirable behaviour?

IMO, root/file.d should just be a generic wrapper around a file, it should not have any compiler-specific logic embedded within.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This applies to __stdin.d as well then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

=> #9795

kinke added a commit to kinke/dmd that referenced this pull request May 14, 2019
In 2.086, __main.d was 'read' in mars.d, and __stdin.d in struct File.
In dlang#9728, I moved both to File, and neglected that __main.d is only
a special filename if the `-main` switch is used.

Move these special cases to mars.d and fix the __main.d oversight.
@kinke kinke deleted the file branch May 14, 2019 20:06
kinke added a commit to kinke/dmd that referenced this pull request May 14, 2019
In 2.086, __main.d was 'read' in mars.d, and __stdin.d in struct File.
In dlang#9728, I moved both to File, and neglected that __main.d is only
a special filename if the `-main` switch is used.

Move these special cases to mars.d and fix the __main.d oversight.
kinke added a commit to kinke/dmd that referenced this pull request May 14, 2019
In 2.086, __main.d was 'read' in mars.d, and __stdin.d in struct File.
In dlang#9728, I moved both to File, and neglected that __main.d is only
a special filename if the `-main` switch is used.

Move these special cases to mars.d and fix the __main.d oversight.
kinke added a commit to kinke/dmd that referenced this pull request May 14, 2019
In 2.086, __main.d was 'read' in mars.d, and __stdin.d in struct File.
In dlang#9728, I moved both to File, and neglected that __main.d is only
a special filename if the `-main` switch is used.

Move these special cases to mars.d and fix the __main.d oversight.
lesderid pushed a commit to lesderid/dmd that referenced this pull request May 17, 2019
In 2.086, __main.d was 'read' in mars.d, and __stdin.d in struct File.
In dlang#9728, I moved both to File, and neglected that __main.d is only
a special filename if the `-main` switch is used.

Move these special cases to mars.d and fix the __main.d oversight.
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.

8 participants