Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PHP silently optimize consecutive fseek-commands into one fseek command?

I am running Windows 7 - 64 bit, with the latest XAMPP version that has a 32-bit PHP version.

On testing http://php.net/manual/en/function.fseek.php#112647 for a very big file (bigger than PHP_MAX_INT 2147483647) I'm now pretty sure, that the consecutively following fseeks are summed up before being executed on the filepointer.

I have two questions:

  1. Could I break up this summing up with reasonable means (or only with the workaround mentioned in the link above)?

  2. Is this aggregation happening in PHP (as I assume, though I don't know where in PHP) or in Windows 7?

Answering myself: Trying two workarounds with multiple seeks didn't work on my system. Instead they put the filepointer to different positions at under PHP_MAX_INT. (32-bit PHP only can seek up to PHP_MAX_INT + 8192. Reading from there on is still possible, but I don't know how far.)

Therefore the question is obsolete for my specific case, as 32-bit PHP only can seek up to PHP_MAX_INT + 8192, whatever you do. I leave the question, because two people voted it up, and might be interested in a general answer.

I filed a bug report here:
https://bugs.php.net/bug.php?id=69213
Result: With a 64-bit PHP build it might work, but I didn't try it.

like image 399
John Avatar asked Mar 10 '15 12:03

John


1 Answers

It doesn't. It actually does something even dumber. Here's a snippet from the PHP source code:

      switch(whence) {
          case SEEK_CUR:
              offset = stream->position + offset;
              whence = SEEK_SET;
              break;
      }

This is in the guts of the implementation for PHP's fseek. What's happening here is: if you tell PHP to seek from the current position, it translates that to an "equivalent" seek from the start of the file. This only works when that offset computation doesn't overflow; if it does, well, offset is a signed integer, so that's undefined behavior.

And, okay, this is there because PHP buffers streams internally, so they need to do something. But it doesn't have to be this.

You're probably best off trying to do your work in a language that actually does what you tell it to.

like image 69
Alex Avatar answered Sep 29 '22 01:09

Alex