Skip to content

[CORE] Enable and improve buffered IO in LocalFile to reduce its cost by up to 90% #1299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 18, 2025

Conversation

xezon
Copy link

@xezon xezon commented Jul 17, 2025

This change enables and improves the buffered IO in Game Engine's File System.

The original implementation was from EA, but it was lacking a few things which have been corrected.

Measurements in #1074 have shown that the unbuffered IO can be 10x slower than the buffered IO on game launch.

GameText STR files greatly benefit from the file IO buffer. Win32BIGFileSystem::openArchiveFile does as well with many small reads.

@xezon xezon requested a review from OmniBlade July 17, 2025 16:55
@xezon xezon added Major Severity: Minor < Major < Critical < Blocker Performance Is a performance concern Gen Relates to Generals ZH Relates to Zero Hour labels Jul 17, 2025
Copy link

@OmniBlade OmniBlade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is mostly code written by EA, but I would suggest removing the option to have the class operate in text mode as the original posix API used does not support text mode as far as I understand it and all code using this class will assume binary mode and act accordingly. Any code that needs text mode using stdio directly.

Also the comments clearly said to nuke it after 2003, shame on you for not following instructions :D

@xezon
Copy link
Author

xezon commented Jul 18, 2025

Chat GPT says _O_TEXT does something, but it is Windows specific. I think we can keep it. It also will have a use in terms of line buffering for fopen (in the follow up change).

Chat GPT

The _O_TEXT flag in C is used with the open function provided by the low-level C runtime I/O in Windows (specifically the Microsoft C Runtime Library).

Summary:

  • _O_TEXT tells the open function to open a file in text mode.

  • In text mode, special character translation occurs during I/O:

    • Reading: carriage return–line feed combinations (\r\n) are translated into a single newline character (\n).
    • Writing: newline characters (\n) are translated into carriage return–line feed combinations (\r\n).

Context:

In C on Windows, open() is defined in <fcntl.h> and has this form:

int open(const char *filename, int oflag [, int pmode]);

The oflag can include various flags like:

  • _O_RDONLY, _O_WRONLY, _O_RDWR
  • _O_CREAT, _O_TRUNC, _O_APPEND
  • _O_BINARY, _O_TEXT

Example:

#include <fcntl.h>
#include <io.h>
#include <stdio.h>

int fd = open("example.txt", _O_RDONLY | _O_TEXT);

This opens example.txt in read-only text mode.

Important Notes:

  • _O_TEXT is Windows-specific. On POSIX systems (Linux, macOS), there's no distinction between text and binary mode at the file descriptor level.
  • If you don't specify _O_TEXT or _O_BINARY, the file will be opened in text mode by default in older environments, but being explicit is safer.

Comparison:

  • _O_TEXT = translated line endings
  • _O_BINARY = raw bytes, no translation

@Mauller
Copy link

Mauller commented Jul 18, 2025

In theory this should improve the loading of data from BIG archives as well

@xezon xezon changed the title [CORE] Enable and improve buffered IO in Game Engine's File System to reduce its cost by up to 90% [CORE] Enable and improve buffered IO in LocalFile to reduce its cost by up to 90% Jul 18, 2025
@xezon xezon merged commit 18d8a82 into TheSuperHackers:main Jul 18, 2025
15 checks passed
@xezon xezon deleted the xezon/enable-buffered-io branch July 18, 2025 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Gen Relates to Generals Major Severity: Minor < Major < Critical < Blocker Performance Is a performance concern ZH Relates to Zero Hour
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Raw file reads and writes are unbuffered
3 participants