public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Bastien Bouclet <bastien.bouclet@gmail.com>
To: newlib@sourceware.org
Subject: Re: [PATCH] newlib: fix fseek optimization with SEEK_CUR
Date: Wed, 13 Nov 2019 20:36:00 -0000	[thread overview]
Message-ID: <CAAPuaS2saCvOo3QGf7mBb9nv8btkWYpTVwd1=r413WQymDMnzw@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1218 bytes --]

Hi Corinna,

Thank you for your answer.

> I checked this against upstream BSD versions.  OpenBSD and NetBSD
> operate like our code, including the flush, while FreeBSD uses its
> internal ftello and never flushed since the repository import back in
> 1994.

One difference I've noticed is that fflush does not invalidate the
stream read buffer in the BSD versions of libc.  In newlib this was
introduced in commit a8ef755c2776b8da4ea386360c1df74ce268c165.  Which
is probably why OpenBSD and NetBSD can call fflush in fseek with
SEEK_CUR.

> Can we be sure this works as desired on append streams as well?

Regarding the append streams, it's worth noting there is another call
to fflush at the beginning of fseek in that case.  I've written a small
test program to verify they did not regress in simple cases.

> Also, given that this is changing very basic code, nobody is unaffected.

I would like to see the performance issue fixed one way or another.
The systems I target do not have a page cache, the extra reads have a
noticeable impact on user experience.  Another other option could be
having a compile time option for disabling the code in fflush that
forces a disk access on the next read.

Regards,
Bastien

[-- Attachment #2: stdio-append.c --]
[-- Type: text/x-csrc, Size: 1559 bytes --]

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
	FILE *fp = fopen("test.txt", "a+");
	if (fp == NULL) {
		puts("failed to open test file in append mode");
		exit(1);
	}

	size_t written = fwrite("1234567890", 10, 1, fp);
	if (written != 1) {
		puts("failed to write");
		exit(1);
	}

	int ret = fseek(fp, -8, SEEK_CUR);
	if (ret == -1) {
		puts("failed to seek");
		exit(1);
	}

	char read_buf[100];
	memset(read_buf, 0, sizeof(read_buf));

	size_t read = fread(read_buf, 2, 1, fp);
	if (read != 1) {
		printf("failed to read %d\n", errno);
		exit(1);
	}

	if (strcmp(read_buf, "34") != 0) {
		printf("unexpected read value %s\n", read_buf);
		exit(1);
	}

	ret = ungetc('a', fp);
	if (ret != 'a') {
		puts("failed to ungetc");
		exit(1);
	}

	ret = fseek(fp, 3, SEEK_CUR);
	if (ret == -1) {
		puts("failed to seek");
		exit(1);
	}

	read = fread(read_buf, 2, 1, fp);
	if (read != 1) {
		printf("failed to read %d\n", errno);
		exit(1);
	}

	if (strcmp(read_buf, "78") != 0) {
		printf("unexpected read value %s\n", read_buf);
		exit(1);
	}

	written = fwrite("0987654321", 10, 1, fp);
	if (written != 1) {
		puts("failed to write");
		exit(1);
	}

	ret = fseek(fp, -20, SEEK_CUR);
	if (ret == -1) {
		puts("failed to seek");
		exit(1);
	}

	read = fread(read_buf, 20, 1, fp);
	if (read != 1) {
		printf("failed to read %d\n", errno);
		exit(1);
	}

	if (strcmp(read_buf, "12345678900987654321") != 0) {
		printf("unexpected read value %s\n", read_buf);
		exit(1);
	}

	fclose(fp);

	puts("success");

	return 0;
}

             reply	other threads:[~2019-11-13 20:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-13 20:36 Bastien Bouclet [this message]
  -- strict thread matches above, loose matches on Subject: below --
2019-11-09 16:28 Bastien Bouclet
2019-11-13 10:15 ` Corinna Vinschen
2019-11-18 10:11 ` Corinna Vinschen
2020-01-29 18:02   ` Corinna Vinschen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAAPuaS2saCvOo3QGf7mBb9nv8btkWYpTVwd1=r413WQymDMnzw@mail.gmail.com' \
    --to=bastien.bouclet@gmail.com \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).