The GPU loves arrays of structures AoS, since all vertex data fits in its triangle assembly cache. Once given to the GPU, the software side doesnt really care for all vertex parameters so this optimisation is pointless. Only relavent when you have instance rendering (leaves, grass) but then you only need an array of vec3’s, not the other parameters so back to normal arrays.
Meanwhile, game engines need operator overloading for adding/multiplying vectors (spatial transforms, lighting, physics) and core zig design philosophy prevents operator overloading.
Blind leading the blind. Disclaimer - I do professional rendering engines.
> Meanwhile, game engines need operator overloading for adding/multiplying vectors (spatial transforms, lighting, physics) and core zig design philosophy prevents operator overloading.
This is a frustrating decision. My use cases for low level languages overlap closely with my use cases for vectors (etc) with operator overloading. It was one of the first things which put a bad taste in my mouth about Zig.
Zig has a builtin vector type and will get a builtin matrix type. The only useful thing that's missing from shading language vector-math syntax is swizzling, but you don't get that via operator overloading either (and for dot- and cross-product you'll still need a function, but that's also in line with shading languages).
Not GP, but I've written game engines and rendering engines. Vector operations are just common enough that having to write `.mul` every time is a huge pain, especially when you put many of them together for a large formula. Compare:
We learn to read and think about math a certain way, which is incompatible with Zig. Also, Zig's design philosophy of "reading code over writing code" is incompatible with the kind of small modification-test-cycles required when doing games, and creative programming in general. So Zig is sort of DOA anyway for that kind of thing.
But I've been using Zig for non-game projects and it's been fantastic, so definitely not "Blind leading the blind" for the overall language design, imo.
I know this is already possible with comptime, though I haven't implemented it yet since I haven't needed vector math in what I'm working on currently. Can't decide whether using math names is better or worse than using the full variable names though.
I have a sibling comment -- having thought about this for a very very long time, zig should really implement binary pseudo-operator syntactic sugar. I don't think this violate zig's spirit of 'no hidden function calls' in that I don't think it takes much of a mental lift to "get" that (_ <+> _) means "heyo this is a function call, not a true operator".
At first I was going to say that I disagreed since you couldn't choose what implementation of addition you wanted, but now that I've read your comment where you import the type of addition used, it's growing on me. Would you have operator precedence, or would it be more like Smalltalk's binary operators?
Andrew talks about it because it introduces hidden control flow where you're expecting simple operators. In Zig anything that deals with control flow is a keyword (including short circuiting and, which is `and` instead of `&&`).
I'd argue though that the real disadvantage to having overloadable arithmetic is that you're limited to one implementation. This is actually my biggest beef with Rust, namely traits/type classes. It locks you into a single implementation when you may want to do something different based on the context. Zig pushes the dispatch decision to the callsite, not a trait subsystem (see how Zig implements hash mays for example). So I'd personally prefer to use a DSL, since it lets me specify what type of dispatch to use.
Overloadable operators are not an instance of hidden control flow. Overloadable operators represent a user-defined function call, and thus can't influence control flow any more than a regular function. And if regular functions can't do anything weird to control flow (e.g. if your language already lacks exceptions (or even weirder things like Ruby-style procs)), then overloadable operators can't either.
> It locks you into a single implementation when you may want to do something different based on the context.
If you want differing behavior in a certain context, and if you don't want to use a different method to make the differing behavior explicit (e.g. the `wrapping_add` methods that Rust provides on numeric types), then you can use a different type for that context, e.g. the `std::num::Wrapping` type that Rust provides.
> Overloadable operators are not an instance of hidden control flow.
In general perhaps not, but in Zig it definitely does. Zig considers calling a function to change control flow, because it's no longer just an operator but something that can cause side effects, includinh mutating in place. Perhaps control flow isn't the right term, maybe non-trivial would be better?
With regard to wrappers, I personally find them ugly since 1. They bring in indirection, and I have a personal vendetta against unnecessary indirection, 2. Wrapping doesn't compose well and is a pain to shephard between representations, 3. It's harder to make a function generic across different representations, and 4. Wrappers often don't re-export everything available to their underlying value.
> Perhaps control flow isn't the right term, maybe non-trivial would be better?
Indeed, there are plenty of valid reasons to be wary of operator overloading, such as the risk that someone might insert a network call into your vector addition. There's some precedence from C++ in calling an operator invocation "trivial" when it hasn't been user-defined, in general I might go further and say that a good overloaded operator is "well-behaved" when it not only has a non-surprising implementation (e.g. no side-effects) but also its function is congruent with the specific chosen operator (so no overloading bitshift for iostreams).
I’m not advocating this, but it is worth observing that it is yet another problem one could attempt to address with dependency injection, similar to io and allocators.
It's appealing to people who want to understand and control everything they're doing. When I'm using pandas or SQLAlchemy, I have no idea what the code is actually doing. Most people don't care about such implementation details, but some people do.
yes! i had this exact idea. i also thought about integrating geometric/clifford algebra using zig's type system so that you could have one mathematical multivector object instead of complex / quaternion types, etc.
That's the other great thing about using comptime, is you can specify which DSL you want to use for which scenario. You're not locked into one implementation.
For me the answer is very simple: Operators make it easier to read the code which makes it easier to spot bugs. It also makes it easier to turn formulas from textbooks into code.
If 50% of the code you're working with is using vectors and matrices, not having operators for those parts is quite annoying.
Note that you can have vector operators without overloading, e.g. Odin has built in vector and matrix types.
But personally I think it's better to give the user more power instead of only letting the compiler author pick which types to allow operators on. Like how Java overloads + but only on the String class. Why do they get to do it, but not me?
you actually don't want "operator overloading", you want syntactic sugar. I once proposed just a special operator syntax at the parser level, but it got rejected, but if you REALLY wanted it, you could probably do this in about 100-120 lines as a fork of the zig compiler, just hacking (a <_> b) as a special form to be transformed into @"<_>"(a, b). Requiring parentheses elides questions about operator precedence.
const @"<+>" = @import("operator_module").plus;
...
const x = (a <+> b);
I think both operator overloading and most operators themselves are syntactic sugars. Operator overloading happens to point towards specific functions, whereas arithmetic integer operators point to compiler intrinsics.
no, in general overloading is not syntactic sugar, it's a feature of the language (being able to (re-)define a function in place X and have it change the function in unrelated place Y).
I don't see how it is unrelated. If have a custom type `A` with an overload on `+`, it will only affect places I used custom type `A`. If there wasn't operator overloading, I would just have to use a different notation to call the same function, but with possibly worse ergonomics (which is also why I think your solution doesn't really satisfy that, it doesn't read like algebra which is kind of the point). Given that type A is presumed to be custom, I don't see how place Y would be unrelated since it deliberately uses type `A`.
If we include operator overloading for any types, then sure. i32 + i32 might suddenly start meaning something else. But I think that's beyond the scope of what is normally asked by operator overloading.
Yes it is control flow, but IMO it's not hidden. It's true that you need to learn that * happens before + (which usually happens in school), but I don't see how that's any different from needing to learn that `and` short-circuits, or that `if` only evaluates its body if the condition is true.
Compare to what people usually call hidden control flow (exceptions, RAII, ...) where you don't know which parts of the code will run unless you read the definitions of the classes and bodies of the functions you use. The syntax at the call site is not enough to tell.
I mean as an avid Lisp fan, I feel like Lisp basically answers the question of how much syntax you need in a langauge. I must admit though, not having to deal with operators precedence is really nice
1> let ((x 3) (step 2) (width 5)) ((2 * step + x) mod width)
2
This is TXR Lisp with auto-infix and auto-compound enabled for the REPL:
2> *listener-auto-infix-p*
t
3> *listener-auto-compound-p*
t
So we can omit the outermost parentheses, and infix syntax is automatically recognized, freely intermixed with regular Lisp, as if the (ifx ...) macro were wrapped around the input.
I've come up with a very good way of handling infix in Lisp, and documented it in a decent amount of detail as well, not just as a manual for the user but anyone wanting to implement something similar.
Regarding operators, there are 3 distinct problems.
One is to allow the use of simple mathematical symbols as names for functions, instead of allowing only alphanumeric identifiers.
Most programming languages allow only a small fixed set of symbols to be used as "operators", i.e. as function names.
The better solution is to allow any Unicode character from certain categories, e.g. "Sm" and "Po" ("Symbol, math" and "Punctuation, other"), which does not have an already assigned role in the language syntax, to be used as a function name.
Most LISP variants allow the use of various kinds of character symbols as function names.
The second problem is overloading. Overloading must be treated uniformly for any kind of functions, regardless if their names are identifiers or operator symbols, i.e. not like in Java, where forbidding operator overloading was a mistake (that was an overreaction to C++, which allows the overloading of a few "operators" that are not normal functions and whose overloading should not have been allowed, e.g. the comma operator).
The overloading of operators, especially for user-defined data types is something absolutely essential for scientific and technical computing.
The majority of programmers have not been exposed to programs that contain a great amount of computations, so they are accustomed only with simple expressions that contain a few variables.
In scientific and technical computing it is very frequent to have very big expressions, which may contain a large number of operations and variables, where the variables may have various types, like complex numbers, vectors, matrices, complex vectors, complex matrices, or there may be a type system with distinct types for various physical quantities, like voltages, electric currents, capacitances and so on.
Anyone who had to write frequently such big expressions will definitely prefer, both for writing and for reading, to use overloaded operator symbols instead of long function names, which would fill most of the visual space with superfluous characters, obscuring the structure of the big expression.
The third problem is the syntax of function invocation. Most programming languages allow functions whose names are identifiers to use only prefix invocation but for some symbolic operators they allow infix invocation.
Here I also prefer the languages that do not differentiate between functions with alphanumeric names and functions with symbolic names (i.e. operators). There are languages where for any function it may be specified that it must be invoked as an infix operator, if this is desired.
Which is the best between the 3 classic solutions for expression syntax, traditional expressions with infix operators and multi-level precedence rules (like in FORTRAN and ALGOL), expressions with infix operators and a unique precedence rule for all operators (like in APL) and expressions without infix operators (like in LISP), is debatable.
Each of the 3 solutions has advantages and disadvantages, so the choice between them is a matter of personal preferences.
On the other hand, SIMD loves SoA, and so does the CPU cache. It all depends on what you're doing with your data.
Zig professes to be a C replacement, not a C++ replacement, so leaving out operator overloading is consistent with that design goal. But I agree, I would prefer to program in a language that expresses mathematical relationships more naturally.
So is the argument that any SoA is pointless? Or just for GPU stuff? Because this isn't really talking about all that one way or another.
Also does one really need operator overloading? That feels a little strong. I've gotten by with functions just fine.. Does that make the GPU not like me Mr. wise engineer?
Splitting fat vertex component data into multiple streams also often makes sense in rendering engines (e.g. not all vertex shaders might need all vertex components). Strict SoA or strict AoS hardly ever makes sense, but an 'inbetween' approach often does (maybe call it SoAoS) - and this should be possible just fine with Zig's comptime approach, e.g. only apply the SoA transform to the toplevel items of a struct.
As for CPU-side vector math:
Zig already has a @Vector type (which will probably be renamed to @Simd) and it will get a builtin matrix type. With those two things, the main reason for operator overloading in game/rendering engines is pretty much handled via builtin types.
Zig is adding native vectors including operator support, there are some recent issues/prs about this topic.
The general technique of SoA is pretty useful both in games and other applications, but of course I cannot speak to the specific use-case you are describing.
Zig vectors force data into SIMD registers even if that would make the code slower. They're a specialty type. You should only reach for vectors if you would have used SIMD intrinsics in C for example.
Zig vectors do not necessarily force data into SIMD registers; a scalar implementation would work equally well. This is not just a theoretical argument, because Zig code that uses `@Vector` also has to compile for architectures that do not have SIMD instructions.
That being said, the parent commenter is actually referring to other recent proposals as opposed to existing `@Vector` functionality:
The other day I just wanted to loop through characters in a std::string to copy data to a new string with a few escape characters (sending to peripheral device). Simple enough task for AI. I got a coroutine monstrocity back, with copies to std::array and a range based iterator, since I specified C++23. If I specified C++11, I would have received a:
char p = src.data();
while (p)
{
…
p++;
}
I had the experience to keep calling out AI to simplify and downgrade the solution to something primitive, which ended up smaller, faster, easier to maintain. Juniors with real world experience would not bother, they’ll take the first working AI result.
Linux is just the kernel, the ecosystem is make up of half a dozen desktop managers, windowing systems, API toolkits, sound servers, file systems, package hits etc.
There is an abstraction layer between all these systems. Multiuser, whether you need it or not.
Haiku is a unified system, so native apps have one windowing system, one desktop environment, one API, one media kit, one file system etc. There are less layers for data to travel, hence it will always be faster. Also Haiku targets desktop users (single user system, for better or worse), while Linux in all honesty targets servers and embedded with desktop a distant 3rd use case. Haiku package management is a generation ahead of Linux.
Finally, BeOS/Haiku core architecture is built from modern 90's designs, while Linux started as a clone of Unix (deep in the bowels of Linux there is a TTY terminal block device).
Finally, BeOS had a cool factor (and their fanboys) that Linux never had. Dual CPU from day #1. Blinkenlights. Geek port. Playing videos on a face of a cube. is_computer_on(). Linux is sooo boring in comparison.
Test the API kernel calling convention when dealing with 64 bit IEE754 doubles on a 32 bit CPU, especially when dealing with MSB vs LSB processors.
Also, a long time ago (pre 486DX), processors did not have FPU circuitry instead it was a FPU coprocessor. When dealing with a kernel context switch, you'd have to copy all registers to a stack. With a coprocessor, you'd have to make sure those registers got copied as well. Which was slower with coprocessors ... So for a time some real time kernels did not allow context switching of FPU. To support that, you'd get the performance hit.
These days its all integrated so you dont have to worry about it ...
Meanwhile, game engines need operator overloading for adding/multiplying vectors (spatial transforms, lighting, physics) and core zig design philosophy prevents operator overloading.
Blind leading the blind. Disclaimer - I do professional rendering engines.
reply