Skip to content

py/stream.c: Implemented truncate(). #11500

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

DavidEGrayson
Copy link
Contributor

@DavidEGrayson DavidEGrayson commented May 15, 2023

This fixes issue #4775.

Signed-off-by: David Grayson <davidegrayson@gmail.com>
@github-actions
Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64: +1948 +0.244% standard[incl +104(data)]
      stm32:  +516 +0.131% PYBV10
        rp2:  +520 +0.160% PICO

@dpgeorge dpgeorge added py-core Relates to py/ directory in source extmod Relates to extmod/ directory in source labels May 16, 2023
@dpgeorge
Copy link
Member

Thanks for the contribution.

As you can see above from the code size report, it's quite a large increase in code size. More than I would have expected since the change is not that large. Do you think there's any way to optimise further for size?

@DavidEGrayson
Copy link
Contributor Author

DavidEGrayson commented May 16, 2023

The majority of the code size change is due to the firmware now including file truncation library routines for FAT and LFS which were previously garbage-collected by the linker as unused functions.

Here are the results of some experiments where I measured the size of the RP2 port firmware with FAT and MSC enabled. For the third row, I added return 0; at the top of f_truncate, and for the fourth row, I added that line to both f_truncate and lfs2_file_truncate.

RP2+FAT+MSC size Change from above
master commit 3229791 323396
This PR, ec18976 323956 +560
empty f_truncate 323756 -200
empty lfs2_file_truncate 323580 -176

So 67% of the code space increase comes from those library routines.

I think truncate support should be an optional feature, and by default only be enabled if MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES is 1.

Perhaps the code size could be improved a little by changing f_truncate to take the new file size as an argument instead of just assuming we want to truncate at the stream position. The place where we call f_truncate would get much simpler, and several references to fp->fptr inside the function would get replaced by a local variable.

By the way, this PR currently uses f_lseek to expand a FAT file, and it seems like that results in unspecified bytes being added to the end of the file. If we improve f_truncate, we could make sure that only zeros are added when the file is expanded.

@projectgus
Copy link
Contributor

This is an automated heads-up that we've just merged a Pull Request
that removes the STATIC macro from MicroPython's C API.

See #13763

A search suggests this PR might apply the STATIC macro to some C code. If it
does, then next time you rebase the PR (or merge from master) then you should
please replace all the STATIC keywords with static.

Although this is an automated message, feel free to @-reply to me directly if
you have any questions about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extmod Relates to extmod/ directory in source py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants