top | item 40296704

(no title)

zgs | 1 year ago

Every time I've seen similar done in the past, it has come back to hurt the instigator.

discuss

order

masklinn|1 year ago

Apple's been using that for more than a decade: https://www.mikeash.com/pyblog/friday-qa-2012-07-27-lets-bui... http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_e...

OCaml's also been using tagging to mark unboxed type pretty much from the start, nearly 30 years ago: https://dev.realworldocaml.org/runtime-memory-layout.html#:~....

duskwuff|1 year ago

And it's not like Apple isn't aware of the dangers, either; they used top-byte tagging on the 68000, and it took them a lot of time and effort to migrate away from that to support 16+ MB systems.

jstimpfle|1 year ago

It's common for libraries to use the 2 lowest bits in tree structures e.g. RB tree. It's not a problem at all since the data structure is controlled by the library. Even if tree nodes are allocated/provided by the user, having a "4-byte aligned" requirement in the API contract isn't a problem at all -- in fact you need to work quite hard to allocate a structure with a pointer that isn't at least 4 byte aligned (i386), or 8 byte aligned (x64).

a1369209993|1 year ago

> in fact you need to work quite hard to allocate a structure with a pointer that isn't at least 4 byte aligned (i386), or 8 byte aligned (x64).

Well, no, actually; it's:

  p = malloc(size+1)+1;
It's just quite implausible that you'd do that by accident.

db48x|1 year ago

Every lisp system that ever existed uses this technique, and it never hurt them any. Emacs or SBCL or Open Genera; they all work perfectly well.

gpderetta|1 year ago

And the modern take on this is NaN Boxing!

rjsw|1 year ago

Maclisp on PDP-10 and Franz Lisp didn't, they used bibop type checking instead of tagged pointers.

lmm|1 year ago

> Every lisp system that ever existed uses this technique, and it never hurt them any.

Wasn't it part of the reason they ended up with poor "mechanical sympathy" on regular PCs, and got a bad performance reputation as a result?

_flux|1 year ago

OCaml uses the lowest bit, which is nice because (aligned) pointers work as-is.

mbrubeck|1 year ago

This bitvec I wrote for Servo and Gecko uses this technique, and has been shipping in Firefox releases for over six years with no bugs found:

https://docs.rs/smallbitvec/

I'm pretty sure you can find several more examples in Firefox, as well as other major browsers.

junon|1 year ago

VM writers use this egregiously. I've not heard of it causing issues. It's not that complicated.

Lua, Lisps, many others.

titzer|1 year ago

All fast JavaScript VMs use some form of pointer tagging. V8 uses (compressed) tagged pointers and both JSC and Firefox used NaN-boxing.

WasmGC has a i31ref as part of its reference hierarchy, which is implemented with pointer tagging.

pixelesque|1 year ago

I've used tagged pointers with no issues at all over the years to "smuggle" / store values in...

coldtea|1 year ago

This "trick" has been used since the dawn of time in major platforms

adql|1 year ago

I dunno, pointers were kinda small on 8/16 bit platforms

adql|1 year ago

Any example ?

speed_spread|1 year ago

If you build your whole app with it, yeah. If you use it tactically for a certain data type it can be very nice. Just don't try to dissimulate it's specialness.