Programming thread

The best advice I ever got on this came from an Intel dev: "99% of anything you want to do with SIMD is already in MKL, and the rest gets picked up by the compiler." His point was at best, handrolling intrinsics gets you single-digit benefits at the application level, compared to letting the compiler handle it, and for any kind of bulk computation (like a FFT or something), it's already been done by somebody smarter than you. I was told this 10 years ago and have yet to find a counterexample.
It's always still a good idea for you to write at least one for learning purposes, because it's cool. Also, the Intel engineer is saying that about compilers because the ICC compiler is reportedly better at SIMD than gcc and LLVM, IIRC. Compilers can be cringe sometimes.
 
Do you guys use classes for javascript? I havent learned about them yet and dont functions, objects, and modules pretty much do everything a class can? I know classes are important for other languages like Java but are they for javascript?
Classes are also relatively new to JS, which is presumably why they're not used so much yet.
 
Had a conversation with someone the other day about how overengineered and overkill a lot of popular tools are, specifically Airflow and other workflow/DAG shit. The more I think on it, the more convinced I become that that's correct. Airflow for instance doesn't really do jack shit to version your DAGs, at least not in the newest version I've used.

That makes me want to write a basic orchestrator that you kick off with cron or something.
 
It's always still a good idea for you to write at least one for learning purposes, because it's cool. Also, the Intel engineer is saying that about compilers because the ICC compiler is reportedly better at SIMD than gcc and LLVM, IIRC. Compilers can be cringe sometimes.

It's somewhat better. It's more about effort v reward. Basically, if everywhere you can, you just follow the simple compiler-friendly loop recipes and call optimized 3rd-party libs, there's almost nothing to gain from hand-rolling intrinsics that makes it worth the time investment + maintenance cost.

We had a very bright guy who wrote this complex system of template expressions to deploy his own handrolled intrinsics to do fancy math shit. He had some data showing that, in certain circumstances and on specific tasks, it outperformed MKL by about 1.5x or so. Using it was a massive pain in the ass, plus his salary was entirely consumed by maintaining and extending it, and then, when all was said and done, at the application level, it got us around 2% speedup on a tiny number of real use cases.

So who fucking cares.

Then there was this other guy who wrote his own 3D vector class with handrolled intrinsics for some raytracing code. Okay, fine, whatever. It has plenty of #define spaghetti to make sure it ran on POWER, SSE2, AVX, AVX2, etc. Now, there was a sort of valid motivation here, the build architect was this massive cunt who refused to let anyone put -mavx2 in the build parameters. So the dev wasn't wrong to do this. But eventually, someone in management realized that letting your build engineer sabotage your product's performance just because having to update his test scripts makes him have a big fat faggot cry, so we were allowed to just like compile the fucking code to the fucking target instead of doing a #define tapdance around some fat retard who belongs at the bottom of the sea.

Did this, ripped out his spaghetti, replaced with clean loops, compiler picked it up, absolutely negligible performance difference.

Those aren't the only two examples, but as a general rule, if you're writing intrinsics and using compiler directives, and you're not working on a compute kernel, you are probably doing something that is more trouble than it's worth.
 
It's an equation of how tight your loops are vs. how many gray hairs you want. You can absolutely have shit that outperforms the standard BLAS whatever in your specific circumstances, because you know exactly what you're doing with those matrices and how big they are. If your application is 99.99% number-crunching, you will want to do crazy shit for the 2x speedup there. If you manually vectorize your user interaction code, you are insane (unless this somehow bottlenecks the CPU for 5 seconds, which also means you are insane.) If you manually vectorize something that only runs a couple of times a second in a worker thread and takes 2 ms, you are more than insane. TL;DR: profile your code (with anything from advanced profiling tools to subtracting two time points and printing to console) and work on code that bogs things down until it doesn't.

Anyway none of this fucking matters because we all ship our number crunching to the funny graphics coprocessor anyway.
 
  • Agree
Reactions: UERISIMILITUDO
It's an equation of how tight your loops are vs. how many gray hairs you want. You can absolutely have shit that outperforms the standard BLAS whatever in your specific circumstances, because you know exactly what you're doing with those matrices and how big they are. If your application is 99.99% number-crunching, you will want to do crazy shit for the 2x speedup there. If you manually vectorize your user interaction code, you are insane (unless this somehow bottlenecks the CPU for 5 seconds, which also means you are insane.) If you manually vectorize something that only runs a couple of times a second in a worker thread and takes 2 ms, you are more than insane. TL;DR: profile your code (with anything from advanced profiling tools to subtracting two time points and printing to console) and work on code that bogs things down until it doesn't.

Anyway none of this fucking matters because we all ship our number crunching to the funny graphics coprocessor anyway.

