Skip to content

WIP: Simple Cython implementation#259

Closed
matusvalo wants to merge 8 commits intomasterfrom
cython
Closed

WIP: Simple Cython implementation#259
matusvalo wants to merge 8 commits intomasterfrom
cython

Conversation

@matusvalo
Copy link
Copy Markdown
Member

@matusvalo matusvalo commented Feb 28, 2019

This PR is an alternative to #258.

It consists of 3 parts:

  1. Use __slots__ in GenericContent, since should be more lightweight than regular class
  2. Removes and simplifies parameters of functions in serialization.py - this is prerequisite for cythonizing + this should be faster in cpython 3.7+ [1] [2]
  3. simple cythonization of serialization.py hot paths using pure python mode - this enables two possible ways of using library a) cythonized b) not cythonized

The advantage of this approach is to have one single code base which is already well tested and tuned by users. The disadvantage is that not everything can be converted to "pure" C.

What is pending:

  1. performance tests (should be faster but no tests are done)
  2. There is one single unittests failing (all other are passed) - this needs to be decided if the test is ignored on cython or not
  3. setup.py needs to be refactored to somehow allow user to choose if he wants use cythonized version or pure python
  4. CI must be set to execute tests cythoned and pure python version of library
  5. I have even more aggressive cythonization of serialization.py but it is partialy broken and needs some small adjustments of serialization.py

[1] https://bugs.python.org/issue26110
[2] https://docs.python.org/3.7/whatsnew/3.7.html#cpython-bytecode-changes

@matusvalo
Copy link
Copy Markdown
Member Author

There is only one difference of behavior between cythonized and pure python version of library (as long as I know) - and this is causing the only one unittest failing:

cythonized version of library due translating python function to C function is not raising FrameSyntaxError on some places - e.g.:

raise FrameSyntaxError(
'Unknown value in table: {0!r} ({1!r})'.format(
ftype, type(ftype)))
return val, offset

In cython it is tranlated to:

+141:         raise FrameSyntaxError(
  /*else*/ {
    __Pyx_GetModuleGlobalName(__pyx_t_2, __pyx_n_s_FrameSyntaxError); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 141, __pyx_L1_error)
    __Pyx_GOTREF(__pyx_t_2);
+142:             'Unknown value in table: {0!r} ({1!r})'.format(
+143:                 ftype, type(ftype)))
+144:     return val, offset

But this should not happen when broker is correctly implemented - but in any case cythonized version should not be used with nonstandard/experimental brokers.

@matusvalo
Copy link
Copy Markdown
Member Author

matusvalo commented Feb 28, 2019

Here is cythonization report of serialization.py:

serialization.zip

Here you can see which lines (less yellow, more pure C used) uses pure C (these yields biggest speed improvement). But this can be improved more when we will convert and statically type buffer and format of some functions - e.g.:

def _read_item(buf, offset):

but as I have said needs more changes in library.

Comment thread amqp/basic_message.py Outdated
Comment thread amqp/channel.py Outdated
Comment thread amqp/channel.py Outdated
@matusvalo matusvalo requested a review from thedrow March 5, 2019 09:25
Copy link
Copy Markdown
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

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

the approach and codes look less redical than rust version.

@thedrow
Copy link
Copy Markdown
Contributor

thedrow commented Mar 11, 2019

I still think we should avoid C extensions and unsafe code.
Cython code cannot be shared with CFFI code. The Rust code can be.

@auvipy
Copy link
Copy Markdown
Member

auvipy commented Mar 11, 2019

then we could try this in another fork. celery should be python3 only

@thedrow
Copy link
Copy Markdown
Contributor

thedrow commented Mar 11, 2019

I don't think you understand what I mean.
C extensions are not the right way to go.
Rust is when it is optional.

@auvipy
Copy link
Copy Markdown
Member

auvipy commented Dec 16, 2019

@matusvalo
Copy link
Copy Markdown
Member Author

Also aiohttp is using cython - see:

https://github.com/aio-libs/aiohttp/tree/master/aiohttp

@matusvalo
Copy link
Copy Markdown
Member Author

Closed in favor of #311

@matusvalo matusvalo closed this Mar 30, 2020
@auvipy auvipy deleted the cython branch August 19, 2020 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants