[Python-ideas] Specify number of items to allocate for array.array() constructor

Antoine Pitrou solipsis at pitrou.net
Thu Jul 21 00:02:37 CEST 2011


Hello,

> Is there a reason why there the array.array constructor does not allow
> to simply specify the number of items that should be allocated? (I do
> not really care about the contents.)
> Would this be a worthwhile addition to / modification of the array module?

I think it would be. Rather than an additional constructor parameter,
perhaps it could be a separate method (like we already have extend(),
fromfile(), etc.).

In the meantime, on 3.x you should be able to use the following trick,
at least under modern Linux / Unix systems:

>>> f = open("/dev/zero", "rb")
>>> m = mmap.mmap(f.fileno(), 2*1024*1024*1024, access=mmap.ACCESS_READ)
    # I'm putting 2GB for the sake of example. Since the mmap is
    # created over /dev/zero, kernel optimizations should avoid any
    # physical RAM consumption (virtual memory aka address space
    # will still be used, of course).
>>> a = array.array("i")
>>> a.frombytes(m)
>>> len(a)
536870912
>>> a[0]
0
>>> a[-1]
0
>>> m.close(); f.close()
    # This releases any virtual memory used by the mmap object


Regards

Antoine.





More information about the Python-ideas mailing list