The apps I've worked on spend 90-99% of their time in number crunching. But none of them spend 90-99% of their time in the same routine. The question isn't can you go 2x faster than BLAS on a specific call, it's whether or not you can go so much faster than literally anything that doesn't impose the dev & maint costs of intrinsics that it speeds up the entire application enough to be worth it. I've seen zero examples so far, and this includes codes that do nothing but chew on floats in batch mode. Even when BLAS isn't very fast, it's a memory bandwidth issue, and then kernel fusion + following recipes is so fast that again, intrinsics get at most marginal gain.

I'm not saying examples don't exist, I'm saying I haven't seen any. That includes on GPU - if your code includes #defined calls to NVIDIA intrinsics, I'm not going to accept that commit without an extremely compelling reason, that reason being release tests are at least 1.5x faster, end to end, with your magic.
 
The apps I've worked on spend 90-99% of their time in number crunching. But none of them spend 90-99% of their time in the same routine. The question isn't can you go 2x faster than BLAS on a specific call, it's whether or not you can go so much faster than literally anything that doesn't impose the dev & maint costs of intrinsics that it speeds up the entire application enough to be worth it. I've seen zero examples so far, and this includes codes that do nothing but chew on floats in batch mode.
Unless in a restricted environment of some kind, dev costs are more important by far.
 
The main thing that classes give you, other than that they're nice to use, is private and static fields. You might be able to achieve this with some bastard arrangement of global variables, or define them in a closure somewhere so that they're not accessible to the outside, but classes will just package them in naturally.

A private field is only accessible within class methods, e.g. you could do:
JavaScript:
class MyClass {
  #privateValue;
  getValue() {
    return this.#privateValue;
  }
  setValue(val) {
    this.#privateValue = val;
  }
}
Obviously this isn't a particularly useful example, but now any MyClass object will have get/set functions that access the private #privateValue field that won't be accessible outside otherwise. You could use private fields to store aspects of the object's internal state in a way that wouldn't be meaningful to the outside, or you can use private fields which are methods to perform internal stuff that you'll only need to call from other class methods.

Static fields are global to the class, so if MyClass had a defined static #staticValue; then any class method could access MyClass.#staticValue, but only class methods could access it. Note that any instance of the class could access the static value, and it'd be the same value. You could do something like have the class keep track of how many instances of it have been created (or are "active").
I recently found a more flexible way to achieve the same result as classes with just functions. This is a rather extreme example taken from a WIP refactor of an extension I'm working on:

JavaScript:
const Chat = ((platform) => {
    var socket = new WebSocket(SOCKET_URI);

    ...

    function getChatContainer() {
        return document.querySelector(platform.chatContainer);
    }

    function getExistingMessages() {
        ...
    }

    function sendMessages(msgs) {
        ...
    }

    function mutationObserve(muts, observer) {
        ...
    }

    function bindMutationObserver() {
        ...
    }

    function newBindInterval() {
        if (document.querySelector(".sneed-chat-container") === null) {
            const container = getChatContainer();
            if (!(container === null || container.classList.contains("sneed-chat-container"))) {
                console.log(`[SNEED::${platform.name}] Binding chat container.`);
                bindMutationObserver();
            }
        }
    }

    return {
        newBindInterval
    };
});

var base = Chat(Platform());
setInterval(base.newBindInterval, 1000);

You basically define a function that encapsulates everything and return an object at the end which contains all of the vars and functions you want to make public. You can see in the example that I pass another object to it as an argument. This is one of a few ways to achieve the same goals as inheritance. JS isn't really my thing, so idk if this is common knowledge.
 
Last edited:
You basically define a function that encapsulates everything and return an object at the end which contains all of the vars and functions you want to make public. You can see in the example that I pass another object to it as an argument. This is one of a few ways to achieve the same goals as inheritance. JS isn't really my thing, so idk if this is common knowledge.
So basically it's the revealing module pattern?
 
So basically it's the revealing module pattern?
Ah, didn't know there was a name for it; that's exactly what it is. With the amount of power and flexibility this provides, I haven't found a good reason to prefer normal classes over it. There are a lot of things I don't like about JS, but this feature feels really elegant to me as a long-time lover of LISP.
 
You basically define a function that encapsulates everything and return an object at the end which contains all of the vars and functions you want to make public. You can see in the example that I pass another object to it as an argument. This is one of a few ways to achieve the same goals as inheritance. JS isn't really my thing, so idk if this is common knowledge.
That's basically what I referred to off-hand as "wrap everything in a closure somewhere". Returning an object out of the closure gives you an accessible set of properties and/or methods that interfaced with it.

It can be done to more or less the same effect, but classes are kinda just nicer.

The other thing that I forgot to mention about JS classes is their inheritance model though. You can do something like extend XMLHttpRequest and it'll have all of the methods/properties that it should have but you can override them and refer to the super properties/methods (the properties inherited from the parent class) as needed.
JavaScript:
window.XMLHttpRequest = class XMLHttpRequest extends window.XMLHttpRequest {
  #requestURL;
  open(method, url, ...properties) {
    this.#requestURL = url;
    super.open(method, url, ...properties);
  }
  send(body) {
    this.addEventListener('load', () => {
      // do something here?
    });
    super.send(body);
  }
}
...everything else in XMLHttpRequest doesn't have to be redefined, it'll work exactly as it should work.
 
