Git Product home page Git Product logo

Comments (19)

beef9999 avatar beef9999 commented on September 28, 2024

Photon will not switch threads unless your code specifically asks to, by calling thread_yield or thread_sleep.
The underlay implementation of lock and synchronization is thread_sleep, as well as network IO and file IO, and other integrated tools.

If you need to protect some thing that might be changed by other threads, and your code might yield CPU by internally calling some sleep, you should use locks.

from photonlibos.

kosav7r avatar kosav7r commented on September 28, 2024

I am using the Coro20.h, could std::suspend_alwaysalso be used to yield?

Could you also give some details about Spinlock? Would it yield as well, or is it a busy wait?

from photonlibos.

beef9999 avatar beef9999 commented on September 28, 2024

coro20.h is experimental and less performant, you should not use that.

Spinlock does not yield, its code is small, you can read the thread.h source.

Your single vCPU program should probably not use spinlock

from photonlibos.

kosav7r avatar kosav7r commented on September 28, 2024

Is there any plan to improve coro20? That style is programming has been getting very popular recently. Is there any benchmark or some ballpark estimate how less performant it is?

from photonlibos.

beef9999 avatar beef9999 commented on September 28, 2024

Why the traditional multithreaded programming paradigm is less popular?

from photonlibos.

kosav7r avatar kosav7r commented on September 28, 2024

I never said it’s less popular, I am saying it’s been getting traction. Using co_* calls and task chaining is a lot simpler than the thread API of photon.

from photonlibos.

beef9999 avatar beef9999 commented on September 28, 2024

There are many open source frameworks on C++20 coroutines, but we failed to find any competitors in terms of performance. Gaps are huge. You can run their echo server example

from photonlibos.

kosav7r avatar kosav7r commented on September 28, 2024

Agree, photon seems like a very well-organized and targeted library. However, I would consider supporting co_* syntax would be just abatraction around what's already achieved in this library. Is there any reasoning to understand better, why coro20 would be less performant?

from photonlibos.

beef9999 avatar beef9999 commented on September 28, 2024

Agree, photon seems like a very well-organized and targeted library. However, I would consider supporting co_* syntax would be just abatraction around what's already achieved in this library. Is there any reasoning to understand better, why coro20 would be less performant?

@Coldwings Hear what the author says.

from photonlibos.

lihuiba avatar lihuiba commented on September 28, 2024

@kosav7r Stackless coroutine, including the one provided by C++20 and coro20.h, has fundamental issues in performance when you have multiple levels of invocations, i.e. function A calls function B, which further calls function C, etc. Stackless coroutine incurs a high overhead to the calling chain. We provide coro20.h only because it's getting popular.

from photonlibos.

lihuiba avatar lihuiba commented on September 28, 2024

Can't multiple threads(coroutines) cause race conditions

Yes, it's still possible, if you cause context switching in a logic block. But it's less likely and more deterministic than kernel threads.

from photonlibos.

lihuiba avatar lihuiba commented on September 28, 2024

by calling thread_yield or thread_sleep.

directly or indirectly.

Note that, other functions can internally invoke thread_yield or thread_sleep, such as socket send/recv, or file read/write, etc.

from photonlibos.

lihuiba avatar lihuiba commented on September 28, 2024

give some details about Spinlock

Coroutines in a single vCPU should usually avoid spinlock, because there's no chance for another coroutine to lease the lock in the case of contention. This is effectively a dead lock.

from photonlibos.

kosav7r avatar kosav7r commented on September 28, 2024

@kosav7r Stackless coroutine, including the one provided by C++20 and coro20.h, has fundamental issues in performance when you have multiple levels of invocations, i.e. function A calls function B, which further calls function C, etc. Stackless coroutine incurs a high overhead to the calling chain. We provide coro20.h only because it's getting popular.

Thank you for your response. Sorry If I sound like a broken record. Is there a quantifiable estimate or benchmark? How much less performance is expected? Would it be still faster than libraries like libcoro or seastar. We switched our programming model to coroutines at work and saw a huge performance improvement. However, it's not only that, everybody became so productive because the traditional way of doing things with callbacks eventually becomes a callback hell.

For example; photon::thread is great, lightweight fiber/coroutine but implementing a simple, let's say “read_from_disk” involves thinking about callbacks. Whereas, in coroutine API, it's as simple as

task<content> read(){bunch of co_awaits with co_return}

Simple to read, less code. What do you think?

from photonlibos.

lihuiba avatar lihuiba commented on September 28, 2024

We have only a micro benchmark by solve the puzzle of Hanoi. The callback-style code is:

void Hanoi(char n, char from, char to, char aux, Callback cb) {
    if (n==0) return;
    Hanoi(n-1, from, aux, to);
    cb(n, from, to); // move the n-th disk
    Hanoi(n-1, aux, to, from);
}

Each callback gives out a single move.

The relative time costs of C++20 stackless coroutine, C++23 stackless std::generator, and Boost.Context (stackful) are as follow (normalized to the callback version):
image

This benchmarks mimic the real life situation of multiple levels of function (coroutine) invocation, while avoiding the effects of real I/O.

from photonlibos.

lihuiba avatar lihuiba commented on September 28, 2024

photon::thread ... involves thinking about callbacks

No, usually you don't need to think about callbacks with photon. That's one of the most attractive parts of stackful coroutine.

from photonlibos.

kosav7r avatar kosav7r commented on September 28, 2024

What's the most preferred way to return a value from the photon thread upon completion without blocking?

Would you pass a promise and get a future, or continue with the callback style approach?

Simple example; read from a database. With c++20 style coroutine, that's

coro<content> read(string key)

from photonlibos.

lihuiba avatar lihuiba commented on September 28, 2024

We usually use shared variable(s) and a semaphore to coordinate foreground and background threads. And I believe the promise/future paradigm is more fit for general cases.

from photonlibos.

kosav7r avatar kosav7r commented on September 28, 2024

Thank you for the PR! That’s a great step.

As someone coming from different async approaches(different languages), mostly following reactive programming style, I find using thread, shared variables, and a semaphore a difficult approach to solving problems.

Unfortunately, C++ didn't have any standardization in the scope, so I see callbacks or shared variables are common patterns in the C++ world.

Writing async pipelines is extremely simplified if we can have async compositions like coroutines or Future. I hope you will consider this a lot more. I am open to discussions.

from photonlibos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.