Repository: lucianopaz/compress_pickle

Issue:
Add optional "pickletools.optimize" step to dump
Add optional "pickletools.optimize" step to dump prior to the compression. 
https://docs.python.org/3/library/pickletools.html#pickletools.optimize

This should squeeze some additional load performance and size reduction for minimal effort. 



Discussion:

Patch:
diff --git a/azure-pipelines.yml b/azure-pipelines.yml
index 6fbc25d..6224741 100644
--- a/azure-pipelines.yml
+++ b/azure-pipelines.yml
@@ -91,7 +91,7 @@ stages:
         reportDirectory: "$(System.DefaultWorkingDirectory)/**/htmlcov"
 
     - bash: |
-        bash <(curl -s https://codecov.io/bash)
+        bash <(curl -s https://codecov.io/bash) -n "$(NAME)" -C $BUILD_SOURCEVERSION
       displayName: publish code coverage to CodeCov
       condition: not(startsWith(variables.imageName, 'windows'))
 
diff --git a/compress_pickle/compress_pickle.py b/compress_pickle/compress_pickle.py
index 549164e..3ee9e68 100644
--- a/compress_pickle/compress_pickle.py
+++ b/compress_pickle/compress_pickle.py
@@ -4,6 +4,7 @@ A thin wrapper of standard ``pickle`` with standard compression libraries
 """
 import sys
 import pickle
+import pickletools
 import io
 from .utils import validate_compression, preprocess_path
 
@@ -22,6 +23,7 @@ def dump(
     *,
     unhandled_extensions="raise",
     set_default_extension=True,
+    optimize=False,
     **kwargs
 ):
     r"""Dump the contents of an object to disk, to the supplied path, using a
@@ -32,43 +34,51 @@ def dump(
 
     Parameters
     ----------
-    obj: any
+    obj : any
         The object that will be saved to disk
-    path: str, bytes, PathLike or iostream
+    path : str, bytes, PathLike or iostream
         A path-like object (``str``, ``bytes``, ``os.PathLike``) or a file-like
         object (we call it iostream but it is defined by the ``io`` module, for
         example ``io.BytesIO`` or other types of streams). The path to which
         to dump the ``obj``.
-    compression: None or str (optional)
+    compression : None or str (optional)
         The compression protocol to use. By default, the compression is
         inferred from the path's extension. To see available compression
         protocols refer to
         :func:`~compress_pickle.utils.get_known_compressions`.
-    mode: None or str (optional)
+    mode : None or str (optional)
         Mode with which to open the file buffer. The default changes according
         to the compression protocol. Refer to
         :func:`~compress_pickle.utils.get_compression_write_mode` to
         see the defaults.
-    protocol: int (optional)
+    protocol : int (optional)
         Pickle protocol to use
-    fix_imports: bool (optional)
+    fix_imports : bool (optional)
         If ``fix_imports`` is ``True`` and ``protocol`` is less than 3, pickle
         will try to map the new Python 3 names to the old module names used
         in Python 2, so that the pickle data stream is readable with Python 2.
-    buffer_callback: None or callable (optional)
+    buffer_callback : None or callable (optional)
         Only used in python 3.8. Tells pickle how to serialize buffers.
         Refer to the standard ``pickle`` documentation for details.
-    set_default_extension: bool (optional)
+    set_default_extension : bool (optional)
         If ``True``, the default extension given the provided compression
         protocol is set to the supplied ``path``. Refer to
         :func:`~compress_pickle.utils.set_default_extensions` for
         more information.
-    unhandled_extensions: str (optional)
+    unhandled_extensions : str (optional)
         Specify what to do if the extension is not understood when inferring
         the compression protocol from the provided path. Can be "ignore" (use
         ".pkl"), "warn" (issue warning and use ".pkl") or "raise" (raise a
         ValueError).
-    kwargs:
+    optimize : bool
+        If ``True``, the pickled data is optimized using ``pickletools.optimize``
+        before writing it to the ``path``. This may lead end files that have
+        smaller in size, but this also means that the uncompressed pickled
+        data will first be loaded completely in memory to optimize it and
+        finally write it to the ``path``. Meaning it can produce a
+        ``MemoryError``.
+        
+    kwargs :
         Any extra keyword arguments are passed to the compressed file opening
         protocol. The only exception is the ``compression`` kwarg of the
         ``zipfile`` protocol. This kwarg is called ``zipfile_compression``.
@@ -107,15 +117,27 @@ def dump(
                     fix_imports=fix_imports,
                     **version_dependent_kwargs
                 )
+                if optimize:
+                    buff = pickletools.optimize(buff)
                 arch.writestr(arcname, buff)
             else:
-                pickle.dump(
-                    obj,
-                    io_stream,
-                    protocol=protocol,
-                    fix_imports=fix_imports,
-                    **version_dependent_kwargs
-                )
+                if optimize:
+                    buff = pickle.dumps(
+                        obj,
+                        protocol=protocol,
+                        fix_imports=fix_imports,
+                        **version_dependent_kwargs
+                    )
+                    buff = pickletools.optimize(buff)
+                    io_stream.write(buff)
+                else:
+                    pickle.dump(
+                        obj,
+                        io_stream,
+                        protocol=protocol,
+                        fix_imports=fix_imports,
+                        **version_dependent_kwargs
+                    )
         finally:
             if sys.version_info >= (3, 6):
                 io_stream.flush()
@@ -125,7 +147,17 @@ def dump(
                 arch.close()
     else:
         try:
-            pickle.dump(obj, io_stream, protocol=protocol, fix_imports=fix_imports)
+            if optimize:
+                buff = pickle.dumps(
+                    obj,
+                    protocol=protocol,
+                    fix_imports=fix_imports,
+                    **version_dependent_kwargs
+                )
+                buff = pickletools.optimize(buff)
+                io_stream.write(buff)
+            else:
+                pickle.dump(obj, io_stream, protocol=protocol, fix_imports=fix_imports)
         finally:
             io_stream.flush()
             if must_close:
@@ -133,7 +165,13 @@ def dump(
 
 
 def dumps(
-    obj, compression=None, protocol=-1, fix_imports=True, buffer_callback=None, **kwargs
+    obj,
+    compression=None,
+    protocol=-1,
+    fix_imports=True,
+    buffer_callback=None,
+    optimize=False,
+    **kwargs
 ):
     r"""Dump the contents of an object to a byte string, using a
     given compression protocol.
@@ -159,6 +197,10 @@ def dumps(
     buffer_callback: None or callable (optional)
         Only used in python 3.8. Tells pickle how to serialize buffers.
         Refer to the standard ``pickle`` documentation for details.
+    optimize : bool
+        If ``True``, the pickled data is optimized using ``pickletools.optimize``
+        before compressing it. This will produce a final byte array that has a
+        smaller or equal size than the ``optimze=False`` case.
     kwargs:
         Any extra keyword arguments are passed to the compressed file opening
         protocol. The only exception is the ``compression`` kwarg of the
@@ -175,6 +217,7 @@ def dumps(
             fix_imports=fix_imports,
             buffer_callback=buffer_callback,
             set_default_extension=False,
+            optimize=optimize,
             **kwargs
         )
         return stream.getvalue()
@@ -201,59 +244,59 @@ def load(
 
     Parameters
     ----------
-    path: str, bytes, PathLike or iostream
+    path : str, bytes, PathLike or iostream
         A path-like object (``str``, ``bytes``, ``os.PathLike``) or a file-like
         object (we call it iostream but it is defined by the ``io`` module, for
         example ``io.BytesIO`` or other types of streams). The path from which
         to load the ``obj``.
-    compression: None or str (optional)
+    compression : None or str (optional)
         The compression protocol to use. By default, the compression is
         inferred from the path's extension. To see available compression
         protocols refer to
         :func:`~compress_pickle.utils.get_known_compressions`.
-    mode: None or str (optional)
+    mode : None or str (optional)
         Mode with which to open the file buffer. The default changes according
         to the compression protocol. Refer to
         :func:`~compress_pickle.utils.get_compression_read_mode` to
         see the defaults.
-    fix_imports: bool (optional)
+    fix_imports : bool (optional)
         If ``fix_imports`` is ``True`` and ``protocol`` is less than 3, pickle
         will try to map the new Python 3 names to the old module names used
         in Python 2, so that the pickle data stream is readable with Python 2.
-    encoding: str (optional)
+    encoding : str (optional)
         Tells pickle how to decode 8-bit string instances pickled by Python 2.
         Refer to the standard ``pickle`` documentation for details.
-    errors: str (optional)
+    errors : str (optional)
         Tells pickle how to decode 8-bit string instances pickled by Python 2.
         Refer to the standard ``pickle`` documentation for details.
-    buffers: None or iterable (optional)
+    buffers : None or iterable (optional)
         Only used in python 3.8. Provides pickle with the buffers from which
         to read out of band buffer views.
         Refer to the standard ``pickle`` documentation for details.
-    arcname: None or str (optional)
+    arcname : None or str (optional)
         Only necessary if ``compression="zipfile"``. It is the name of the file
         contained in the zip archive which must be read and decompressed.
         If ``None``, the ``arcname`` is assumed to be ``path`` (when ``path``
         is path-like), ``path.name`` (when ``path`` is file-like and it has a
         name attribute) or "default" when ``path`` has no ``name`` attribute.
-    set_default_extension: bool (optional)
+    set_default_extension : bool (optional)
         If `True`, the default extension given the provided compression
         protocol is set to the supplied `path`. Refer to
         :func:`~compress_pickle.utils.set_default_extensions` for
         more information.
-    unhandled_extensions: str (optional)
+    unhandled_extensions : str (optional)
         Specify what to do if the extension is not understood when inferring
         the compression protocol from the provided path. Can be "ignore" (use
         ".pkl"), "warn" (issue warning and use ".pkl") or "raise" (raise a
         ValueError).
-    kwargs:
+    kwargs :
         Any extra keyword arguments are passed to the compressed file opening
         protocol. The only exception is the ``compression`` kwarg of the
         ``zipfile`` protocol. This kwarg is called ``zipfile_compression``.
 
     Returns
     -------
-    The unpickled object: any
+    The unpickled object : any
 
     Notes
     -----
@@ -325,38 +368,38 @@ def loads(
 
     Parameters
     ----------
-    data: bytes
+    data : bytes
         The bytes that contain the object to load from
-    compression: None or str
+    compression : None or str
         The compression protocol to use. To see available compression
         protocols refer to
         :func:`~compress_pickle.utils.get_known_compressions`.
-    fix_imports: bool (optional)
+    fix_imports : bool (optional)
         If ``fix_imports`` is ``True`` and ``protocol`` is less than 3, pickle
         will try to map the new Python 3 names to the old module names used
         in Python 2, so that the pickle data stream is readable with Python 2.
-    encoding: str (optional)
+    encoding : str (optional)
         Tells pickle how to decode 8-bit string instances pickled by Python 2.
         Refer to the standard ``pickle`` documentation for details.
-    errors: str (optional)
+    errors : str (optional)
         Tells pickle how to decode 8-bit string instances pickled by Python 2.
         Refer to the standard ``pickle`` documentation for details.
-    buffers: None or iterable (optional)
+    buffers : None or iterable (optional)
         Only used in python 3.8. Provides pickle with the buffers from which
         to read out of band buffer views.
         Refer to the standard ``pickle`` documentation for details.
-    arcname: None or str (optional)
+    arcname : None or str (optional)
         Only necessary if ``compression="zipfile"``. It is the name of the file
         contained in the zip archive which must be read and decompressed.
         If ``None``, the ``arcname`` is assumed to be "default".
-    kwargs:
+    kwargs :
         Any extra keyword arguments are passed to the compressed file opening
         protocol. The only exception is the ``compression`` kwarg of the
         ``zipfile`` protocol. This kwarg is called ``zipfile_compression``.
 
     Returns
     -------
-    The unpickled object: any
+    The unpickled object : any
 
     Notes
     -----
diff --git a/compress_pickle/utils.py b/compress_pickle/utils.py
index 1fa4279..9e548d8 100644
--- a/compress_pickle/utils.py
+++ b/compress_pickle/utils.py
@@ -85,7 +85,7 @@ def get_known_compressions():
 
     Returns
     -------
-    compressions: list
+    compressions : list
         List of known compression protocol names.
     """
     return [c for c in _DEFAULT_EXTENSION_MAP]
@@ -97,10 +97,10 @@ def validate_compression(compression, infer_is_valid=True):
 
     Parameters
     ----------
-    compression: str or None
+    compression : str or None
         A compression protocol. To see the known compression protocolos, use
         :func:`~compress_pickle.utils.get_known_compressions`
-    infer_is_valid: bool
+    infer_is_valid : bool
         If ``True``, ``compression="infer"`` is considered a valid compression
         protocol. If ``False``, it is not accepted as a valid compression
         protocol.
@@ -126,7 +126,7 @@ def get_default_compression_mapping():
 
     Returns
     -------
-    compression_map: dict
+    compression_map : dict
         Dictionary that maps known compression protocol names to their default
         file extension.
     """
@@ -139,7 +139,7 @@ def get_compression_write_mode(compression):
 
     Returns
     -------
-    write_mode_map: dict
+    write_mode_map : dict
         Dictionary that maps known compression protocol names to default write
         mode used to open files for
         :func:`~compress_pickle.compress_pickle.dump`.
@@ -160,7 +160,7 @@ def get_compression_read_mode(compression):
 
     Returns
     -------
-    read_mode_map: dict
+    read_mode_map : dict
         Dictionary that maps known compression protocol names to default write
         mode used to open files for
         :func:`~compress_pickle.compress_pickle.load`.
@@ -183,15 +183,15 @@ def set_default_extensions(filename, compression=None):
 
     Parameters
     ----------
-    filename: str
+    filename : str
         The filename to which to set the default extension
-    compression: None or str (optional)
+    compression : None or str (optional)
         A compression protocol. To see the known compression protocolos, use
         :func:`~compress_pickle.utils.get_known_compressions`
 
     Returns
     -------
-    filename: str
+    filename : str
         The filename with the extension set to the default given by the
         compression protocol.
 
@@ -222,16 +222,16 @@ def infer_compression_from_filename(filename, unhandled_extensions="raise"):
 
     Parameters
     ----------
-    filename: str
+    filename : str
         The filename for which to infer the compression protocol
-    unhandled_extensions: str (optional)
+    unhandled_extensions : str (optional)
         Specify what to do if the extension is not understood. Can be
         "ignore" (do nothing), "warn" (issue warning) or "raise" (raise a
         ValueError).
 
     Returns
     -------
-    compression: str
+    compression : str
         The inferred compression protocol's string
 
     Notes
@@ -289,56 +289,56 @@ def preprocess_path(
 
     Parameters
     ----------
-    path: str, bytes, PathLike or iostream
+    path : str, bytes, PathLike or iostream
         A path-like object (``str``, ``bytes``, ``os.PathLike``) or a file-like
         object (we call it iostream but it is defined by the ``io`` module, for
         example ``io.BytesIO`` or other types of streams).
-    mode: str
+    mode : str
         Mode with which to open the file-like stream. If "read", the default
         read mode is automatically assigned from
         :func:`~compress_pickle.utils.get_compression_read_mode`. If "write, the
         default write mode is automatically assigned from
         :func:`~compress_pickle.utils.get_compression_write_mode`.
-    compression: str or None (optional)
+    compression : str or None (optional)
         A compression protocol. To see the known compression protocolos, use
         :func:`~compress_pickle.utils.get_known_compressions`.
-    set_default_extension: bool (optional)
+    set_default_extension : bool (optional)
         If ``True``, the default extension given the provided compression
         protocol is set to the supplied ``path``. Refer to
         :func:`~compress_pickle.utils.set_default_extensions` for
         more information.
-    unhandled_extensions: str (optional)
+    unhandled_extensions : str (optional)
         Specify what to do if the extension is not understood when inferring
         the compression protocol from the provided path-like object. Can be
         "ignore" (use ".pkl"), "warn" (issue warning and use ".pkl") or
         "raise" (raise a ``ValueError``).
-    arcname: None or str (optional)
+    arcname : None or str (optional)
         Only necessary if ``compression="zipfile"``. It is the name of the file
         contained in the zip archive which must be used.
         If ``None``, the ``arcname`` is assumed to be ``path``'s basename (when
         ``path`` is path-like), ``path.name`` (when ``path`` is file-like and
         it has a name attribute) or "default" when ``path`` has no ``name``
         attribute.
-    kwargs:
+    kwargs :
         Any extra keyword arguments are passed to the compressed file opening
         protocol. The only exception is the ``compression`` kwarg of the
         ``zipfile`` protocol. This kwarg is called ``zipfile_compression``.
 
     Returns
     -------
-    io_stream: iostream
+    io_stream : iostream
         The wrapping file-like stream that can be used with ``pickle.dump`` and
         ``pickle.load``
-    arch: None or iostream
+    arch : None or iostream
         If compression is ``"zipfile"``, ``arch`` is the ``ZipFile`` instance
         and ``io_stream`` points to the file from which to read or write inside
         the ``ZipFile`` archive.
-    arcname: str or None
+    arcname : str or None
         Only used on python3.5 for compatibility. Under python3.5, it is the
         name of the file inside the ``ZipFile`` archive from which to read or
         write. It is ``None`` on higher versions of python or when the
         compression isn't ``"zipfile"``.
-    must_close: bool
+    must_close : bool
         A boolean value that indicates whether the ``io_stream`` must be closed
         by the calling function after reading/writing or not.
 
@@ -384,42 +384,42 @@ def open_compression_stream(path, compression, stream, mode, arcname=None, **kwa
 
     Parameters
     ----------
-    path: str or iostream
+    path : str or iostream
         The ``path`` output from :func:`~compress_pickle.utils.preprocess_path`.
-    compression: str or None
+    compression : str or None
         A supported compression protocol. To see the known compression
         protocolos, use :func:`~compress_pickle.utils.get_known_compressions`.
-    stream: iostream
+    stream : iostream
         The ``stream`` output from
         :func:`~compress_pickle.utils.preprocess_path`.
-    arcname: None or str (optional)
+    arcname : None or str (optional)
         Only necessary if ``compression="zipfile"``. It is the name of the file
         contained in the zip archive which must be used.
         If ``None``, the ``arcname`` is assumed to be ``path``'s basename (when
         ``path`` is path-like), ``path.name`` (when ``path`` is file-like and
         it has a name attribute) or "default" when ``path`` has no ``name``
         attribute.
-    kwargs:
+    kwargs :
         Any extra keyword arguments are passed to the compressed file opening
         protocol. The only exception is the ``compression`` kwarg of the
         ``zipfile`` protocol. This kwarg is called ``zipfile_compression``.
 
     Returns
     -------
-    io_stream: iostream
+    io_stream : iostream
         The wrapping file-like stream that can be used with ``pickle.dump`` and
         ``pickle.load``
-    arch: None or iostream
+    arch : None or iostream
         If compression is ``"zipfile"``, ``arch`` is the ``ZipFile`` instance
         and ``io_stream`` points to the file from which to read or write inside
         the ``ZipFile`` archive.
-    arcname: str or None
+    arcname : str or None
         Only used when ``compression="zipfile"``. It is the name of the file
         inside the ``ZipFile`` archive from which to read or write. If an input
         ``arcname`` is supplied and is different than ``None``, it is returned
         here as is. Under other compression protocols, the inputed ``arcname``
         is returned, whether it is ``None`` or not.
-    must_close: bool
+    must_close : bool
         A boolean value that indicates whether the ``io_stream`` must be closed
         by the calling function after reading/writing or not.
 


Test patch:
diff --git a/tests/conftest.py b/tests/conftest.py
index 75f1b29..e8417af 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -8,6 +8,7 @@ from fixtures import (
     set_default_extension,
     file_compressions,
     file_types,
+    optimize,
     compressions_to_validate,
     preprocess_path_on_path_types,
     preprocess_path_on_file_types,
diff --git a/tests/fixtures.py b/tests/fixtures.py
index d5b3ac3..7e2fe29 100644
--- a/tests/fixtures.py
+++ b/tests/fixtures.py
@@ -99,6 +99,11 @@ def file_types(request):
     return request.param
 
 
+@pytest.fixture(scope="module", params=[False, True], ids=str)
+def optimize(request):
+    return request.param
+
+
 @pytest.fixture(
     scope="module",
     params=itertools.product(FILE_COMPRESSIONS + UNHANDLED_COMPRESSIONS, [True, False]),
@@ -160,7 +165,7 @@ def preprocess_path_on_file_types_and_compressions(
 
 
 @pytest.fixture(scope="function")
-def dump_load(file, random_message, file_compressions, set_default_extension):
+def dump_load(file, random_message, file_compressions, set_default_extension, optimize):
     message = random_message
     _file = _stringyfy_path(file).format(file_compressions)
     if isinstance(file, bytes):
@@ -188,6 +193,7 @@ def dump_load(file, random_message, file_compressions, set_default_extension):
         file,
         file_compressions,
         set_default_extension,
+        optimize,
         expected_file,
         expected_fail,
     )
@@ -196,9 +202,9 @@ def dump_load(file, random_message, file_compressions, set_default_extension):
 
 
 @pytest.fixture(scope="function")
-def simple_dump_and_remove(random_message, compressions):
+def simple_dump_and_remove(random_message, compressions, optimize):
     path = "test_dump_vs_dumps_{}".format(compressions)
-    yield (path, compressions, random_message)
+    yield (path, compressions, random_message, optimize)
     os.remove(path)
 
 
diff --git a/tests/test_pickling.py b/tests/test_pickling.py
index 0ac8eee..fcf9a4b 100644
--- a/tests/test_pickling.py
+++ b/tests/test_pickling.py
@@ -34,7 +34,7 @@ def test_load_fails_on_unhandled_compression(wrong_compressions):
 
 @pytest.mark.usefixtures("simple_dump_and_remove")
 def test_dump_compresses(simple_dump_and_remove):
-    path, compression, message = simple_dump_and_remove
+    path, compression, message, optimize = simple_dump_and_remove
     kwargs = dict()
     if compression == "zipfile":
         kwargs = dict(zipfile_compression=zipfile.ZIP_DEFLATED)
@@ -54,6 +54,7 @@ def test_dump_load(dump_load):
         path,
         compression,
         set_default_extension,
+        optimize,
         expected_file,
         expected_fail,
     ) = dump_load
@@ -61,7 +62,11 @@ def test_dump_load(dump_load):
         warnings.simplefilter("ignore", UserWarning)
         if expected_fail is None:
             dump(
-                message, path, compression, set_default_extension=set_default_extension
+                message,
+                path,
+                compression,
+                set_default_extension=set_default_extension,
+                optimize=optimize,
             )
             loaded_message = load(
                 path, compression, set_default_extension=set_default_extension
@@ -74,6 +79,7 @@ def test_dump_load(dump_load):
                     path,
                     compression,
                     set_default_extension=set_default_extension,
+                    optimize=optimize,
                 )
             with pytest.raises(expected_fail):
                 load(path, compression, set_default_extension=set_default_extension)
@@ -87,9 +93,15 @@ def test_dumps_loads(random_message, compressions):
 
 @pytest.mark.usefixtures("simple_dump_and_remove")
 def test_dump_vs_dumps(simple_dump_and_remove):
-    path, compression, message = simple_dump_and_remove
-    dump(message, path, compression=compression, set_default_extension=False)
-    cmp1 = dumps(message, compression=compression, arcname=path)
+    path, compression, message, optimize = simple_dump_and_remove
+    dump(
+        message,
+        path,
+        compression=compression,
+        set_default_extension=False,
+        optimize=optimize,
+    )
+    cmp1 = dumps(message, compression=compression, arcname=path, optimize=optimize)
     with open(path, "rb") as f:
         cmp2 = f.read()
     if compression != "gzip":
@@ -100,18 +112,24 @@ def test_dump_vs_dumps(simple_dump_and_remove):
 
 @pytest.mark.usefixtures("simple_dump_and_remove")
 def test_dump_load_on_filestreams(simple_dump_and_remove):
-    path, compression, message = simple_dump_and_remove
+    path, compression, message, optimize = simple_dump_and_remove
     read_mode = "rb"  # get_compression_read_mode(compression)
     write_mode = "wb"  # get_compression_write_mode(compression)
     with open(path, write_mode) as f:
-        dump(message, f, compression=compression)
+        dump(message, f, compression=compression, optimize=optimize)
     with open(path, read_mode) as f:
         raw_content = f.read()
         f.seek(0)
         loaded_message = load(f, compression=compression)
     assert loaded_message == message
     os.remove(path)
-    dump(message, path, compression=compression, set_default_extension=False)
+    dump(
+        message,
+        path,
+        compression=compression,
+        set_default_extension=False,
+        optimize=optimize,
+    )
     with open(path, read_mode) as f:
         benchmark = f.read()
     # zipfile compression stores the data in a zip archive. The archive then
@@ -123,8 +141,14 @@ def test_dump_load_on_filestreams(simple_dump_and_remove):
 
 @pytest.mark.usefixtures("simple_dump_and_remove")
 def test_load_vs_loads(simple_dump_and_remove):
-    path, compression, message = simple_dump_and_remove
-    dump(message, path, compression=compression, set_default_extension=False)
+    path, compression, message, optimize = simple_dump_and_remove
+    dump(
+        message,
+        path,
+        compression=compression,
+        set_default_extension=False,
+        optimize=optimize,
+    )
     with open(path, "rb") as f:
         data = f.read()
     cmp1 = loads(data, compression=compression, arcname=os.path.basename(path))