Skip to content

improve file read#6942

Merged
waruqi merged 2 commits intodevfrom
io
Oct 19, 2025
Merged

improve file read#6942
waruqi merged 2 commits intodevfrom
io

Conversation

@waruqi
Copy link
Member

@waruqi waruqi commented Oct 18, 2025

  • Before adding new features and new modules, please go to issues to submit the relevant feature description first.
  • Write good commit messages and use the same coding conventions as the rest of the project.
  • Please commit code to dev branch and we will merge into master branch in feature
  • Ensure your edited codes with four spaces instead of TAB.

  • 增加新特性和新模块之前,请先到issues提交相关特性说明,经过讨论评估确认后,再进行相应的代码提交,避免做无用工作。
  • 编写友好可读的提交信息,并使用与工程代码相同的代码规范,代码请用4个空格字符代替tab缩进。
  • 请提交代码到dev分支,如果通过,我们会在特定时间合并到master分支上。
  • 为了规范化提交日志的格式,commit消息,不要用中文,请用英文描述。

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @waruqi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on improving the efficiency of file reading operations within the xmake I/O module. The primary goal is achieved by increasing the buffer size used for both peeking into streams and reading entire files, and by transitioning to a dynamically managed buffer for full file reads. These changes are expected to enhance performance by reducing the number of I/O calls and optimizing memory handling during file access.

Highlights

  • Increased I/O Buffer Size: The default block size for stream peeking and reading operations has been increased tenfold by introducing a new macro, XM_IO_BLOCK_MAXN, which is set to TB_STREAM_BLOCK_MAXN * 10.
  • Dynamic Buffer Allocation for File Reads: The xm_io_file_read_all_directly function now utilizes a dynamically allocated buffer managed by file->rcache for reading file content, replacing the previous stack-allocated fixed-size array. This buffer also uses the new, larger XM_IO_BLOCK_MAXN size.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request aims to improve file reading performance by increasing the I/O buffer size. The change from a stack-allocated buffer to a heap-allocated one is a good improvement to prevent potential stack overflows with the larger buffer. However, I've found a critical issue where a memory allocation failure is not handled correctly, which could lead to a crash in production environments. I've provided a code suggestion to fix this. Additionally, I've made a suggestion to improve code maintainability by documenting a magic number.

xm_io_return_error(lua, "init buffer failed!");

tb_byte_t* data = tb_buffer_resize(&file->rcache, XM_IO_BLOCK_MAXN);
tb_assert(data);
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

tb_assert is generally disabled in release builds and should not be used for handling recoverable errors like memory allocation failure. If tb_buffer_resize returns NULL (e.g. due to out-of-memory), data will be NULL, and the subsequent call to tb_stream_read will cause a crash. This should be handled with an explicit check to ensure stability in production environments.

    if (!data)
    {
        tb_buffer_exit(&buf);
        xm_io_return_error(lua, "out of memory: failed to resize read cache");
    }

/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
#define XM_IO_BLOCK_MAXN (TB_STREAM_BLOCK_MAXN * 10)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The magic number 10 is used here to increase the block size. To improve maintainability, it would be beneficial to add a comment explaining the rationale for this specific multiplier. For instance, clarifying the base size of TB_STREAM_BLOCK_MAXN and the target size of XM_IO_BLOCK_MAXN would provide valuable context for future developers.

@waruqi waruqi added this to the v3.0.5 milestone Oct 18, 2025
@waruqi waruqi merged commit b703ec3 into dev Oct 19, 2025
41 of 45 checks passed
@waruqi waruqi deleted the io branch October 19, 2025 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant