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
This commit is contained in:
Marek Roszko 2022-08-19 20:01:36 -04:00
parent df3560ab8e
commit dd8dc4e513
7 changed files with 179 additions and 2 deletions

View File

@ -26,6 +26,7 @@
#include <cstdarg>
#include <config.h> // HAVE_FGETC_NOLOCK
#include <kiplatform/io.h>
#include <core/ignore.h>
#include <richio.h>
#include <errno.h>
@ -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 )
{

View File

@ -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
)

View File

@ -0,0 +1,34 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2023 Mark Roszko <mark.roszko@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <kiplatform/io.h>
#include <wx/crt.h>
#include <wx/string.h>
#include <fcntl.h>
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;
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef KIPLATFORM_IO_H_
#define KIPLATFORM_IO_H_
#include <stdio.h>
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_

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <kiplatform/io.h>
#include <wx/string.h>
#include <windows.h>
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<intptr_t>( 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
}

28
libs/kiplatform/osx/io.mm Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <kiplatform/io.h>
#include <wx/crt.h>
#include <wx/string.h>
FILE* KIPLATFORM::IO::SeqFOpen( const wxString& aPath, const wxString& aMode )
{
return wxFopen( aPath, aMode );
}

View File

@ -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
$<TARGET_PROPERTY:gzip-hpp,INTERFACE_INCLUDE_DIRECTORIES>