peachpiecompiler / peachpie

PeachPie - the PHP compiler and runtime for .NET and .NET Core
https://www.peachpie.io
Apache License 2.0
2.32k stars 202 forks source link

Compiling github.com/pmmp/PocketMine-MP to .NET #905

Open mclauncherlinux opened 3 years ago

mclauncherlinux commented 3 years ago

Do you think it would improve the softwares performance? (Compiling it to net core) https://github.com/pmmp/PocketMine-MP

jakubmisek commented 3 years ago

The code is well written and strongly typed. It might benefit from the compilation and .NET runtime.

However, it requires a few extensions that are not (yet) implemented in PeachPie - pthreads, yaml, ctype. This would have to be implemented in C#.

dktapps commented 3 years ago

Have fun trying to implement pthreads ...

(P.S. if you value your sanity, I don't advise it)

jakubmisek commented 3 years ago

@dktapps thank you :)) still pthreads extension seems to be pretty straightforward since we already have .NET Thread and ThreadPool classes, and Monitor, and all the stuff?

dktapps commented 3 years ago

@jakubmisek php-pthreads has a lot of magic behaviour which isn't obvious to the uninformed user, which PocketMine has to use and workaround, so I think it might not be a straightforward task to implement it.

A lot of its problems stem from the fact that PHP is anti-user-threading by design, so pthreads has a lot of limitations which you wouldn't expect. These limitations would be OK if they were obvious, but php-pthreads has a lot of magical unintuitive behaviour that tries to work around these limitations (for example, it coerces arrays into Volatile objects when assigned as fields of Threaded objects, it serializes non-Threaded objects (essentially copying them), etc etc).

It also has a very poorly designed API (it has a class Threaded which serves as a map, queue, "array", mutex, condvar, and thread-safe base class all in one).

I'm working on my own fork of pthreads which cleans up most of this magical insanity, but it's still a work in progress.

jakubmisek commented 3 years ago

@dktapps Thanks for the insights, makes sense and sounds like most of the PHP. I guess there is no "internal" documentation for that other than the pthreads source code itself?

At least PocketMine has well-written tests that can verify and reveal any misbehavior.

We can try to implement a required subset of the API in C#, and verify it against available tests. Also, most of the hacks like volatility stuff are actually not needed, because PeachPie is built thread-aware, on top of managed language with garbage collection, etc. which might help.

dktapps commented 3 years ago

@jakubmisek there is some (pretty bad, and old) documentation on the PHP manual, but the manual doesn't talk about all the bear traps. Most of the weird stuff you have to find out for yourself.

I'm working on a fork of pthreads which PocketMine will migrate to in the future, which will eliminate the magic behaviour and make it easier to reason about. At that point I figure it would be pretty trivial to implement such an API in PeachPie.

I just raised concerns because PocketMine implicitly depends on magic behaviour of pthreads that is very hard to get right. I can't see a world in which PocketMine running on current pthreads would be practical to run on anything else.

As for the test suite: it's kinda limited and probably won't tell you that much about the threading aspects of things.

(For context, I'm the current primary maintainer of PocketMine.)

sounds like most of the PHP.

I couldn't have said it better myself. But this specific corner is like PHP x100.

jakubmisek commented 3 years ago

I have noticed what great work you did on PocketMine and all the related projects, and I'm taking your advice gratefully. We'll watch the progress and see if it will be "sane" to start working on pthreads.

dktapps commented 2 years ago

Q about this. Does peachpie provide any kind of threading API or binding for C# threading? I wonder if it might end up being worthwhile to just port PM to PeachPie directly and make use of .NET threading features, but I'd need something to start with as a baseline so I could start testing. Without some kind of threading API it's a no-go.

My comments earlier in the year were founded on the idea of having PM compatible with both PHP (with threading extension) and PeachPie at the same time, but I just started to wonder if it might be an idea to ditch PHP entirely and just use PeachPie alone - threading on PHP limits us too much regardless of the API used because Zend isn't designed for it.

jakubmisek commented 2 years ago

@dktapps since PeachPie is a language on top of .NET, there is all the .NET Threading API available. No specific PeachPie functions for threading tho.

When implementing such a library, PeachPie abstracts the PHP types such as callbacks, so they can be converted to typed .NET delegates in both ways.

I'm not sure how the Zend engine deals with threading (both TS and NTS), so I suppose when a new thread is created under Zend, it gets a copy of the current application state (globals, declared functions, classes, etc..)? We'd have to add a similar API for PeachPie's Context

dktapps commented 2 years ago

The only model of threading that ZE supports is a thread per request model. The memory manager is designed such that requests running in different threads cannot share anything with each other, which is great for scope isolation of web requests, but bad for userland threading, since it actively gets in the way of that. That's why pthreads sucks so much. Everything has to be copied - classes, functions, objects, arrays, strings, etc.

My thought was that if PeachPie were to facilitate userland threading via .NET, it wouldn't matter as much about being compatible with pthreads if threading were possible at all, since pthreads is an evil that the world would be better off without anyway. A model where stuff is actually possible to share between threads would be much better than the cancer that I have to deal with now.

jakubmisek commented 2 years ago

that's correct; immutable objects and user objects can be shared between threads. Only the underlying Context with some raw tables should be copied, because there is no thread safety

dktapps commented 1 year ago

Hi all,

Since PocketMine-MP 5.0.0, it's now using pmmp/ext-pmmpthread, an extension forked from pthreads with a redesigned API.

This API should, in theory, be much easier to implement in peachpie than pthreads. All the APIs are documented in the .stub.php files here: https://github.com/pmmp/ext-pmmpthread/tree/fork/stubs All of the old limitations still apply, but the old magical headache behaviour is all removed.

I don't currently have the spare time to explore implementing this myself, but if someone fancies spending some time on it and has questions, I'll be happy to help.

However, modern PM also depends on several custom extensions, so it will take more work to get it running on peachpie than I originally thought at the time of writing this issue. A lot has changed in 2 years.

Thanks for this awesome project!