That's basically what I referred to off-hand as "wrap everything in a closure somewhere".
Oh, lol not sure how I missed that. Having functions, and essentially all non-primitives in JS, be treated as objects is a powerful paradigm to work with. There's something about well structured functional stuff that is immensely satisfying to my autismo brain. One of my all-time favourite projects was a final exam I did for a CS (not required since I majored in pure math) bird course back in the day; I had to write a full Scheme compiler in C using the classical CPS+trampoline based approach.

Classes are really just the old prototypes system under the hood so you can achieve the same results with whatever feels easiest. My example above does that inheritance backwards in a sense using a more functional approach with callbacks. There are other approaches that are less involved, but I have been conditioned over decades to be very wary of scoping in general. Old habits die hard.
 
Last edited:
  • Agree
Reactions: Creative Username
Ah, didn't know there was a name for it; that's exactly what it is. With the amount of power and flexibility this provides, I haven't found a good reason to prefer normal classes over it. There are a lot of things I don't like about JS, but this feature feels really elegant to me as a long-time lover of LISP.
JavaScript is kind of like LISP pretending to be Java anyway.
 
JavaScript is kind of like LISP pretending to be Java anyway.
Very true. IIRC, Netscape originally contracted Brendan Eich to embed Scheme for scripting while they wanted Sun to do the same with Java (cue war flashbacks to Java applets) and eventually they decided on something new that looks like Java and chose one of the most confusing names in computer science to boost adoption during the Java boom.

I have always wondered what the web would be like with something closer to pure Scheme or maybe Racket. JS's weird hybrid nature lets web devs make some of the worst design decisions possible and can really harm readability if sane decisions weren't made early on, and that's my primary beef with it. Racket, for example, can work as a functional/imperative hybrid if you really want it to, but at some point you might as well give up and use Python or something instead (or hy, which I have found pretty fun to work with). If it weren't for retards in professional development roles and all of the god awful shit they make with JS, I would likely be singing its praises a lot more.
 
Very true. IIRC, Netscape originally contracted Brendan Eich to embed Scheme for scripting while they wanted Sun to do the same with Java (cue war flashbacks to Java applets) and eventually they decided on something new that looks like Java and chose one of the most confusing names in computer science to boost adoption during the Java boom.

I have always wondered what the web would be like with something closer to pure Scheme or maybe Racket. JS's weird hybrid nature lets web devs make some of the worst design decisions possible and can really harm readability if sane decisions weren't made early on, and that's my primary beef with it. Racket, for example, can work as a functional/imperative hybrid if you really want it to, but at some point you might as well give up and use Python or something instead (or hy, which I have found pretty fun to work with). If it weren't for retards in professional development roles and all of the god awful shit they make with JS, I would likely be singing its praises a lot more.
JS is, at its core, a functional language. There is a reason that a lot of the DOM APIs do functional shit in the first place.

Functional is easily one of the best programming paradigms because of its simplicity and its solutions to simple problems that other paradigms are shit at. Even most non-functional languages have first-class functions for a reason, otherwise certain things would be next to impossible. Even C (procedural language, like functional but slightly less magical) lets you pass around void*s for callbacks and dynamic behavior. That said, being over a certain level of autistic about pure functionality and no side-effects will lead to a non-functional (but bug-free) program.

We can't have nice things because people get turned off by the parentheses.
 
How are javascript and php used in conjunction? I know javascript can generate html to display on a website but so can php.
PHP runs on the server side. It can generate arbitrary HTML/JS/CSS and send it to the client. The client runs JavaScript, and the client-side JS can modify pages in arbitrary ways. You could, for example, make a PHP script that sends a mostly blank page that uses JS to load in content from other PHP scripts, but this is bad for SEO and bad for schizos who disable JS. No PHP is run on the client, the client can't even see the PHP code, and no JS runs on the server.
 
no JS runs on the server
Though it can, with Node!

You could, for example, make a PHP script that sends a mostly blank page that uses JS to load in content from other PHP scripts, but this is bad for SEO and bad for schizos who disable JS.
I think disabling JS is pretty much impossible in the browser nowadays, unless you're very determined. And Googlebot is supposedly able to run simple Javascript code for page-rendering, but I don't know how well it does it.
 
Though it can, with Node!
He's asking about PHP, so he's going to be asking from a LAMP stack POV.
I think disabling JS is pretty much impossible in the browser nowadays, unless you're very determined. And Googlebot is supposedly able to run simple Javascript code for page-rendering, but I don't know how well it does it.
It's as simple as going into the super-advanced about: config page and disabling it. NoScript and Tor Browser also allow for an easily-available switch. Googlebot uses a headless Chrome and runs everything in a page, and picks out links from anchor tags. Google has guides on how to make your JS abomination work properly. Also, not every search engine runs JS.
edit: btfo by the :c chrismoji (:c)
 
Back