Fokko commented on code in PR #6532:
URL: https://github.com/apache/iceberg/pull/6532#discussion_r1064193045


##########
python/pyiceberg/avro/decoder.py:
##########
@@ -48,13 +48,18 @@ def read(self, n: int) -> bytes:
         """
         if n < 0:
             raise ValueError(f"Requested {n} bytes to read, expected positive 
integer.")
-        read_bytes = self._input_stream.read(n)
-        read_len = len(read_bytes)
-        if read_len <= 0:
-            raise EOFError
-        elif read_len != n:
-            raise ValueError(f"Read {len(read_bytes)} bytes, expected {n} 
bytes")
-        return read_bytes
+        data = b""
+
+        n_remaining = n
+        while n_remaining > 0:
+            data_read = self._input_stream.read(n_remaining)
+            read_len = len(data_read)
+            if read_len <= 0:
+                raise EOFError(f"Got negative length: {read_len}")
+            data += data_read

Review Comment:
   I went with the list approach. It is slightly slower than the bytearray 
approach, but in our case we'll have to convert the `bytearray` back to bytes 
which also introduces a penalty:
   
   ```
   ```



##########
python/pyiceberg/avro/decoder.py:
##########
@@ -48,13 +48,18 @@ def read(self, n: int) -> bytes:
         """
         if n < 0:
             raise ValueError(f"Requested {n} bytes to read, expected positive 
integer.")
-        read_bytes = self._input_stream.read(n)
-        read_len = len(read_bytes)
-        if read_len <= 0:
-            raise EOFError
-        elif read_len != n:
-            raise ValueError(f"Read {len(read_bytes)} bytes, expected {n} 
bytes")
-        return read_bytes
+        data = b""
+
+        n_remaining = n
+        while n_remaining > 0:
+            data_read = self._input_stream.read(n_remaining)
+            read_len = len(data_read)
+            if read_len <= 0:
+                raise EOFError(f"Got negative length: {read_len}")
+            data += data_read

Review Comment:
   I went with the list approach. It is slightly slower than the `bytearray` 
approach, but in our case, we'll have to convert the `bytearray` back to bytes 
which also introduces a penalty:
   
   ```
   ➜  python git:(fd-fix-eof) ✗ python3
   Python 3.10.9 (main, Dec 15 2022, 17:11:09) [Clang 14.0.0 
(clang-1400.0.29.202)] on darwin
   Type "help", "copyright", "credits" or "license" for more information.
   >>> h1 = """
   ... ret = bytearray()
   ... for i in range(2**10):
   ...     ret += b'a' * 2**10
   ... ret
   ... """
   >>> 
   >>> h2 = """
   ... ret = bytearray()
   ... for i in range(2**10):
   ...     ret += b'a' * 2**10
   ... bytes(ret)
   ... """
   >>> 
   >>> g = """
   ... ret = list()
   ... for i in range(2**10):
   ...     ret.append(b'a' * 2**10)
   ... b''.join(ret)
   ... """
   >>> 
   >>> import timeit
   >>> print(timeit.timeit(stmt=h1, number=100000))
   4.586242791032419
   >>> print(timeit.timeit(stmt=h2, number=100000))
   6.316140374983661
   >>> print(timeit.timeit(stmt=g, number=100000))
   4.913256458006799
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to