This is an archive of the discontinued LLVM Phabricator instance.

Support: Add move semantics to mapped_file_region
ClosedPublic

Authored by dexonsmith on Apr 8 2021, 6:52 PM.

Details

Summary

Update llvm::sys::fs::mapped_file_region to have a move constructor and
a move assignment operator, allowing it to be used as an Optional. Also,
update FileOutputBuffer's OnDiskBuffer to take advantage of this,
avoiding an extra allocation from the unique_ptr.

A nice follow-up would be to make the mapped_file_region constructor
private and replace its use with a factory function, such as
mapped_file_region::create(), that returns an Expected (or ErrorOr). I
don't plan on doing that immediately, but I might swing back later.

No functionality change, besides the saved allocation in OnDiskBuffer.

Diff Detail

Event Timeline

dexonsmith created this revision.Apr 8 2021, 6:52 PM
dexonsmith requested review of this revision.Apr 8 2021, 6:52 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 8 2021, 6:52 PM

looks like the underlying mapped_file_region could be made movable without the overhead of Optional (both the Windows and Unix dtors of mapped_file_region test whether the mapping is non-null, so a move ctor can null out the Mapping member to ensure no double-closing, etc) which might be nicer (avoiding the Optional overhead)/more general?

dexonsmith retitled this revision from Support: Allow mapped_file_region to be used in an Optional to Support: Add move semantics to mapped_file_region.
dexonsmith edited the summary of this revision. (Show Details)

Follow @dblaikie's suggestion to allow mapped_file_region to be used directly as an optional.

  • Add a default constructor.
  • Add an operator bool().
  • Add unmap().
dblaikie accepted this revision.Apr 9 2021, 1:40 PM

Looks great!

llvm/include/llvm/Support/FileSystem.h
1289–1291

This is the default/doesn't need to be written, since the type has user-declared move ops, so they could be omitted - but don't mind if you prefer to have them here for documentation.

This revision is now accepted and ready to land.Apr 9 2021, 1:40 PM
This revision was landed with ongoing or failed builds.Apr 9 2021, 6:00 PM
This revision was automatically updated to reflect the committed changes.

Thanks for the review! Landed in 0db6488a7704a322ae05f20ef3466bed3eb1bb98.

llvm/include/llvm/Support/FileSystem.h
1289–1291

I left this in, as I find it's a bit clearer for reading, but if the style is moving away from that I don't feel strongly.

dblaikie added inline comments.Apr 10 2021, 11:25 AM
llvm/include/llvm/Support/FileSystem.h
1289–1291

Nah, don't think there's any major prevailing take on that - do whatever you reckon is most readable!