From dd8dc4e5132dba6a230790e19ab1474fc2e9abff Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Fri, 19 Aug 2022 20:01:36 -0400 Subject: [PATCH] Add a sequential read flagged fopen helper Trying to squeeze out some kind of gain by informing the OS we will read a file sequentially. In particular our FILE_LINE_READER just uses fgetc until the end --- common/richio.cpp | 3 +- libs/kiplatform/CMakeLists.txt | 3 ++ libs/kiplatform/gtk/io.cpp | 34 +++++++++++++ libs/kiplatform/include/kiplatform/io.h | 43 ++++++++++++++++ libs/kiplatform/msw/io.cpp | 68 +++++++++++++++++++++++++ libs/kiplatform/osx/io.mm | 28 ++++++++++ plugins/3d/vrml/CMakeLists.txt | 2 +- 7 files changed, 179 insertions(+), 2 deletions(-) create mode 100644 libs/kiplatform/gtk/io.cpp create mode 100644 libs/kiplatform/include/kiplatform/io.h create mode 100644 libs/kiplatform/msw/io.cpp create mode 100644 libs/kiplatform/osx/io.mm diff --git a/common/richio.cpp b/common/richio.cpp index 36cb30fd02..4f6cdf7f4b 100644 --- a/common/richio.cpp +++ b/common/richio.cpp @@ -26,6 +26,7 @@ #include #include // HAVE_FGETC_NOLOCK +#include #include #include #include @@ -209,7 +210,7 @@ FILE_LINE_READER::FILE_LINE_READER( const wxString& aFileName, unsigned aStartin unsigned aMaxLineLength ): LINE_READER( aMaxLineLength ), m_iOwn( true ) { - m_fp = wxFopen( aFileName, wxT( "rt" ) ); + m_fp = KIPLATFORM::IO::SeqFOpen( aFileName, wxT( "rt" ) ); if( !m_fp ) { diff --git a/libs/kiplatform/CMakeLists.txt b/libs/kiplatform/CMakeLists.txt index e41c628e1e..c86fa18b05 100644 --- a/libs/kiplatform/CMakeLists.txt +++ b/libs/kiplatform/CMakeLists.txt @@ -11,6 +11,7 @@ if( APPLE ) set( PLATFORM_SRCS osx/app.mm osx/environment.mm + osx/io.mm osx/policy.mm osx/ui.mm ) @@ -25,6 +26,7 @@ elseif( WIN32 ) set( PLATFORM_SRCS msw/app.cpp msw/environment.cpp + msw/io.cpp msw/policy.cpp msw/ui.cpp ) @@ -39,6 +41,7 @@ elseif( UNIX ) set( PLATFORM_SRCS gtk/app.cpp gtk/environment.cpp + gtk/io.cpp gtk/policy.cpp gtk/ui.cpp ) diff --git a/libs/kiplatform/gtk/io.cpp b/libs/kiplatform/gtk/io.cpp new file mode 100644 index 0000000000..9950ee32b3 --- /dev/null +++ b/libs/kiplatform/gtk/io.cpp @@ -0,0 +1,34 @@ +/* +* This program source code file is part of KiCad, a free EDA CAD application. +* +* Copyright (C) 2023 Mark Roszko +* +* This program is free software: you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation, either version 3 of the License, or (at your +* option) any later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program. If not, see . +*/ + +#include + +#include +#include + +#include + +FILE* KIPLATFORM::IO::SeqFOpen( const wxString& aPath, const wxString& aMode ) +{ + FILE* fp = wxFopen( aPath, aMode ); + + posix_fadvise( fileno( fp ), 0, 0, POSIX_FADV_SEQUENTIAL ); + + return fp; +} \ No newline at end of file diff --git a/libs/kiplatform/include/kiplatform/io.h b/libs/kiplatform/include/kiplatform/io.h new file mode 100644 index 0000000000..23993a812a --- /dev/null +++ b/libs/kiplatform/include/kiplatform/io.h @@ -0,0 +1,43 @@ +/* +* This program source code file is part of KiCad, a free EDA CAD application. +* +* Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors. +* +* This program is free software: you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation, either version 3 of the License, or (at your +* option) any later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program. If not, see . +*/ + +#ifndef KIPLATFORM_IO_H_ +#define KIPLATFORM_IO_H_ + +#include + +class wxString; + +namespace KIPLATFORM +{ +namespace IO +{ + /** + * Opens the file like fopen but sets flags (if available) for sequential read hinting. + * Only use this variant of fopen if the file is truely going to be read sequentially only + * otherwise you may encounter performance penalities. + * + * Windows in particular is a little ulgy to set the sequential scan flag compared + * to say linux and it's posix_fadvise + */ + FILE* SeqFOpen( const wxString& aPath, const wxString& mode ); +} // namespace IO +} // namespace KIPLATFORM + +#endif // KIPLATFORM_IO_H_ \ No newline at end of file diff --git a/libs/kiplatform/msw/io.cpp b/libs/kiplatform/msw/io.cpp new file mode 100644 index 0000000000..7a10c2c068 --- /dev/null +++ b/libs/kiplatform/msw/io.cpp @@ -0,0 +1,68 @@ +/* +* This program source code file is part of KiCad, a free EDA CAD application. +* +* Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors. +* +* This program is free software: you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation, either version 3 of the License, or (at your +* option) any later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program. If not, see . +*/ + +#include + +#include + +#include + + +FILE* KIPLATFORM::IO::SeqFOpen( const wxString& aPath, const wxString& mode ) +{ +#ifdef _MSC_VER + // We need to use the win32 api to setup a file handle with sequential scan flagged + // and pass it up the chain to create a normal FILE stream + HANDLE hFile = INVALID_HANDLE_VALUE; + hFile = CreateFileW( aPath.wc_str(), + GENERIC_READ, + FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_FLAG_SEQUENTIAL_SCAN, + NULL ); + + if (hFile == INVALID_HANDLE_VALUE) + { + return NULL; + } + + int fd = _open_osfhandle( reinterpret_cast( hFile ), 0 ); + + if( fd == -1 ) + { + // close the handle manually as the ownership didnt transfer + CloseHandle( hFile ); + return NULL; + } + + FILE* fp = _fdopen( fd, mode.c_str() ); + + if( !fp ) + { + // close the file descriptor manually as the ownership didnt transfer + _close( fd ); + } + + return fp; +#else + // Fallback for MSYS2 + return wxFopen( aPath, aMode ); +#endif +} \ No newline at end of file diff --git a/libs/kiplatform/osx/io.mm b/libs/kiplatform/osx/io.mm new file mode 100644 index 0000000000..0be0791c82 --- /dev/null +++ b/libs/kiplatform/osx/io.mm @@ -0,0 +1,28 @@ +/* +* This program source code file is part of KiCad, a free EDA CAD application. +* +* Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors. +* +* This program is free software: you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the +* Free Software Foundation, either version 3 of the License, or (at your +* option) any later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License along +* with this program. If not, see . +*/ + +#include + +#include +#include + +FILE* KIPLATFORM::IO::SeqFOpen( const wxString& aPath, const wxString& aMode ) +{ + return wxFopen( aPath, aMode ); +} \ No newline at end of file diff --git a/plugins/3d/vrml/CMakeLists.txt b/plugins/3d/vrml/CMakeLists.txt index dd766e792d..6a44098aa6 100644 --- a/plugins/3d/vrml/CMakeLists.txt +++ b/plugins/3d/vrml/CMakeLists.txt @@ -65,7 +65,7 @@ add_library( s3d_plugin_vrml MODULE x3d/x3d_transform.cpp ) -target_link_libraries( s3d_plugin_vrml kicad_3dsg core ${OPENGL_LIBRARIES} ${wxWidgets_LIBRARIES} ZLIB::ZLIB ) +target_link_libraries( s3d_plugin_vrml kicad_3dsg core ${OPENGL_LIBRARIES} ${wxWidgets_LIBRARIES} kiplatform ZLIB::ZLIB ) target_include_directories( s3d_plugin_vrml PRIVATE $