Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recreate uvloop, aiofiles and fetch using CFFI or HPy #18

Open
cirospaciari opened this issue Nov 4, 2022 · 8 comments
Open

Recreate uvloop, aiofiles and fetch using CFFI or HPy #18

cirospaciari opened this issue Nov 4, 2022 · 8 comments
Labels
enhancement New feature or request

Comments

@cirospaciari
Copy link
Owner

uvloop dont support windows, this library have libuv static linked on windows so... we can support windows with no problem

@cirospaciari cirospaciari added the enhancement New feature or request label Nov 4, 2022
@cirospaciari
Copy link
Owner Author

cirospaciari commented Nov 5, 2022

Asyncio support for now is a mess see #11, works using workarounds instead of an fully integration with libuv
uvloop the most used integration with libuv and asyncio don't support PyPy and Windows
Recreating this library using HPy #16 or CFFI is an good alternative, we already have libuv library partially integrated to create the workarounds

Windows status for uvloop: MagicStack/uvloop#14
PyPy status for uvloop: MagicStack/uvloop#32
HPy status for cython (uvloop uses cython): cython/cython#4197

Also aiofiles or aiofile is really slow in PyPy3 using libuv for io should solve this issue.

The performance hit with the current asyncio solution is 50% (not acceptable)

@cirospaciari cirospaciari changed the title Recreate uvloop using CFFI or HPy Recreate uvloop and aiofiles using CFFI or HPy Nov 5, 2022
@cirospaciari
Copy link
Owner Author

cirospaciari commented Nov 6, 2022

Removing asyncio workarounds we get a performance boost from 770k to 880k req/s still much lower than 1.3 million req/s, i will do some performance checks in older version but at first seems that having a bigger Response and Request object with more functions is impacting performance (but we need all this features), so we will integrate all features first, measure the impact again, integrate your own aiofiles and fetch API with libuv and after this i will start an "uvloop" like project to fully integrate asyncio with libuv, removing the workarounds. Giving it at least 12% to 30% more raw performance.

Adding Http3 will improve much more performance than replacing the workaround right now, and its way less effort for some one like me that is working only a few hours per week in this project (until i get support).

Migration to HPy in future will bring Python3 performance up a lot.
If uvloop gets HPy first we will add windows support and pre build libraries to uvloop as weel in an fork and attempt an PR to an official release of uvloop with windows and PyPy3 support.

@cirospaciari cirospaciari changed the title Recreate uvloop and aiofiles using CFFI or HPy Recreate uvloop, aiofiles and fetch using CFFI or HPy Nov 6, 2022
@cirospaciari
Copy link
Owner Author

cirospaciari commented Nov 6, 2022

Compiling an static version of curl for cross platform async http requests with libuv using an similar interfacee that fetch API
https://github.com/curl/curl/blob/master/docs/examples/multi-uv.c
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

import json
from socketify import fetch

options = { 
 'method': 'POST', 
 'headers': {
    'Content-Type': 'application/json',
  },
  'body': json.dumps(data).encode("utf-8")
}
response = await fetch("https://example.com/profile", options)
print(response.json())

response = await fetch("http://example.com/movies.json")
print(response.text())

@cirospaciari
Copy link
Owner Author

Implementing an experimental, SelectorEventLoop for macOS or Linux is the next step

@cirospaciari
Copy link
Owner Author

cirospaciari commented Dec 18, 2022

implement SelectorEventLoop using libuv and HPy and also check if rewriting the whole asyncio will give the async performance boost that we need. Custom Task in HPy will be needed too

target 900k req/s with CPython async
target 1.5 req/s with PyPy async

Today numbers:
249k req/s with CPython async
644k req/s with PyPy async

We need HPy or Cython to compete with japronto and vibora in CPython, in PyPy we are already faster in sync and async than japronto. Neither japronto or vibora supports PyPy because of uvloop dependency. CFFI really hurts performance in CPython.

wrk -H "Host: tfb-server" -H "Accept: text/plain,text/html;q=0.9,application/xhtml+xml;q=0.9,application/xml;q=0.8,/;q=0.7" -H "Connection: keep-alive" --latency -d 5 -c 4096 --timeout 8 -t 8 http://localhost:8000/ -s pipeline.lua -- 16

@wedobetter
Copy link

wedobetter commented Feb 24, 2023

Screenshot 2023-02-24 at 14 45 33

I am getting some serious slow down with async request handlers:

pypy latest 3.9 Darwin arm64
socketify: 0.0.16
strawberry 0.159.0

To avoid drivers, libraries, cffi and other stuff, I have tested this with a GQL query that just returns an empty list and I am using the graphiql helper in the docs.
Just by toggling the handler sync / async, I am getting:

1 socketify process benchmarks

SYNC performance: ~5ms
ASYNC performance: ~1600ms

Is this a known problem?

Update:
I have tried to replicate this on Linux / X86_64
async is ok on CPython, but degraded on PyPy + async

@cirospaciari
Copy link
Owner Author

cirospaciari commented Feb 24, 2023

We use native async in both PyPy and CPython only socketify itself uses libuv internally (only in C/C++ not in python)
This slowdown is probably something in the strawberry library I will look at it, and see if I can optimize it. Please send me the code snippet of a minimal reproduction example so that I can test and optimize for this scenario. This issue is not related to #18 so I will create a new issue to investigate strawberry's async performance.

@cirospaciari
Copy link
Owner Author

Here is the issue tracking the strawberry problem:
#104

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants