News for hackers. Stuff that matters.
Re:Taking PHP Seriously by Klathmon on Thursday October 13, 2016 @05:09PM (#12704049)
The article points out that arguably the best part of PHP is the "shared nothing lifecycle". That each request starts new, and the process dies at the end of the request. It's by far my favorite part, and I completely agree that it makes "reasoning about" (boy do I hate that phrase...) your program much easier.

Why are there no other "competitors" in this space? Why do most other languages go with the alternative route of using an event loop or daemonizing your "server" to serve up requests? I can understand why Node.js does it (otherwise the async nature would just be an annoyance for no gain), but why haven't there been any other server-side languages that not only work well like this, but actively target it?

It seems like it would be easier for everyone involved. You wouldn't need much of a GC when the process is killed every time, you don't need to worry about async IO or multi-threading or catching errors or any of the other annoyances that come with most of the traditional "event loop" way of doing things.

So what's the catch?

Re:Taking PHP Seriously by tyingq on Thursday October 13, 2016 @05:17PM (#12704108)
>>Why are there no other "competitors" in this space?

There are. Any language that supports either fastcgi, or runs as an apache plugin (or similar) functions in the "each request starts new" space. There's certainly others, but Python and Perl are both good examples.

Starting new with each request does, of course, mean lots of re-work that shouldn't have to be done with each request. It does initially keep complexity down, but at scale, the applications I've worked with have to use a shared cache (like acpu) to avoid that rework and meet performance goals.

Re:Taking PHP Seriously by Klathmon on Thursday October 13, 2016 @05:21PM (#12704143)
I mean more of languages that actually "target" this kind of setup.

I know it can result in a lot of "extra work", but I really feel like most of the pain of that could be abstracted away by the runtime.

Nginx/apache setup a PHP process before the request comes in, so why couldn't you expose that point to your language and let the programming language do work before that point, even "snapshot"ing it to avoid re-doing that work for each request?

It just seems like there are better ways of solving this problem, because an event loop is such a "hammer" solution to the problem.

Re:Taking PHP Seriously by jstimpfle on Thursday October 13, 2016 @05:54PM (#12704374)
I think python has exactly that with WSGI. With this protocol you make a module containg an "application" function object which is called by the server when a request arrives. On module load time you can do any required setup work.

(On my systems, by default there is some reuse of the loaded module, so you can leak state between requests. However it should be possible to avoid reused processes).

Re:Taking PHP Seriously by ryanlm on Thursday October 13, 2016 @11:11PM (#12705921)
Can you set up Python to have the same pattern as PHP? I've always had to proxy the web server to the Python process running HTTP.
Re:Taking PHP Seriously by tyingq on Friday October 14, 2016 @07:25AM (#12707373)
Python via fastcgi, wscgi, etc, is the same pattern. Or mod_python inside Apache.
Re:Taking PHP Seriously by gkya on Saturday October 15, 2016 @06:27AM (#12713479)

  import cgi
Also, the httpd communicates to the CGI program via environment variables so one can also write their program in assembly.
Re:Taking PHP Seriously by dfsegoat on Friday October 14, 2016 @10:05AM (#12708112)
Yeah, I run new django apps right alongside my php5 code. It does definitely take a bit more "muss and fuss" than php5 to configure (mod_wsgi, etc) but is just as easy.

You also need to restart apache or change the wsgi loader file, (or set up explicit hoooks to do so) to get your new code to appear when debugging / after uploading changes. This is where I'd say php is superior (upload file, refresh browser).

Re:Taking PHP Seriously by ubernostrum on Saturday October 15, 2016 @05:45PM (#12715789)
Back in the day, mod_python also supported PSP (Python Server Pages), which was similar to PHP/ASP in that it allowed you to write a file as a mix of HTML and Python code inside delimiting tags.
Re:Taking PHP Seriously by mikey_p on Thursday October 13, 2016 @05:44PM (#12704314)
The downside is literally exactly the same as the upside: you have to bootstrap from literally nothing for every single request. Think about how ActiveRecord in Rails reads from the DB schema to generate it's magic methods, etc. This is usually done on startup or at least cached in memory once it's loaded. This is something that you have to manage more carefully with a PHP app since you can't have anything initialized before a request starts. Code paths, auto-loaders, database connections, config files, etc. All that has to happen over and over for each request. You can cache that stuff, but it's still overhead that isn't central to handling a request.

There's actually work being done to build PHP frameworks that get around this with ReactPHP, ZeroMQ, Photon, etc. Essentially this is what HHVM does as well, by at least keeping your compiled bytecodes in memory (similar to APC/eAccelarator/ZendOptimizer, etc).

See: http://reactphp.org http://www.photon-project.com https://gnugat.github.io/2016/04/13/super-speed-sf-react-php...

Even the idea of an alternative runtime for PHP (ala HHVM) is nothing new: http://quercus.caucho.com

Re:Taking PHP Seriously by bkanber on Thursday October 13, 2016 @07:11PM (#12704951)
> Code paths, auto-loaders, database connections, config files, etc. All that has to happen over and over for each request.

So? If it takes < 1ms, what's the harm? And opcode cache comes out of the box and most PHP DB drivers can use connection pooling.

We serve most of our requests in < 10ms, and we're not even on PHP7 yet.

Re:Taking PHP Seriously by pharrington on Thursday October 13, 2016 @09:27PM (#12705581)
Is machine computation mostly responsible for your revenue? If so, would it make more sense to measure web server responses in terms of cpu cycles and such instead of time?
Re:Taking PHP Seriously by colllectorof on Thursday October 13, 2016 @07:42PM (#12705122)
>Think about how ActiveRecord in Rails reads from the DB schema to generate it's magic methods, etc.

All the magic you ever need can be dynamically created in __get, __set and __call. It works, it works well, and it makes not only reasoning, but also deployment an order of magnitude simpler.

>Code paths, auto-loaders, database connections, config files, etc.

Use a single code path, a single autoloader and a single database connection. Cache the opcodes of stuff you load. Write config files and templates in pure PHP, so they're cached as well. Done.

I've never, ever had problems with bringing PHP page generation time below .1 seconds. Usually it took something like .03 seconds on crappy hardware. And that was without extensive caching of DB results or anything else that's particularly clever.

Re:Taking PHP Seriously by SolarUpNote on Thursday October 13, 2016 @06:56PM (#12704841)
Doesn't Opcache take care of that? Using a large framework isn't really a problem because all of the classes, configuration, etc is all kept in ram.
Re:Taking PHP Seriously by TazeTSchnitzel on Thursday October 13, 2016 @07:18PM (#12705000)
The code itself is held in shared memory, so it doesn't need to be recompiled. However, you still have the application startup time to worry about.
Re:Taking PHP Seriously by toast0 on Thursday October 13, 2016 @06:28PM (#12704656)
> Code paths, auto-loaders, database connections, config files, etc. All that has to happen over and over for each request.

Do you most of those need to happen at all, ever? In my mind, auto-loader is a sign that nobody knows what's being loaded (if they did, they would just require it). Requires should be full pathed (because have you seen what happens when you search the include path). Mysql_pconnect has been doing connection pooling for a long time, mysqli supports it too (so yes, you do need to connect sometimes), anyway, if you're loading a framwork, loading that probably takes longer than connecting to a mysql server in the same colo.

Re:Taking PHP Seriously by dpcx on Thursday October 13, 2016 @07:50PM (#12705162)
IMHO, developer time is more expensive than CPU time; I don't want or need to have to write out all of those requires. That's the job of the autoloader. And if I'm writing my code remotely well enough, each section only uses the things that it needs. Granted, there may be some things that each request get that they don't need with the autoloader. But I can optimize later. "Make it work, make it right, make it fast"
Re:Taking PHP Seriously by Twirrim on Friday October 14, 2016 @12:41PM (#12709262)
> IMHO, developer time is more expensive than CPU time; I don't want or need to have to write out all of those requires.

Every wasted millisecond on the server side matters. It impacts scalability, customer experience (and revenue) etc. You should take every easy win you can get there.

Not doing full includes is just ridiculously lazy. It doesn't take that long to write them out, and your IDE can help you out there. I bet you spend more time on social media during office hours than you'd ever save from not having to type out those import statements.

Re:Taking PHP Seriously by bpicolo on Saturday October 15, 2016 @01:40PM (#12714896)
Except you still need autoloading configured for most libraries in the ecosystem to work, unless you want to manually chain absolutely huge quantities of requires.
Re:Taking PHP Seriously by avisser on Friday October 14, 2016 @10:20AM (#12708227)
> I don't want or need to have to write out all of those requires. That's the job of the autoloader.

Or the IDE. IntelliJ or ReSharper does that for me. And I like dealing with any naming conflicts right then and there, which those tools allow me to do.

Re:Taking PHP Seriously by cagataygurturk on Saturday October 15, 2016 @08:24AM (#12713725)
> IMHO, developer time is more expensive than CPU time

I see that you've never worked on a project where the CPU time needed to handle the traffic with crappy code is much more expensive than developer time to write efficient software.

Re:Taking PHP Seriously by dpcx on Tuesday October 18, 2016 @11:14AM (#12735242)
I have. And one of my first steps after starting to clean things up is to switch to composer and autoload; because then I can start ripping things out much more easily, and still make improvements to both code quality and performance.
Re:Taking PHP Seriously by toast0 on Thursday October 13, 2016 @09:19PM (#12705553)
Users' time is more important than developer time or CPU time.

Burning cpu and maybe i/o looking for code all over the place is going to make it really hard to make it fast later.

Re:Taking PHP Seriously by stephenr on Friday October 14, 2016 @09:40AM (#12707969)
> In my mind, auto-loader is a sign that nobody knows what's being loaded

Or that, you know. There is an extremely powerful language feature and the developers are taking advantage of it.

Re:Taking PHP Seriously by forgottenpass on Friday October 14, 2016 @12:42PM (#12709272)
The downside is literally exactly the same as the upside: you have to bootstrap from literally nothing for every single request.

Well, it depends on how it's implemented, and how pedantic someone gets about what "shared nothing" means.

There is a long history of unix daemons that initialize, listen(), and then fork off a child process whenever a client connects. The child does all the work and the parent goes back to listening. You can do the "up front" work once and then each child can only modify their copy of the data, preventing it from interfering with the next request.

Re:Taking PHP Seriously by weberc2 on Thursday October 13, 2016 @06:22PM (#12704601)
The real question is why does PHP depend on globals or thread locals for request state at all? PHP's "new process per request" is a solution to a problem that PHP created for itself. In other languages, the request handler receives the request state as an argument, so when the request completes there is no state to reset.

> It seems like it would be easier for everyone involved. You wouldn't need much of a GC when the process is killed every time, you don't need to worry about async IO or multi-threading or catching errors or any of the other annoyances that come with most of the traditional "event loop" way of doing things.

Firing up a new process (or even a new thread) for each request is very expensive--much more so than a generational GC. Async IO also mitigates this (why have 10K parked threads consuming memory all your memory if all of them are stalled, waiting for database)? Services that spawn a thread for each request run into the C10K problem--they can only serve 10K simultaneous clients. Regardless of your concurrency model, you can't ignore errors--you still need to dispatch the right status codes and error information (not to mention logging, etc).

You should check out Go. You write concurrent code and the runtime effectively manages a thread pool under the covers so you get all of the performance benefits of async programming with the developer-friendliness of synchronous programming. In the case of a web app, you just need to write your request handlers--the web server manages the request concurrency for you, and the language's runtime manages the parallelism.

Re:Taking PHP Seriously by tyingq on Thursday October 13, 2016 @06:54PM (#12704833)
>>PHP's "new process per request" is a solution to a problem that PHP created for itself

True, but I'm not aware of any notable language that did it differently in the mid 1990's when PHP was rolled out. PHP does not spawn a new OS process per request these days. It does spawn a new interpreter, but not a new OS process. Combined with the now bundled opcache, the re-work per request is limited to the developer's own initialization code if they don't choose to use a user-space shared cache.

Re:Taking PHP Seriously by weberc2 on Thursday October 13, 2016 @07:14PM (#12704969)
> True, but I'm not aware of any notable language that did it differently in the mid 1990's when PHP was rolled out.

The OP's question wasn't scoped to the mid-90s.

> PHP does not spawn a new OS process per request these days. It does spawn a new interpreter, but not a new OS process.

Like I said, the same holds true even for an OS thread. Anyway, if it's not a new OS process, then some sort of GC is needed, contrary to the OP's statement.

> Combined with the now bundled opcache, the re-work per request is limited to the developer's own initialization code if they don't choose to use a user-space shared cache.

Again, these are all solutions to problems PHP invented for itself. Other languages don't require special caching mechanisms; they get by on normal scoping rules and shared memory.

Re:Taking PHP Seriously by codedokode on Thursday October 13, 2016 @07:59PM (#12705220)
I don't think so. You either keep state between requests and therefore get problems like memory leaks (which often happen in large Ruby or NodeJS apps written by not very experienced developers) or you keep no state and have to do initialization on every request.
Re:Taking PHP Seriously by weberc2 on Friday October 14, 2016 @11:05AM (#12708530)
This is only true if your framework depends on global variables to manage request state. If your framework passes request state in as an argument to the request handler, then there is no state to reset. Don't use global variables and you don't have any of these problems.
Re:Taking PHP Seriously by codedokode on Friday October 14, 2016 @01:27PM (#12709655)
How do you know whether other components (objects, modules) do not keep state between requests? Unless you 1) use language with pure stateless functions like Haskell or 2) delete everything after each request you cannot guarantee that. PHP uses latter approach.

With PHP you can see your program as a pure function whose result depends only on arguments. You don't have to care about memory leaks like you don't have to do memory management when you have a GC. And if you want more performance and less convenience you can always opt into running your own web server.

> Don't use global variables

If you have an object that is persisted between requests you already have some state that is saved.

Re:Taking PHP Seriously by weberc2 on Friday October 14, 2016 @02:10PM (#12709993)
Programming 101: scope your objects appropriately. If you have an object whose state is bound to the scope of the request handler, initialize it in that context ("don't use global vars" is just a special case of this rule). In other words, everything initialized outside your request handler's scope should have its state reused across requests. The PHP community never entirely learned this, so you guys are building workarounds. This isn't intended to be PHP bashing; just objective criticism.
Re:Taking PHP Seriously by codedokode on Friday October 14, 2016 @04:47PM (#12710984)
I do not see how scopes are related to a problem of memory leak. Let's say you have a Django middleware that is initialized outside the scope of a request. Let's say you decided to add some stats counter in that middleware and wrote a code like this:

    def __call__(self, request):
        client_ip = request.META['REMOTE_ADDR']
        self.last_visitors.append(client_ip) 
        ...
This code follows your scoping rules but still leaks memory. So it looks like you don't understand why memory leaks occur. I explained somewhere in ancestor comment that to prevent leaks you either need a language with pure functions or reset environment on each request, none of which is possible in Python yet the latter is available for PHP users.

With Django processing model any array can become the source of a memory leak. I have heard that Ruby and NodeJS developers often have to restart their applications every several hours because they even have no tools to find the source of a leak. The problem that does not exist in PHP by design.

Re:Taking PHP Seriously by weberc2 on Monday October 17, 2016 @10:05AM (#12725086)
You're conflating application state with request state. Updating application state based on a request is a perfectly reasonable thing to do, but it necessarily invites the possibility of a memory (or database, file, etc) leak. Either PHP can't update application state at all or it can and necessarily suffers from the same potential to leak memory (I suspect the latter, since an application without side-effects would be worthless).
Re:Taking PHP Seriously by Klathmon on Thursday October 13, 2016 @07:28PM (#12705050)
To clarify, I didn't say that no GC would be needed, just that it can be much less robust, use quicker algorithms, and in many cases is simpler to actually implement.
Re:Taking PHP Seriously by weberc2 on Friday October 14, 2016 @11:13AM (#12708597)
This isn't true. A shared memory server doesn't require special, complex garbage collection algorithms--any generational GC will due, because request state lives in the youngest generation where collection is virtually free.
Re:Taking PHP Seriously by Siyo on Thursday October 13, 2016 @05:19PM (#12704119)
There are plenty of competitors in this space, they just don't use the CGI model of spawning a new _OS_ process for every request. You have Erlang/Elixir that have their own lightweight processes that don't share memory and have independent GC. And you can build similar systems on Java/Go. The difference is that in those language you have a choice. There are certain types of applications that you just can't reasonably make in PHP.
Re:Taking PHP Seriously by sciurus on Thursday October 13, 2016 @05:41PM (#12704294)
"they just don't use the CGI model of spawning a new _OS_ process for every request"

Which isn't what PHP does, fwiw.

Re:Taking PHP Seriously by Siyo on Thursday October 13, 2016 @06:20PM (#12704586)
What do you mean exactly? I mean yeah, it's a bit different with FastCGI when you have a pool of persistent processes. But you still basically boot up your whole application every time a request comes in. Opcode caching helps with loading code, but not execution. And you're still working with OS processes for concurrency, which is not ideal.
Re:Taking PHP Seriously by skarap on Friday October 14, 2016 @02:23AM (#12706424)
In fact you can create a "daemonized" webserver in PHP and handle each request in a special handler function. For async case there is ReactPHP (http://reactphp.org/), don't know any product for sync case, but creating one is certainly possible.

And you can also set up Ruby, Python, Rust, Haskell, Java, etc to create a new interpreter/vm/whatever for every request. All of those languages have libraries to support CGI. Using the same OS process but restarting the interpreter/vm will be a bit more complex, but absolutely possible. You can even implement the "every file is an entry point" approach for at least Python and Ruby.

The difference is what is the intended use for each platform. E.g. creating a new OS process, loading the PHP interpreter, bootstrapping Wordpress, getting some data from the DB and rendering a page is possible under 600ms while creating a new OS process, loading the CPython, bootstrapping Django, getting some data from the DB and rendering a page will easily take you 4 seconds. This is because Django developers didn't expect you'll be doing that, so they put a lot of stuff in the initialization step (system checks, creating db connections, setting up logging, ...).

Re:Taking PHP Seriously by kevingadd on Thursday October 13, 2016 @07:31PM (#12705058)
I worked on a large-scale (30m+ users, tons of concurrent sessions and high request rates) site + online service + native client setup in PHP for years.

This was never actually an asset. Startup times were huge, lack of shared state meant extra requests to database servers (and to memcache instances to avoid database round trips) to get the same data per-request. Each request got longer and a given page or api call might issue many duplicate requests in a short period of time.

Plus since PHP doesn't have viable threading or async (definitely didn't at the time) each memcache or sql request had to be synchronous, resulting in even more wasted time. At the very least memcache had good support for batch operations so we could manually batch our queries.

Overall though this is not an asset. It's only the 'best' part of PHP if you don't want to think about writing good software that manages shared state.

To be fair, many applications don't actually benefit from being good code, so PHP is probably a good fit for those.

Re:Taking PHP Seriously by flushandforget on Thursday October 13, 2016 @05:53PM (#12704368)
Many CGI scripts were coded like this.

Here is an example in C: https://www.cs.tut.fi/~jkorpela/forms/cgic.html

Php was born out of trying to reduce a lot of that cruft and blending code and html output easily. And that's what initially made it a winner.

Re:Taking PHP Seriously by anarazel on Thursday October 13, 2016 @05:16PM (#12704101)
This is my least favorite part. As soon as your program gets more complicated the amount of repeated parsing, compilation and setup you need to do for every request causes massive speed and structural issues.
Re:Taking PHP Seriously by leeoniya on Thursday October 13, 2016 @05:40PM (#12704278)
> the amount of repeated parsing

Which should be near 0 if you can be bothered to enable OPcache in php.ini. Not only parsing/disk reading but also bytecode generation are bypassed.

Re:Taking PHP Seriously by anarazel on Thursday October 13, 2016 @06:05PM (#12704466)
Sure. But that doesn't really address the problem, just makes it smaller. You still need to reinstantiate a lot of runtime state.
Re:Taking PHP Seriously by leeoniya on Thursday October 13, 2016 @06:40PM (#12704747)
I can't tell you how much more "runtime state" exists in PHP than in say Java. In both cases you need to initialize some sort of security-aware context. Once you've eliminated parsing, disk i/o and compilation phases. You should be on pretty even ground of bytecode vs bytecode. Java has a Hotspot JIT, so it will probably win there. I think a JIT is coming to PHP as well since the architectural rework that went into PHP7.
Re:Taking PHP Seriously by bpicolo on Thursday October 13, 2016 @06:48PM (#12704791)
There's more too it than that though, yeah? Say you use a service that uses oauth2 authentication. You need to do a request to pull in a oauth token. If you lose all state between requests, you waste time re-fetching tokens.

In PHP, persisting this sort of thing between requests becomes a painful task instead of a triviality

Re:Taking PHP Seriously by stephenr on Friday October 14, 2016 @09:44AM (#12707991)
> In PHP, persisting this sort of thing between requests becomes a painful task instead of a triviality

apc_fetch/apc_store hardly seems painful.

Re:Taking PHP Seriously by bkanber on Thursday October 13, 2016 @07:15PM (#12704980)
Session cookies were basically invented for transient statefulness over HTTP.
Re:Taking PHP Seriously by bpicolo on Thursday October 13, 2016 @07:22PM (#12705018)
I'm not referring to per-user sorts of things. You can't cache those locally in a sane way in any language. I'm talking about what would usually be memoizable sorts of things.
Re:Taking PHP Seriously by bkanber on Thursday October 13, 2016 @07:46PM (#12705143)
There are memoization libraries for PHP but really it's just another cache implementation. Put it in RAM, put it on disk, put it in memcached or redis, I don't really see too much a difference between $cache->set($key, $value) in PHP vs cache[key] = value in node for instance.
Re:Taking PHP Seriously by leeoniya on Thursday October 13, 2016 @07:09PM (#12704939)
if you need to persist some data/state between requests, php has a mature memcached extension. it's a fast memory store, but i'm not certain of the security implications since i dont think it has any kind of built-in isolation.
Re:Taking PHP Seriously by bpicolo on Thursday October 13, 2016 @07:15PM (#12704978)
Sure, but that means I have to either run a memcached instance on every server (which I've actually had to do before just for the sake of PHP) or I have to make network calls for something I shouldn't have to, and then also do serialization/deserialization. That's fine for plenty of things, but certainly isn't optimal for all sorts of stuff.

Not to mention, all the other languages out there memcached just as well.

Re:Taking PHP Seriously by Klathmon on Thursday October 13, 2016 @07:04PM (#12704904)
But in most languages http requests should be stateless. Even in node or Java you should be hitting the db to pull that info anyway, as sharing the last info in the process leads to scaling issues as well as big problems if the application crashes.

The alternative is crazy sharding systems which are becoming more and more difficult as devices are less and less "static"

Re:Taking PHP Seriously by codedokode on Thursday October 13, 2016 @07:54PM (#12705182)
You can add a ReactPHP based HTTP server to your app to avoid reinitializing, but you'll have to deal with memory leaks between requests and process management yourself.
Re:Taking PHP Seriously by sitharus on Thursday October 13, 2016 @05:19PM (#12704122)
The catch is overhead. There are things you need at the start of each request - global configuration, database connections etc - that other systems do once at startup. PHP has to do this every request.
Re:Taking PHP Seriously by davidw on Thursday October 13, 2016 @05:21PM (#12704137)
> "reasoning about" (boy do I hate that phrase...)

A bit tangential, but I find myself disliking it too, for no good reason that I can think of.

Perhaps it's something of a crutch in that it's really "I like it more, but 'reason' sounds better" ? Not sure that's quite it, either, as in the above case with a "new everything per request", it's certainly true that it's easier to think about what's happening.

Re:Taking PHP Seriously by jandrese on Thursday October 13, 2016 @05:31PM (#12704210)
I like the old hacker term for "reasoning about". Grok. It's much easier to grok a program when everything just runs from top to bottom and doesn't jump to a different third party library every 3 lines or have 15 layers of indirection and frameworks.
Re:Taking PHP Seriously by andreareina on Friday October 14, 2016 @03:31AM (#12706603)
"grok" implies a deep understanding, much more so than "reason about".
Re:Taking PHP Seriously by davidw on Thursday October 13, 2016 @06:05PM (#12704458)
"Grok" is good. Perhaps it's that it sounds less pretentious, somehow?
Re:Taking PHP Seriously by williamgb on Thursday October 13, 2016 @06:39PM (#12704738)
Interesting! I have somehow developed the opposite opinion. The phrase "to reason" is plain English and uncomplicated. Grok, by comparison, is arcane and to the uninitiated is semantically opaque. The latter strikes me as a more suitable vehicle for demonstrating pretension.
Re:Taking PHP Seriously by combatentropy on Thursday October 13, 2016 @07:32PM (#12705067)
"Reason about" is pretentious, and "grok" is jargon. I think the way to say it in plain, everyday English is "understand."
Re:Taking PHP Seriously by davidw on Thursday October 13, 2016 @06:41PM (#12704755)
Fair enough. I never claimed my dislike of "reasoning about" things was rational! 'Grok' definitely has a context, that likely does not include talking to non-hacker types like clients, board members, or whatever.
Re:Taking PHP Seriously by williamgb on Thursday October 13, 2016 @07:16PM (#12704990)
I understand your situation :) I keep a long list of seemingly innocuous words which, through no fault of their own, are repugnant to my ears. It's utterly irrational.
Re:Taking PHP Seriously by VintageCool on Thursday October 13, 2016 @05:45PM (#12704325)
I think that the abstracted language makes it easier to admit that something is difficult to reason about.
Re:Taking PHP Seriously by andreareina on Friday October 14, 2016 @04:06AM (#12706702)
It's a pretty nuanced phrase and difficult to replace. I might make the case that "easy to reason about" <=> "simple" in the sense that Rich Hickey uses it[1] but that doesn't do anything for the verb itself.

The phrase has a high correlation with subjects that are themselves highly correlated with smug proponents; functional programming is one of the greater ones of these.

Personally I like the phrase. Then again I self-identify as a (non-smug) SmugLispWeenie[2] so of course I like it.

[1] https://www.infoq.com/presentations/Simple-Made-Easy [2] http://web.archive.org/web/20160709054130/http://c2.com/cgi/...

Re:Taking PHP Seriously by Nullabillity on Thursday October 13, 2016 @05:20PM (#12704135)
Because it's very wasteful to keep reinterpreting the same page every time, as opposed to just running it in RAM from a warm JIT.

You can get the same reasoning benefits in pretty much any language by just not using any global variables. Yes, you're still running a framework/server with internal state, but with PHP you've got the same "issue" with Apache/Nginx.

Re:Taking PHP Seriously by TazeTSchnitzel on Thursday October 13, 2016 @07:21PM (#12705012)
> Because it's very wasteful to keep reinterpreting the same page every time, as opposed to just running it in RAM from a warm JIT.

You are running it from RAM. PHP's OPcache, and HHVM, will both compile the code only once (well, HHVM will compile it multiple times due to JIT, but it only touches the disk once) and cache it.

Re:Taking PHP Seriously by bkanber on Thursday October 13, 2016 @06:46PM (#12704777)
> very wasteful

Define "very". Because for me it's more like "a few hundred microseconds per request".

Re:Taking PHP Seriously by wvenable on Thursday October 13, 2016 @05:19PM (#12704118)
The startup time can be pretty extreme -- although you can mitigate much of this with various caching strategies but the typical PHP request involves reading dozens to maybe hundreds of files from disk, compiling them into byte code, and then executing that byte code, and then deallocating everything.

And even if you cache everything, you still have the code necessary to setup the runtime environment of you application which runs on every request and does the same thing.

But still, I do actually like that model and it does have many advantages for development. But it's really hard to work in PHP, the language, after using something better.

Re:Taking PHP Seriously by alaaibrahim on Thursday October 13, 2016 @05:41PM (#12704293)
> although you can mitigate much of this with various caching strategies but the typical PHP request involves reading dozens to maybe hundreds of files from disk, compiling them into byte code, and then executing that byte code, and then deallocating everything.

For the conversion to bytecode, that is what apc is for.

Re:Taking PHP Seriously by Jach on Thursday October 13, 2016 @06:56PM (#12704842)
Still often faster than for whatever unholy filter chain of jetty services to get through the stack to the code I care about.
Re:Taking PHP Seriously by awinder on Thursday October 13, 2016 @06:31PM (#12704682)
I may be waaay off base here, but wouldn't amazon's lambda function architecture sort of mirror the benefits of shared-nothing? It doesn't necessarily work like that under the hood, at least if I remember correctly your lambda function may live on past one request, but you must code as if that were the case
Re:Taking PHP Seriously by bkanber on Thursday October 13, 2016 @06:38PM (#12704732)
It does, but it comes with deep tie-in to AWS as well.

But also, you don't typically use lambda to serve pages or a full application, more commonly it's used for specific processing tasks, individual endpoints, and generally code that can run standalone.

Re:Taking PHP Seriously by suneilp on Thursday October 13, 2016 @06:08PM (#12704489)
Starting new, regardless of caching data/bytecode, still means you have to spend time bootstrapping the application.

Other languages, whether directly or indirectly manage cleanup via GC or recycle processes, like UWSGI for Python.

I also don't find any particular benefits over other languages with "reasoning about". As a PHP application grows and a team restructures it into something more mature it ends up looking the same minus the event loop (which just lives somewhere else).

Re:Taking PHP Seriously by pcwalton on Thursday October 13, 2016 @05:39PM (#12704277)
> You wouldn't need much of a GC when the process is killed every time

Yes, you do. You'd be surprised how easily memory balloons out of control unless you regularly run a GC. Log minor collections sometime in Java or JS engines if you don't believe me…

Even worse, if you don't run GC regularly, you will suffer terrible locality as execution continually grows the heap (not to mention the overhead of continually requesting free pages from the OS).

Re:Taking PHP Seriously by nostrademons on Thursday October 13, 2016 @06:18PM (#12704570)
The idea here is that the webserver forks & execs a new process for every request, allocates a bunch of pages, and then runs to completion, at which point the process finishes and the memory is returned to the OS. It's essentially using the OS as an arena, where you can allocate memory at will just by bumping a pointer and then the whole thing is freed at once at the end of the request.

It's not an insane idea - CGI worked exactly like this, as do most command-line shells. And the overhead is often less than most people expect, since most pages are COWed on fork() and the files it touches will likely be in the filesystem cache. Still, I think people found that the overhead from process creation (and in particular, keeping all those processes around & task-switching them while they make DB/network/filesystem calls) was greater than that from GC, and so most frameworks - PHP included, these days! - use a more lightweight mechanism.

Re:Taking PHP Seriously by pcwalton on Thursday October 13, 2016 @06:24PM (#12704620)
Yes, I know what the idea is. I highly suspect it won't be practical due to the reasons given in my other reply. We tried this with JavaScript in a very similar domain and the memory usage was unacceptable.
Re:Taking PHP Seriously by tracker1 on Thursday October 13, 2016 @05:56PM (#12704393)
Sorry, but GP is right here... you really don't need GC if the entire process ends and the memory is completely freed at the end of the request.

Not that I feel cgi is the most efficient way to handle requests... but not needing the complexity of GC could be one advantage of that approach...

Re:Taking PHP Seriously by pcwalton on Thursday October 13, 2016 @06:23PM (#12704607)
It may seem that way at first, but space leaks during individual requests are way too easy otherwise. Think about something as simple as, say, parsing Mustache templates. The parser is probably internally making lots of little substrings to record the names of directives as it comes to them. You would think that this is O(1) in space, since you only need one (or a small number) of strings at a time, but without a GC the space requirements balloon to O(size of input). Being able to dump the memory at the end of a request doesn't help you if you keep blowing out your cache during each one.

I speak from experience: there have been many attempts to mandatorily delay collection until the end of the event loop in browsers, to achieve greater responsiveness. It has always failed. You can add heuristics to try to preferentially collect at the end of the event loop, but you really need to be able to GC anytime.

Re:Taking PHP Seriously by tracker1 on Thursday October 13, 2016 @06:40PM (#12704748)
I'm not completely disagreeing... I've worked on pipeline transformers in node where I exposed gc and ran it after each item in the pipeline was done. It would grow insane without it... likewise I can see that if you're transforming a lot of records for delivery in PHP it could bloat as well.

However, if you're handling requests well within a given memory threshold, it isn't always something you should worry about until it becomes a problem. In which case, you'll probably want to look at something like go, rust, erlang or others to handle improvements, or break off heavy parts into sub/micro-services.

Re:Taking PHP Seriously by captn3m0 on Friday October 14, 2016 @06:25AM (#12707150)
for the specific case of templating: you wouldn't want to parse mustache in production, and use a pre-compiled template.
Re:Taking PHP Seriously by sfrailsdev on Thursday October 13, 2016 @05:19PM (#12704126)
Probably because allocating a thread to every request is less efficient then a bunch of event loops with async. Ideally you do both, but a lot of languages choose to make concurrency something done at the process level and are event driven asynchronous web servers which are deployed as multiple processes, which adds a lot of overhead compared to threads. I think Golang has a nice balance with goroutines, as I understand things.
Re:Taking PHP Seriously by germano on Thursday October 13, 2016 @05:16PM (#12704099)
Elixir and the Phoenix framework work like this, with a process spawned per request.
Re:Taking PHP Seriously by monocasa on Thursday October 13, 2016 @05:39PM (#12704272)
'Process' is an extremely overloaded term. It's more of a green thread for Erlang VM applications.
Re:Taking PHP Seriously by wtetzner on Friday October 14, 2016 @07:54PM (#12711914)
It is a green thread, but Erlang's green threads each have their own heap/garbage collector, so they're not sharing memory.
Re:Taking PHP Seriously by Klathmon on Thursday October 13, 2016 @07:40PM (#12705109)
Ive been hearing nothing but good things about Elixir, I may have to check it out.

Any recommended places to start with?

Re:Taking PHP Seriously by artificial on Friday October 14, 2016 @11:38PM (#12712601)
http://elixirforum.com is a good place to check out. There are a few excellent books out there to help you get started with OTP, which makes Elixir beastly, and Phoenix (a web framework) as well. The forums are an excellent place to find deals on books, too.
Re:Taking PHP Seriously by gkya on Saturday October 15, 2016 @06:49AM (#12713508)
It's interesting that so little know what is Cgi here. It's the Common Gateway Interface and it uses environment variables to communicate to the Cgi scripts and thus the Cgi script can be in whatever language you wish. You don't even need a lib, reading env is enough. Its possible to write your Cgi scripts in assembly.

So this process-per-request thing is not exclusively a Php thing.

Re:Taking PHP Seriously by Klathmon on Saturday October 15, 2016 @09:54AM (#12713916)
I know of CGI scripts (and the newer faster alternatives), I was wondering why more languages don't specifically target that kind of setup, playing to its benefits and trying to minimize the downsides.
Re:Taking PHP Seriously by gkya on Saturday October 15, 2016 @10:11AM (#12713968)
Oh, sorry. I guess it's two-fold then: a) it's not as popular and cool, and then b) not much needs to be done to target it anyways, as most often being a good unix citizen suffices, you get the input and write the output to stdout, what's in between is application realm and whatever you do is up to you.
Re:Taking PHP Seriously by Klathmon on Saturday October 15, 2016 @10:35AM (#12714045)
No worries!

I completely understand the "trendy" part. Sadly even language/frameworks need marketing, and doing things in a "wasteful" and "slower" way but simpler is gonna be a tough sell even if it might be the best move.

I just feel that there is a space for a "better PHP". One that uses a better base language, but still keeps the benefits from the language like the simplicity of the "think edit reload" cycle, the shared nothing architecture that makes programs easier to write and simpler to test, and the horizontal scaling.

Re:Taking PHP Seriously by gkya on Saturday October 15, 2016 @10:55AM (#12714117)
I've not worked on many CGI stuff (other than little things I use myself) but Python has all the housekeeping done. See this other post of mine that has an example of an interactive hello-world: https://news.ycombinator.com/item?id=12712067 . And then, Perl is used for all sorts of CGI stuff. Both have good database support. CGit [1] is a git web interface written in C.

[1] https://git.zx2c4.com/cgit/about/

P.S. An important thing that contributes to the success of PHP is the amount of hosting services that support it.

Re:Taking PHP Seriously by kuschku on Thursday October 13, 2016 @05:17PM (#12704104)
> So what's the catch?

About 2-3 orders of magnitude in performance. That’s the catch.

And it’s the reason why even with PHP you use things as opcaches, cache database results in external daemons, you use process pools with fastcgi instead of actually creating new processes, etc.

And hacking those things on top of PHP make your program even worse to reason about than just using a daemonized system with actor framework.

Re:Taking PHP Seriously by smsm42 on Thursday October 13, 2016 @05:28PM (#12704197)
Unless you're writing in pure hand-optimized assembly language, 3 orders of magnitude in performance is way more than you could claim. PHP web requests commonly execute in hundreds of ms (of course, here I generalize mercilessly, but that covers most of cases I know of and that's what most sites aim for). So you say your non-PHP requests which would do the same thing would finish in hundreds of microseconds? I have very hard time believing it.

> And it’s the reason why even with PHP you use things as opcaches, cache database results in external daemons

You make it sound as if the idea of caching was invented by PHP programmers to address language deficiencies. Nothing can be further from the truth. Everybody uses caches, because it's faster. If you don't use DB cache, there's no way in the universe you can make you app fast under any significant load.

> And hacking those things on top of PHP make your program even worse to reason about

You have very strange view of performance design if you think using caches is "hacking something on top" and has to be avoided.

> than just using a daemonized system with actor framework.

This looks like a complete non-sequitur - you could use actor framework and still use all the things described above, and do it in PHP, and thousands of people do exactly that. On not in PHP, if you'd like - this pattern is completely orthogonal to the language used.

Re:Taking PHP Seriously by losvedir on Thursday October 13, 2016 @06:31PM (#12704679)
> So you say your non-PHP requests which would do the same thing would finish in hundreds of microseconds?

I had a side project written in Elixir/Phoenix make it onto the front page of HN and it indeed was the case that the majority of requests were in hundreds of microseconds. The default log messages actually use "µs" which is kind of cute.

I wrote about it here[0] and extracted a random sample of my logs and you'll see that most of them were in the hundreds of microseconds.

[0] http://gabe.durazo.us/tech/ephemeral-p2p-project/

Re:Taking PHP Seriously by tonyarkles on Thursday October 13, 2016 @09:27PM (#12705578)
Yeah, I giggled with glee the first time I really paid attention to how long my Phoenix requests took.
Re:Taking PHP Seriously by tracker1 on Thursday October 13, 2016 @06:02PM (#12704438)
The system now used to serve GoDaddy's Website Builder content is written in node, deployed to clusters behind load balancers, and from there connecting to Cassandra clusters for the data... iirc, the average turn around time for a given request (including dynamic ones) is less than 12ms from the node server receiving the request to delivering the response, under a load of many millions of requests per second to the system.

Disclosure, I worked at GD in Website Builder when this was being developed, but haven't been there for over 2 years now.

Re:Taking PHP Seriously by kuschku on Friday October 14, 2016 @02:28AM (#12706438)
> So you say your non-PHP requests which would do the same thing would finish in hundreds of microseconds? I have very hard time believing it.

Yes, they do. Phoenix/Elixir is a nice way to get below the millisecond, and even with Java you can for most requests go below a few milliseconds.

And you can obviously handle far more requests at once, as you have all the code already loaded in RAM, and just need to jump into it, instead of reparsing it, or copying it back from a cache.

Re:Taking PHP Seriously by WatchDog on Thursday October 13, 2016 @06:04PM (#12704456)
I think his main issue is that the cache has to operate outside of the application process, adding overhead. Languages with shared state can have much simpler and more efficient caching, no io, no de/serialisation.
Re:Taking PHP Seriously by jandrese on Thursday October 13, 2016 @05:36PM (#12704251)
That 2-3 orders of magnitude difference is in an area that runs pretty quickly however.

People go to a lot of trouble to optimize the front end system, but tie it all to the same database that bottlenecks well before the web server. There are certainly times where PHP or CGI aren't fast enough for the job, but I think people tend to unfairly blame them in situations where the underlying problem is more systematic.

Re:Taking PHP Seriously by merb on Thursday October 13, 2016 @05:39PM (#12704274)
> but tie it all to the same database that bottlenecks well before the web server

that's not true. especially not in the DaaS space.

Re:Taking PHP Seriously by bkanber on Thursday October 13, 2016 @05:47PM (#12704339)
And yet it turns out that those 2-3 orders of magnitude don't really matter for the vast majority of products you could develop. Your DB queries no matter the language are several orders of magnitude slower than the interpreting time.

If you need to process thousands of requests per second per thread then don't use PHP, but that's a rare situation. I wouldn't build RTC in PHP. But web or enterprise applications, which are by far more common, don't really suffer that issue.

And pretty much everyone caches database results...

Re:Taking PHP Seriously by jack9 on Thursday October 13, 2016 @05:56PM (#12704389)
> About 2-3 orders of magnitude in performance

Compared to the other offerings, it's on par or faster (Python and Ruby really have a lot to be desired). You're talking about an actor framework, which is more performant at a cost of using that model and the myriad of issues with managing a more complicated system. The market favors simplicity and less sophisticated developers, in most projects.

Re:Taking PHP Seriously by Shorel on Friday October 14, 2016 @07:44PM (#12711867)
I don't know where do you get your 2-3 orders of magnitude from.

A rewrite I worked on, from PHP to C++, had an improvement of 20 times better performance.

That's only one (1) order of magnitude.

Re:Taking PHP Seriously by kuschku on Saturday October 15, 2016 @08:00AM (#12713666)
From benchmarks such as http://www.techempower.com/benchmarks/#section=data-r11&hw=p... ?

Compare Symfony2 with Spring, for example (~500 vs. ~22'000 responses per second).

Or if you want to only use the raw language itself, php-raw with ulib-postgres (~90'000 vs. ~300'000 responses per second)

And with PHP, the larger the codebase, the slower it always responds, because even with opcaches loading it into RAM is a major bottleneck.

That’s exactly the issue at hand here.

And compare actual performance, for example JSON serialization, that’s not even fair anymore, symfony2 reaches around ~2'000 a second, gemini, the fastest fullstack framework, reaches ~970'000 a second.

Raw PHP reaches still ~170'000, but the fastest non-PHP raw language is still at ~2'200'000 compared to that.

These are actual performance issues that will cost you actual money. Even saving 20% of hardware and server costs can be the difference between your company being cost-effective and dead.

Re:Taking PHP Seriously by grok2 on Thursday October 13, 2016 @05:19PM (#12704128)
Performance/scaling?
Re:Taking PHP Seriously by bkanber on Thursday October 13, 2016 @06:47PM (#12704785)
PHP is perhaps one of the easiest technologies to scale horizontally, because of its statelessness.
Re:Taking PHP Seriously by aikah on Thursday October 13, 2016 @07:55PM (#12705188)
> PHP is perhaps one of the easiest technologies to scale horizontally, because of its statelessness.

PHP is no more stateless than any other language out there. Statelessness is a design choice, not something PHP gives you for free. If your app write things on the local disk or use the default PHP session handler then it is not scalable by default. In order to write code that scales you need to make it scalable, it's not something automatic and certainly not special to PHP.

Re:Taking PHP Seriously by bkanber on Thursday October 13, 2016 @08:08PM (#12705269)
I would argue that PHP is more stateless than Node, for instance. Things that happen in previous requests do not affect following requests in PHP, unless you're explicitly using shared state. That's not the case in node, it isn't the case with most Java HTTP server implementations, and it wasn't the case in Rails back in the day (don't know how it is now).
Re:Taking PHP Seriously by minitech on Thursday October 13, 2016 @08:31PM (#12705357)
Any language is stateless until you explicitly share state; the other languages just make it easier to share state. It’s not a bad design decision – makes them more versatile, or at least more cleanly usable outside of serving webpages – but is something to avoid in similar applications for sure.
Re:Taking PHP Seriously by wtetzner on Friday October 14, 2016 @08:14PM (#12711988)
I'm pretty sure PHP supports sessions. I am missing something?
Re:Taking PHP Seriously by egh5oon on Thursday October 13, 2016 @07:04PM (#12704898)
HA!
Re:Taking PHP Seriously by bkanber on Thursday October 13, 2016 @07:05PM (#12704908)
You should add substance to your comment. If you disagree, say why.
Re:Taking PHP Seriously by di4na on Friday October 14, 2016 @04:11AM (#12706720)
Erlang/Elixir would have a word. :)

The catch are couple.

First Concurrency is _hard_ and relying on the OS scheduler would have been really har don ressource until recently, which is why nearly noone did it.

That used ot be a wart of php, only hidden by Moore's Law and recent work on OS schedulers.

But yeah. Erlang do this since the 80s

Re:Taking PHP Seriously by dom0 on Thursday October 13, 2016 @05:55PM (#12704386)
> I completely agree that it makes "reasoning about" (boy do I hate that phrase...) your program much easier.

Actually I have never seen that this was a problem in practice with the application server model. I therefore don't think this is a relevant argument.

Re:Taking PHP Seriously by aikah on Thursday October 13, 2016 @07:47PM (#12705150)
> Why are there no other "competitors" in this space?

You can do the exact same thing with any language that supports CGI. You can compile a Go binary, put it on an Apache server and you'll get the "shared nothing lifecycle" just like PHP.

Re:Taking PHP Seriously by SideburnsOfDoom on Friday October 14, 2016 @07:44AM (#12707456)
> you don't need to worry about async IO or multi-threading

Isn't there a downside to that? If you want to do multi-threading, you can't just do `ThreadPool.StartNew(() => doSomething())`. You're SOL.

Re:Taking PHP Seriously by antjanus on Thursday October 13, 2016 @08:23PM (#12705329)
Now hang on, doesn't Go do that?
Re:Taking PHP Seriously by Kenji on Thursday October 13, 2016 @05:20PM (#12704132)
Exactly. The "shared nothing lifecycle" really lets the programmer (knowingly or unknowingly) embrace RESTful design. Too bad the language doesn't have strict typing like Java. Otherwise it'd be even more convenient and safer in my eyes.
Re:Taking PHP Seriously by kuschku on Thursday October 13, 2016 @05:24PM (#12704166)
PHP 7 actually has strict typing like java, but no generics.

It’s really interesting, last time I worked with PHP was a few years ago, then recently I wrote something in PHP 7, and it is a lot safer.

(But still no compile time type checking because there is no compile time).