
Pointers are Easy, Optimization is Complicated - mpweiher
https://blog.metaobject.com/2020/09/pointers-are-easy-optimization-is.html
======
gumby
The author has a rather restrictive view of addressing modes.

The C model of pointer reflects the limited addressing modes of the PDP-7 and
PDP-11. It has the advantage of being easy to reason about but provides a
limited set of functions. Some syntactic sugar is provided (e.g. `x[y]`) for
access to indexed addressing, but anything more complex must be written by
hand.

Counterintuitively, art of the role of a programming language is to _restrict_
what you can do in the name of managing cognitive overhead (and is that not
also the role of abstraction) and some of those trade offs are worth it
(restricting bytes to being only 8 bits wide was, in retrospect, OK).

But there are programs that “can’t” be written in C. “Can’t” is in quotes
because you can, but either have to escape into assembly code (e.g. there is
no way to express SIMD intrinsics in C) or you have to say `resolve_ptr(foo)`
rather than `*foo`).

So per pointers specifically:

Segmented memory models far predate the founding of intel corporation, and
serve various purposes. One major one was indeed address space expansion,
which thankfully is much less important than it used to be, but can also
support valuable functionality hard or impossible to express in a language
like c. For example, something like a setuid shared library. Overlapping
segments can express other region smpemantics such as fast and slow memory

Even without segmentation the full value of the pointer can be used for other
purposes such as type tagging; capabilities, signatures and other such
security mechanisms; et al, none of which can be expressed in the C syntax.

(You can see that segmentation and pointer discrimination are really just
different ways of describing the same mechanism to the hardware. However the
semantics of a pointer may quite reasonably reflect other machine state, such
as register state, that cannot be explicitly represented in C)

~~~
mpweiher
> The author has a rather restrictive view of addressing modes.

Not really, no.

I thought a single prominent example would suffice to show how the industry
moved towards the "flat address space" model and heaved a big sigh of relief
after it had done so.

> full value of the pointer can be used for other purposes such as type
> tagging [..]

And this is very much dependent on being able to treat that pointer as an
integer whose bits you can process...

------
scaramanga
Looks like the author has somewhat missed the point of the original article.
Which is interesting because the author repeated the point to make it clear:

> Just because two pointers point to the same address, does not mean they are
> equal and can be used interchangeably.

That is based on the C++ standard and its interpretation among mainstream
compilers.

The rest of the article goes on, at some length, to discuss ideas about how to
define a abstract pointer model for a programming language in which pointers
can be used more easily and safely and, by doing so, allow compilers to make
optimizations which don't confusingly behave differently to unoptimised code.
Which, seems to me at least, kind of the opposite of the characterisation as,
sort of, "a bunch of wank in which compiler designers show us how clever they
are with their useless micro-optimisations at the expense of making everything
more difficult for us mere mortal programmers"

------
gpapilion
The discussion of real mode memory segmentation on early intel platforms
brings back nightmares for me. The memory layout with overlapping segments was
straight up bonkers, and maddening to deal with.

