Repository navigation
Speedup speedup - #259
Speedup speedup#259martindurant wants to merge 6 commits into
Conversation
|
Nice, glad that was useful! |
|
|
||
| @cython.wraparound(False) | ||
| @cython.boundscheck(False) | ||
| def encode(buf, bint utf8=1): |
There was a problem hiding this comment.
Add a docstring? What are the expected input types for buf?
| n_items = input_values.shape[0] | ||
|
|
||
| # setup intermediates | ||
| encoded_values = <char **>malloc(n_items * sizeof(char *)) |
There was a problem hiding this comment.
You should add a try...finally to ensure free() is called in case of exception (or find another strategy :-)).
| PyBytes_AsStringAndSize(u, &encv, &l) | ||
| encoded_values[i] = encv | ||
| data_length += l + 4 # 4 bytes to store item length | ||
| encoded_lengths[i] = l |
There was a problem hiding this comment.
What if l is larger than 2**31? Should we detect it and raise OverflowError?
| arr = _to_array(inp) | ||
| _check_1d_object_array(arr) | ||
| free(encoded_values) | ||
| return bytes(out) |
There was a problem hiding this comment.
So we're making a copy of the output here? This sounds wasteful. How about either 1) return the bytearray 2) write into a bytes object directly?
There was a problem hiding this comment.
Thrift requires bytes, not bytearray. I thought it was not possible to write to bytes, that they are immutable.
There was a problem hiding this comment.
In C you can write to a bytes object as long as you created it yourself using PyBytes_FromStringAndSize(NULL, size) and haven't shared it yet. It was actually done in the code this PR removes (see array_encode).
| return result | ||
| @cython.wraparound(False) | ||
| @cython.boundscheck(False) | ||
| def decode(bytes buf, int n_items, bint utf8=1): |
| data += l | ||
|
|
||
| return result | ||
| return out |
There was a problem hiding this comment.
Should we raise if data != data_end at this point?
| A list of bytes objects is returned. RuntimeError is raised | ||
| if *raw_bytes* contents don't exactly match *n*. | ||
| """ | ||
| def array_decode(np.ndarray data): |
There was a problem hiding this comment.
Should there be a ndim constraint? Also a docstring?
|
|
||
| val = data[i] | ||
| out[i] = PyUnicode_DecodeUTF8( | ||
| PyBytes_AS_STRING(val), |
There was a problem hiding this comment.
Do you know if Cython will type-check val for being a bytes object here?
|
superceded |
Strings en/decoding speedups inspired by https://github.com/alimanfoo/numcodecs/blob/e94a182e28d47b0e3a8aecd902b9e82f48b720b2/numcodecs/vlen.pyx (see discussion)
Benchmarks (time taken, smaller is better):
branch
utf8: read, fixed: 6 162.019
utf8: read, fixed: None 122.684
utf8: write, fixed: 6 169.497
utf8: write, fixed: 6, nostat 111.139
utf8: write, fixed: None 83.332
utf8: write, fixed: None, nostat 29.057
master:
utf8: read, fixed: 6 174.109
utf8: read, fixed: None 182.614
utf8: write, fixed: 6 148.194
utf8: write, fixed: None 154.619
"nostat" is the new option not to bother creating column metadata statistics.
The most common codepath is UTF8 read, not fixed: 180->120ms, good gain.
cc @alimanfoo