Skip to content

Speedup speedup - #259

Closed
martindurant wants to merge 6 commits into
dask:mainfrom
martindurant:speedup_speedup
Closed

martindurant wants to merge 6 commits into
dask:mainfrom
martindurant:speedup_speedup

Conversation

@martindurant

@martindurant martindurant commented Dec 7, 2017 •

Copy link
Copy Markdown
Member

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

@alimanfoo

Copy link
Copy Markdown

Nice, glad that was useful!

Comment thread fastparquet/speedups.pyx

@cython.wraparound(False)
@cython.boundscheck(False)
def encode(buf, bint utf8=1):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a docstring? What are the expected input types for buf?

Comment thread fastparquet/speedups.pyx Outdated
n_items = input_values.shape[0]

# setup intermediates
encoded_values = <char **>malloc(n_items * sizeof(char *))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a try...finally to ensure free() is called in case of exception (or find another strategy :-)).

Comment thread fastparquet/speedups.pyx Outdated
PyBytes_AsStringAndSize(u, &encv, &l)
encoded_values[i] = encv
data_length += l + 4 # 4 bytes to store item length
encoded_lengths[i] = l

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if l is larger than 2**31? Should we detect it and raise OverflowError?

Comment thread fastparquet/speedups.pyx Outdated
arr = _to_array(inp)
_check_1d_object_array(arr)
free(encoded_values)
return bytes(out)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thrift requires bytes, not bytearray. I thought it was not possible to write to bytes, that they are immutable.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread fastparquet/speedups.pyx
return result
@cython.wraparound(False)
@cython.boundscheck(False)
def decode(bytes buf, int n_items, bint utf8=1):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add docstring?

Comment thread fastparquet/speedups.pyx
data += l

return result
return out

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we raise if data != data_end at this point?

Comment thread fastparquet/speedups.pyx
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):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a ndim constraint? Also a docstring?

Comment thread fastparquet/speedups.pyx

val = data[i]
out[i] = PyUnicode_DecodeUTF8(
PyBytes_AS_STRING(val),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if Cython will type-check val for being a bytes object here?

@martindurant

Copy link
Copy Markdown
Member Author

superceded

@martindurant
martindurant deleted the speedup_speedup branch April 19, 2021 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants