@@ -58,14 +58,14 @@ def getall(self):
5858
5959
6060class SChunk (blosc2_ext .SChunk ):
61- def __init__ (self , chunksize = 2 ** 24 , data = None , ** kwargs ):
61+ def __init__ (self , chunksize = None , data = None , ** kwargs ):
6262 """Create a new super-chunk.
6363
6464 Parameters
6565 ----------
6666 chunksize: int
6767 The size, in bytes, of the chunks from the super-chunk. If not provided,
68- it is set to 16 MB .
68+ it is set automatically to a reasonable value .
6969
7070 data: bytes-like object, optional
7171 The data to be split into different chunks of size :paramref:`chunksize`.
@@ -99,13 +99,34 @@ def __init__(self, chunksize=2 ** 24, data=None, **kwargs):
9999 >>> storage = {"contiguous": True, "cparams": {}, "dparams": {}}
100100 >>> schunk = blosc2.SChunk(**storage)
101101 """
102- if kwargs is not None :
103- # This a private param to get an SChunk from a blosc2_schunk*
104- sc = kwargs .pop ("schunk" , None )
105- self .urlpath = kwargs .get ("urlpath" , None )
106- else :
107- self .urlpath = None
108- sc = None
102+ self .urlpath = kwargs .get ("urlpath" , None )
103+ if 'contiguous' not in kwargs :
104+ # Make contiguous true for disk, else sparse (for in-memory performance)
105+ kwargs ['contiguous' ] = False if self .urlpath is None else True
106+
107+ # This a private param to get an SChunk from a blosc2_schunk*
108+ sc = kwargs .pop ("schunk" , None )
109+
110+ # If not passed, set a sensible typesize
111+ if data is not None and hasattr (data , "itemsize" ):
112+ if 'cparams' in kwargs and 'typesize' not in kwargs ['cparams' ]:
113+ cparams = kwargs .pop ('cparams' ).copy ()
114+ cparams ['typesize' ] = data .itemsize
115+ kwargs ['cparams' ] = cparams
116+ elif 'typesize' not in kwargs :
117+ kwargs ['typesize' ] = data .itemsize
118+
119+ # chunksize handling
120+ if chunksize is None :
121+ chunksize = 2 ** 24
122+ if data is not None :
123+ chunksize = data .size * data .itemsize
124+ # Make that a multiple of typesize
125+ chunksize = chunksize // data .itemsize * data .itemsize
126+ # Use a cap of 256 MB (most of the modern machines should have this RAM available)
127+ if chunksize > 2 ** 28 :
128+ chunksize = 2 ** 28
129+
109130 super (SChunk , self ).__init__ (schunk = sc , chunksize = chunksize , data = data , ** kwargs )
110131 self .vlmeta = vlmeta (super (SChunk , self ).c_schunk , self .urlpath , self .mode )
111132
0 commit comments