TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Storing data in pointers

118 pointsby luuover 1 year ago

13 comments

marcus_holmesover 1 year ago
I looked at the Go implementation of this in &quot;tagged pointers&quot; [0]<p>The amount of data that can be used for the tag is architecture-dependant, and the routine discards any tag bits that don&#x27;t fit into the tagged pointer without telling the caller.<p>To me, this seems ridiculous - why not just use a struct with a tag and a pointer, and not run the risk of your tag being destroyed without you knowing because the architecture can&#x27;t fit that many bits?<p>But the Go folks are smart, and must be doing this for a reason. Can anyone explain the thinking here?<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;golang&#x2F;go&#x2F;blob&#x2F;master&#x2F;src&#x2F;runtime&#x2F;tagptr_64bit.go">https:&#x2F;&#x2F;github.com&#x2F;golang&#x2F;go&#x2F;blob&#x2F;master&#x2F;src&#x2F;runtime&#x2F;tagptr_...</a>
评论 #38430104 未加载
评论 #38427981 未加载
评论 #38427992 未加载
评论 #38428284 未加载
评论 #38439469 未加载
评论 #38429760 未加载
评论 #38427424 未加载
JonChesterfieldover 1 year ago
There&#x27;s a trick here I hadn&#x27;t noticed. Good times.<p>If the plan is 8 byte aligned data and use the three low bits for other stuff, you mask them off before loads&#x2F;stores. An alternative is to use the high three bits, store the pointer&#x2F;8, and multiply by 8 to retrieve.<p>That&#x27;s appealing on x64 because memory operations can include a *8 in the encoding.<p>Specifically, I&#x27;ve long wondered whether the pointer masking tricks mess up prefetch&#x2F;speculation. It seems plausible that making the dereference look more like a normal memory operation is helpful for that.<p>(It also means the low-alignment-bits and high-unused-bits can be considered contiguous modulo the *8 on decode which is probably less annoying when using both ranges)
dkerstenover 1 year ago
Note that Intels 5-level paging uses 57 bit addresses, so on these processors, you can only safely assume the top 7 bits are unused. <a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Intel_5-level_paging" rel="nofollow noreferrer">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Intel_5-level_paging</a><p>I also believe that when masking the bits off before loads&#x2F;stores, you need to set them to the same value of the highest used bit (bit 47 or bit 56), while this often is 0, it’s not necessarily the case. Something to be aware of.<p>Finally, when using C++, you gotta be careful of strict aliasing. In C++20 you can use std::bit_cast <a href="https:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;cpp&#x2F;numeric&#x2F;bit_cast" rel="nofollow noreferrer">https:&#x2F;&#x2F;en.cppreference.com&#x2F;w&#x2F;cpp&#x2F;numeric&#x2F;bit_cast</a>
KMagover 1 year ago
It&#x27;s really unfortunate that all of the mainstream OSes run userspace in the lower portions of the address space.<p>Setting the most-significant 13 bits (really, setting the second to 12th bits and at least one of the following bits) of an IEEE-754 float will result in a NaN bit pattern. That means that any pointer to the top 2 petabytes of a 64-bit address space, if cast to an IEEE-754 double will be NaN. This means the NaN-boxing used in Safari and Firefox&#x27;s JavaScript engines, LuaJIT, etc. would be no-ops. (Safari and Firefox use different mechanisms, but they&#x27;d become the same if moved to the top of the address space.)<p>It&#x27;s not enough of a performance difference to re-jigger everything in mainstream OSes, but I imagine if someone were to come up with a unikernel&#x2F;exokernel OS specifically for JITing some dynamic language, there&#x27;s some performance to be had by having all of the dynamic language objects in the upper 2 petabytes of the address space.
jandreseover 1 year ago
Very old Macs used this trick to squeeze their ROM routines down a bit, operating with 24 bit addressing and using the top bits for flags and whatnot. Of course they ran into trouble when machines with 16MB of memory started appearing. If you do this you might be making more work for yourself in the future when you buy a new machine with 256EB of main memory.
评论 #38428373 未加载
评论 #38429144 未加载
评论 #38427524 未加载
评论 #38433737 未加载
评论 #38427299 未加载
评论 #38427803 未加载
jandrewrogersover 1 year ago
This is a nice summary of the practical aspects and considerations. It isn’t something anyone should be doing explicitly on a regular basis but there are occasions, particularly in libraries, where it is the perfect tool for the job.<p>There is also the inverse use case: smuggling pointers inside status codes, enums, and similar. For example, optionally encoding a pointer to additional error metadata for non-zero result codes. In C++ it isn’t that uncommon to also see Result types implemented similarly when the type allows it.
alpaca128over 1 year ago
There&#x27;s also the Rust library smartstring which allows creating short strings without heap allocations: a Rust string consists of three values (pointer, capacity, length), so 24B on a 64bit architecture. With one byte used as tag this leaves 23 bytes for storing the string in the same space.
评论 #38432481 未加载
评论 #38431248 未加载
poweraover 1 year ago
I don&#x27;t want to say you should never do this.<p>But if you aren&#x27;t writing a compiler or an embedded system, you should never do this.
评论 #38425864 未加载
评论 #38430643 未加载
评论 #38430661 未加载
评论 #38436674 未加载
评论 #38427058 未加载
dmt314159over 1 year ago
This will be a problem when trying this on Capability Hardware Enhanced RISC Instructions (CHERI) <a href="https:&#x2F;&#x2F;www.cl.cam.ac.uk&#x2F;research&#x2F;security&#x2F;ctsrd&#x2F;cheri&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.cl.cam.ac.uk&#x2F;research&#x2F;security&#x2F;ctsrd&#x2F;cheri&#x2F;</a> systems. Currently the only CPU with this implemented is Arm&#x27;s Morello prototype.<p>Pointers are replaced by 128-bit capabilities containing what operations are valid, a 64-bit base address and a size. These capabilities are unforgeable, so trying to play with &quot;unused&quot; parts of 64-bit pointers simply won&#x27;t work.<p>FreeBSD &amp; Gnome are running on hardware after minor source-code changes, along with a significant proportion of FreeBSD ports collection.<p>As well as ARM, Microsoft is interested: <a href="https:&#x2F;&#x2F;www.microsoft.com&#x2F;en-us&#x2F;research&#x2F;project&#x2F;portmeirion&#x2F;overview&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.microsoft.com&#x2F;en-us&#x2F;research&#x2F;project&#x2F;portmeirion...</a>
rr808over 1 year ago
My favorite hack back in MFC days was a combo box which I stored the pointer address in the text (to the right after a lot of spaces so was hidden). When a user chooses an item, parse the pointer and de-reference it back to an object.
评论 #38427275 未加载
评论 #38428121 未加载
jonasmerlinover 1 year ago
&quot;I think it&#x27;s quite well known that on a 64-bit system, the maximum bit-width of a virtual address is somewhat lower (commonly 48-bits).&quot; might actually be a perfect example of the Average Framiliarity xkcd[0]. It&#x27;s perfectly fine to write it that way and the article obviously knows its intended audience. But I&#x27;m wondering what percentage of readers here actually knew this beforehand. (I learned it only pretty recently myself. And yes, I should have known this sooner, but ... didn&#x27;t.)<p>[0] <a href="https:&#x2F;&#x2F;xkcd.com&#x2F;2501&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;xkcd.com&#x2F;2501&#x2F;</a>
评论 #38430842 未加载
ForOldHackover 1 year ago
My first thought was to use the Address calculation logic for an additional ALU, then... my second thought was trying to justify this during a code review... and lastly, why Microsoft used the LDT upper byte to make Xenix 286 incompatible... and the headache that changing architectures made for poor programming.
_a_a_a_over 1 year ago
The alpha only accessed 8-byte aligned mem addresses (originally anyway). The bottom 3 bits were ignored (masked out) on lookups, per the spec, to allow users to stuff juicy extra info into these.
评论 #38428927 未加载