Top
Best
New

Posted by prophittcorey 8 hours ago

A Modern Web App in C(prophitt.me)
358 points | 156 commentspage 5
andrepd 3 hours ago|
Instantly reminded me of this: source code of the first big Portuguese www portal and search engine: https://github.com/celso/sapo
so-cal-schemer 4 hours ago||
Who needs rust or javascript anymore when we can write all our code in C again and put an agentic zero-day hunter in the toolchain?

"Make no mistakes."

Or a comfy scripting language with a great FFI as X intended.

paularmstrong 8 hours ago||
> Everything about marketyour.games is modern

I can't read any of the text on that page other than the headings. Is that what makes it modern? I assume the stack mentioned in the blog post is vibe-slop as well.

prophittcorey 7 hours ago||
I wanted to try my hands at a monochromatic website and chose two shades that were far enough apart to pass a AAA contrast rating for accessibility. I'll try to improve it soon.

It was 100% human slopped :)

mrweasel 7 hours ago||
My eyes aren't particularly bad, not at the point where I need glasses to read books, program or anything, but I'm at 160-170% zoom before it's comfortable to read. HN is the same... so it might just be me.
blodorn 3 hours ago||
The BCHS stack is OpenBSD, C, OpenBSD's httpd, and SQLite.

Is that the stack you are referring to, or something else?

devmor 7 hours ago||
I still like writing web applications in PHP for many of the same reasons listed here - modern PHP anyways.

I shy away from frameworks unless I am building something that I expect a large team to work on, or I specifically need multiple complex features that don't make sense to maintain.

I've written a few applications for my own personal use or targeted at friends/family recently that have no third party dependencies other than Bootstrap, which I still like more than tailwind for anything that doesn't need to look like a marketing page.

UltraSane 5 hours ago||
Connecting any C code to the internet is a terrible idea as we have learned over and over again.
assimpleaspossi 4 hours ago|
Your server, operating system and database might all be written in C right now.
BirAdam 4 hours ago|||
It's essentially a guarantee.

Linux is C.

Windows is C and C++.

macOS is C, ObjC, and C++.

UltraSane 4 hours ago|||
And great effort had to be made to fix all the thousands of severe security bugs.
assimpleaspossi 4 hours ago||
As if no large software system ever had such a problem and doesn't still today.
t00 6 hours ago||
"Every request spawns a fresh process" <- this is when I stopped reading to save some time for the latest news in AI. Apologies for an abrupt interrupt.
ActorNightly 6 hours ago||
https://news.ycombinator.com/item?id=26271117
EGreg 7 hours ago||
"Every request spawns a fresh process. Deploying a new version is replacing one file. That means no server restart and no downtime. New requests simply pick up the new binary."

I think that's why FastCGI exists. It is the equivalent of PHP's "Swoole" or "FrankenPHP" or "amphp" or Laravel's "octane", etc. Each process is long-lived and tries to clean up its state as much as possible between requests. Forking a new process can take around 8ms (https://news.ycombinator.com/item?id=19005586)

When I was building https://github.com/Qbix/webserver in PHP, I was faced with forking a new process per request. I implemented fork-per-request at first, similar to PHP-FPM and then implemented the "FastCGI" approach of long-lived processes that clean up after each request. (Modern PHP has introspection so the engine can know what to clean up, before calling the php files to handle things.) However, some things are not really possible to completely reset in PHP, and you have to be careful with database connection pools, etc. Typically those are left open even by preforking processes like php-fpm.

Anyway, I ended up flipping it: use the "FastCGI" approach as a default, and if the original PHP code doesn't work (rare), then for that app, you can choose the regular CGI approach of forking a process per request. That way you get maximum speed by default, and manually opt into safety (this is an exception to my usual priorities).

PHP is actually quite fast. But I was also interested in transpiling it to C. So I built the transpiler at: https://ulanguage.org/playground ... used Claude to transform about 1000 PHP functions to their native equivalents, but if you use any of the others, it would hook into the Zend runtime and link with it. (There are some nuances with zvals and objects, and resources in previous PHP versions, but I managed to overcome them.) By the way, it also works with Node and Python FFI the same way.

You can basically now take PHP, Python or Javascript/Typescript code, and speed up the CPU-bound parts about 10-50x. A lot of that is because of string and JSON handling. Many web apps use strings and JSON a lot. So underneath, I had the compiler use "ropes" for the strings, and avoid concatenation. It also parallelizes the JSON encode and decode into code that can run on multiple cores at once, i.e. large JSON payloads are encoded and decoded in parallel. Those are just huge speedups.

Anyway... all of this is mostly focused on speed optimization, so you can solve the literally C100K problem (forget C10K) today, on a regular box. If you don't need speed or FastCGI, your approach is fast enough for the vast majority of applications.

PS: By the way, if you fork after your program already loaded the framework and initialized the configuration, then you can run a lot more workers, literally 10x-100x more of them, because Linux and Mac will share all the loaded memory between processes, and do Copy-On-Write only on dirty pages. And when your request is done, and the process exits, that custom memory is efficiently cleaned up by the OS. It is this feature that allowed me to finally get the best of both worlds... run all existing PHP code, that makes blocking I/O requests all over the place to the database, filesystem, network etc. while at the same time being able to handle 100,000 requests in flight at the same time! Swoole and FrankenPHP can't do that.

tancop 6 hours ago|||
FastCGI sounds like a perfect fit for Tiger Style and Zig. You can avoid allocations completely if you set up a fixed size arena and reset it after each request.
iririririr 7 hours ago||
what are the code paths in php you're dropping for the actual speed improvement? just the parsing?

if not, you could have sent patches and have the best of both worlds

EGreg 6 hours ago||
Patches? Explain

My goal for speed was to run all existing unmodified PHP code out there, but handle 10x as many requests and throughput

Well, and also a ton of other features too

marcelox86 7 hours ago||
Another interesting looking blog post that I can't bear to read without my eyes bleeding :(
nocman 6 hours ago||
Interesting. It looks fine to me. What's the part that bothers your eyes? (genuinely curious).
smhenderson 7 hours ago||
It supports reader view just fine...
marcelox86 6 hours ago||
I'm talking about all the claude-isms in the writing. i've seen a couple posts on linkedin that read just like this page
LoganDark 4 hours ago|
> marketyour.games

Visited this website out of curiosity, and... Please do not use Courier New as a monospace font, not even as part of a fallback. Its stem width for body text is often less than a pixel, which looks awful.

frou_dh 3 hours ago|
Just the other day I thought it looked like the friggin official Python homepage is using Courier for the code example
LoganDark 1 hour ago||
Looks that way to me too, but I wouldn't look to Python for best practice.
More comments...