Skip to content

size_t cast cleanup - #6982

Merged
MichaelChirico merged 4 commits into
Rdatatable:masterfrom
badasahog:devBranch13
May 20, 2025
Merged

MichaelChirico merged 4 commits into
Rdatatable:masterfrom
badasahog:devBranch13

Conversation

@badasahog

Copy link
Copy Markdown
Contributor

There are a lot of casts to/from size_t that didn't need to exist.

In addition, size_t was often used as a shorthand for int64_t which not only inappropriate, but potentially wasteful because size_t is unsigned, which means that overflow wrapping would slip through the sanitizer.

There are probably other examples of this, which I've left out

@badasahog
badasahog requested a review from aitap as a code owner May 14, 2025 08:23
Comment thread src/fread.c Resolved
@badasahog

Copy link
Copy Markdown
Contributor Author

Is this pr dead?

@MichaelChirico

Copy link
Copy Markdown
Member

personally, I'm a bit under the weather. appreciate your patience.

@aitap aitap left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Most changes are fine, but let's at least commit to use native-sized offsets in the new code and keep fixed-size integers for pre-mmap file sizes and MSVCRT compatibility kludges.

Comment thread src/fread.c Outdated
Comment on lines +200 to +207
static const char* strlim(const char *ch, int64_t limit) {
static char buf[1002];
static int flip = 0;
char *ptr = buf + 501 * flip;
flip = 1 - flip;
char *ch2 = ptr;
if (limit>500) limit=500;
size_t width = 0;
limit = imin(limit, 500);
int64_t width = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you please explain again why this these two types need to be changed from size_t to int64_t? Isn't it the size of space to be taken in the internal buffer, buf? On a 32-bit system it would be definitely wasteful.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@badasahog please weigh in here; we can address in follow-up

Comment thread src/fread.c Outdated
// own page (4k) of the final column, hence 1000 rows of the smallest type (4 byte int) is just
// under 4096 to leave space for R's header + malloc's header.
size_t chunkBytes = umax((size_t)(1000*meanLineLen), 1ULL/*MB*/ *1024*1024);
int64_t chunkBytes = umax((uint64_t)(1000*meanLineLen), 1ULL/*MB*/ *1024*1024);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is used to construct offsets into the memory-mapped file. It should be a native-sized integer, not a fixed-size integer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@badasahog please weigh in here; we can address in follow-up

@MichaelChirico

Copy link
Copy Markdown
Member

hi @badasahog there are still pending review comments for response from you

Comment thread src/fread.c
} else {
ch = (jump == nJumps-1) ? eof - (size_t)(0.5*jump0size) : // to almost-surely sample the last line
pos + (size_t)jump*((size_t)(eof-pos)/(size_t)(nJumps-1));
ch = (jump == nJumps-1) ? eof - (ptrdiff_t)(0.5*jump0size) : // to almost-surely sample the last line

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for the improvements! Would you recommend rewriting (ptrdiff_t)(0.5*jump0size) to jump0size/2 and avoiding the floating point operation and the cast altogether? They do get compiled differently, even at -O2.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@badasahog please weigh in here; we can address in follow-up

@MichaelChirico MichaelChirico left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM in current form. Thanks! Let's continue improvements on the other items in follow-up PRs.

@MichaelChirico
MichaelChirico merged commit 6f05e4a into Rdatatable:master May 20, 2025
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.

3 participants