tdf#133768 speed up temp file creation

Use DELETE_ON_CLOSE attribute, so we can avoid calling osl_removeFile.
Shaves 5% off the export time.

Change-Id: I4fef8f181ef7a92c4805cc5996c3a17800a22602
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/141718
Tested-by: Noel Grandin <noel.grandin@collabora.co.uk>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin 2022-10-24 09:06:42 +02:00 committed by Noel Grandin
parent 83334df9cf
commit 1ea0ea19f1
3 changed files with 14 additions and 2 deletions

View File

@ -50,8 +50,9 @@ enum class StreamMode {
// file i/o // file i/o
NOCREATE = 0x0004, ///< 1 == Don't create file NOCREATE = 0x0004, ///< 1 == Don't create file
TRUNC = 0x0008, ///< Truncate _existing_ file to zero length TRUNC = 0x0008, ///< Truncate _existing_ file to zero length
COPY_ON_SYMLINK = 0x0010, ///< copy-on-write for symlinks (Unix) COPY_ON_SYMLINK = 0x0010, ///< copy-on-write for symlinks (Unix-only)
TEMPORARY = 0x0020, ///< temporary file attribute (Windows-only) TEMPORARY = 0x0020, ///< temporary file attribute (Windows-only)
DELETE_ON_CLOSE = 0x0040, ///< only for temporary files (Windows-only)
// sharing options // sharing options
SHARE_DENYNONE = 0x0100, SHARE_DENYNONE = 0x0100,
SHARE_DENYREAD = 0x0200, // overrides denynone SHARE_DENYREAD = 0x0200, // overrides denynone
@ -65,7 +66,7 @@ enum class StreamMode {
}; };
namespace o3tl namespace o3tl
{ {
template<> struct typed_flags<StreamMode> : is_typed_flags<StreamMode, 0x0f3f> {}; template<> struct typed_flags<StreamMode> : is_typed_flags<StreamMode, 0x0f7f> {};
} }
#define STREAM_SEEK_TO_BEGIN 0L #define STREAM_SEEK_TO_BEGIN 0L

View File

@ -304,6 +304,8 @@ void SvFileStream::Open( const OUString& rFilename, StreamMode nMode )
if ( nMode & StreamMode::TEMPORARY ) if ( nMode & StreamMode::TEMPORARY )
nAttributes |= FILE_ATTRIBUTE_TEMPORARY; nAttributes |= FILE_ATTRIBUTE_TEMPORARY;
if ( nMode & StreamMode::DELETE_ON_CLOSE )
nAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
pInstanceData->hFile = CreateFileW( pInstanceData->hFile = CreateFileW(
o3tl::toW(aFilename.getStr()), o3tl::toW(aFilename.getStr()),

View File

@ -409,7 +409,11 @@ SvStream* TempFileFast::GetStream( StreamMode eMode )
if (!mxStream) if (!mxStream)
{ {
OUString aName = CreateTempNameFast(); OUString aName = CreateTempNameFast();
#ifdef _WIN32
mxStream.reset(new SvFileStream(aName, eMode | StreamMode::TEMPORARY | StreamMode::DELETE_ON_CLOSE));
#else
mxStream.reset(new SvFileStream(aName, eMode | StreamMode::TEMPORARY)); mxStream.reset(new SvFileStream(aName, eMode | StreamMode::TEMPORARY));
#endif
} }
return mxStream.get(); return mxStream.get();
} }
@ -420,8 +424,13 @@ void TempFileFast::CloseStream()
{ {
OUString aName = mxStream->GetFileName(); OUString aName = mxStream->GetFileName();
mxStream.reset(); mxStream.reset();
#ifdef _WIN32
// On Windows, the file is opened with FILE_FLAG_DELETE_ON_CLOSE, so it will delete as soon as the handle closes.
// On other platforms, we need to explicitly delete it.
#else
if (!aName.isEmpty() && (osl::FileBase::getFileURLFromSystemPath(aName, aName) == osl::FileBase::E_None)) if (!aName.isEmpty() && (osl::FileBase::getFileURLFromSystemPath(aName, aName) == osl::FileBase::E_None))
File::remove(aName); File::remove(aName);
#endif
} }
} }