What is about TaggedPtr in Rust?

Tagged pointers have been used for many years by large companies such as DragonflyDB. In some cases, it can be beneficial to save some memory by using tagged pointers. However, why doesn't Rust's standard library have this feature?

It is not because pointers are unsafe. Writing tagged references is not difficult. It seems that tagged pointers and references could be implemented in less than a thousand lines of code.

Let's consider an example. The operating system allows us to set 8-bytes data in some calls. In most cases, this is a pointer. We could use 8 bytes to store a tagged pointer, or we could use 8 bytes to represent a pointer to a structure that includes a pointer and some flags.

Is there an RFC about this?

I don't know. But I have seen tagged pointers used in unsafe code in the bytes crate. So this is possible using raw pointers today.

https://docs.rs/bytes/latest/src/bytes/bytes_mut.rs.html#83-92

No new language features are needed. You can just do it with raw pointers.

2 Likes

Yes, I do it. But let's imagine that each low-level software development team writes its own tagged pointers. This means not only the loss of development time (albeit a short time), but also the need for each team to support them.

There's a thread over on IRLO discussing how best to handle tag bits in pointers, along with some of the issues we face incorporating them into Rust's AM model.

You could make the same argument with any feature though. What makes tagged pointers so fundamental that they require support in the standard library as opposed to just a separate crate?

2 Likes

One popular implementation of tagged pointers is the tagptr crate.

2 Likes