martinpaljak/M2Crypto — pull request #16 by nanki (2014-03-12 08:34:24+00:00)
Title: use BIO_new_mem_buf for read-only BIO.

Reading a large MemoryBuffer is very slow.

```
$ python memread.py 1MB.txt
MemoryBuffer: 0, buffer: 0 time: 0.0000
MemoryBuffer: 0, buffer: 10000 time: 0.0006
MemoryBuffer: 0, buffer: 1000 time: 0.0020
MemoryBuffer: 0, buffer: 100 time: 0.0121
MemoryBuffer: 1, buffer: 0 time: 0.0005
MemoryBuffer: 1, buffer: 10000 time: 0.0252
MemoryBuffer: 1, buffer: 1000 time: 0.2381
MemoryBuffer: 1, buffer: 100 time: 2.4232
```

We should use BIO_new_mem_buf for read-only BIOs as described in
http://www.openssl.org/docs/crypto/BIO_s_mem.html

```python
import sys
import time
import M2Crypto

for mem in [False, True]:
for bufsize in [0, 10000, 1000, 100]:
if mem:
d = M2Crypto.BIO.MemoryBuffer(open(sys.argv[1], 'rb').read())
else:
d = M2Crypto.BIO.openfile(sys.argv[1], 'rb')
start = time.time()
while d.read(bufsize):pass
print "MemoryBuffer: %d, buffer: %7d time: %7.04f" % (int(mem), bufsize, time.time() - start)
```