|
Boost : |
Subject: [boost] Tagged pointer library?
From: Hans Dembinski (hans.dembinski_at_[hidden])
Date: 2017-01-09 04:54:46
Hi all,
I recently learned about tagged pointers, https://en.m.wikipedia.org/wiki/Tagged_pointer. Over the weekend, I was working on a tiny_string implementation which has a width of only one pointer. Essentially it's a null-terminated char* array managed by std::unique_ptr. On my system, std::string is four times larger (probably 8 byte for the pointer, 8 byte for the length, 8 byte for the allocator and 8 byte for ⦠good measure?).
With a tagged pointer, I could even implement a small-string optimisation in that tiny_string, by storing small strings with up to 7 bytes in the memory that otherwise holds the address to the char* array. The tag then allows you to discriminate between the two uses of the memory. You may rightfully question whether it is really practical to do this, but it is certainly one possible application of a tagged pointer.
Looking into boost, I found that the lockfree library has a tagged pointer implementation in its "detail" namespace. The implementation is nice, because it falls back to a safe version with a separate tag field on systems which do not follow the required alignment conventions.
Wouldn't it make sense to make the tagged pointer accessible by users, i.e. place it in a separate library (or in the existing smart_ptr library)?
Best regards,
Hans
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk