Inline fast case for random access file byte/word/dword read

This commit is contained in:
Jonathan G Rennison
2021-05-30 11:24:43 +01:00
parent 054c820521
commit f212332359
2 changed files with 45 additions and 10 deletions

View File

@@ -97,7 +97,7 @@ void RandomAccessFile::SeekTo(size_t pos, int mode)
* Read a byte from the file. * Read a byte from the file.
* @return Read byte. * @return Read byte.
*/ */
byte RandomAccessFile::ReadByte() byte RandomAccessFile::ReadByteIntl()
{ {
if (this->buffer == this->buffer_end) { if (this->buffer == this->buffer_end) {
this->buffer = this->buffer_start; this->buffer = this->buffer_start;
@@ -114,20 +114,20 @@ byte RandomAccessFile::ReadByte()
* Read a word (16 bits) from the file (in low endian format). * Read a word (16 bits) from the file (in low endian format).
* @return Read word. * @return Read word.
*/ */
uint16 RandomAccessFile::ReadWord() uint16 RandomAccessFile::ReadWordIntl()
{ {
byte b = this->ReadByte(); byte b = this->ReadByteIntl();
return (this->ReadByte() << 8) | b; return (this->ReadByteIntl() << 8) | b;
} }
/** /**
* Read a double word (32 bits) from the file (in low endian format). * Read a double word (32 bits) from the file (in low endian format).
* @return Read word. * @return Read word.
*/ */
uint32 RandomAccessFile::ReadDword() uint32 RandomAccessFile::ReadDwordIntl()
{ {
uint b = this->ReadWord(); uint b = this->ReadWordIntl();
return (this->ReadWord() << 16) | b; return (this->ReadWordIntl() << 16) | b;
} }
/** /**

View File

@@ -11,6 +11,7 @@
#define RANDOM_ACCESS_FILE_TYPE_H #define RANDOM_ACCESS_FILE_TYPE_H
#include "fileio_type.h" #include "fileio_type.h"
#include "core/endian_func.hpp"
#include <string> #include <string>
/** /**
@@ -34,6 +35,10 @@ class RandomAccessFile {
byte *buffer_end; ///< Last valid byte of buffer. byte *buffer_end; ///< Last valid byte of buffer.
byte buffer_start[BUFFER_SIZE]; ///< Local buffer when read from file. byte buffer_start[BUFFER_SIZE]; ///< Local buffer when read from file.
byte ReadByteIntl();
uint16 ReadWordIntl();
uint32 ReadDwordIntl();
public: public:
RandomAccessFile(const std::string &filename, Subdirectory subdir); RandomAccessFile(const std::string &filename, Subdirectory subdir);
RandomAccessFile(const RandomAccessFile&) = delete; RandomAccessFile(const RandomAccessFile&) = delete;
@@ -47,9 +52,39 @@ public:
size_t GetPos() const; size_t GetPos() const;
void SeekTo(size_t pos, int mode); void SeekTo(size_t pos, int mode);
byte ReadByte(); inline byte ReadByte()
uint16 ReadWord(); {
uint32 ReadDword(); if (likely(this->buffer != this->buffer_end)) return *this->buffer++;
return this->ReadByteIntl();
}
inline uint16 ReadWord()
{
if (likely(this->buffer + 1 < this->buffer_end)) {
#if OTTD_ALIGNMENT == 0
uint16 x = FROM_LE16(*((const unaligned_uint16*) this->buffer));
#else
uint16 x = ((uint16)this->buffer[1] << 8) | this->buffer[0];
#endif
this->buffer += 2;
return x;
}
return this->ReadWordIntl();
}
inline uint32 ReadDword()
{
if (likely(this->buffer + 3 < this->buffer_end)) {
#if OTTD_ALIGNMENT == 0
uint32 x = FROM_LE32(*((const unaligned_uint32*) this->buffer));
#else
uint32 x = ((uint32)this->buffer[3] << 24) | ((uint32)this->buffer[2] << 16) | ((uint32)this->buffer[1] << 8) | this->buffer[0];
#endif
this->buffer += 4;
return x;
}
return this->ReadDwordIntl();
}
void ReadBlock(void *ptr, size_t size); void ReadBlock(void *ptr, size_t size);
void SkipBytes(int n); void SkipBytes(int n);