Stefan Monnier <monn…@iro.umontreal.ca> wrote:
> a…@littlepinkcloud.invalid [2026-11-24 13:46:24] wrote:
>> Stefan Monnier <monn…@iro.umontreal.ca> wrote:
>>> Hmm... didn't think of that. Indeed this could lead to actual
>>> addresses embedded in the code. Then again, I'd expect most such JIT
>>> setups to want to keep most of those addresses in data rather than in
>>> the code so the GC can more easily trace through them.
>> GCs have not the slightest difficulty tracing through embedded
>> addresses. We use whatever form of address constants is fastest.
>
> Maybe it's not "hard" compared to the rest of what it takes to write
> a high performance GC, but it's still a fair bit more work to have to
> tell the GC where to find the few bitfields that need to be stitched
> together to make up the actual pointer (it definitely makes tracing
> through such references slower), and if you have a moving GC it also
> makes moves harder, because of the need to both retrofit the new address
> into the corresponding bitfields (which also means you can't use an
> instruction sequence special-tailored to the original address) and take
> care of updating/flushing the I-cache.
Yes, we have to do all of that, but it's very-well-understood
technology that we've been using for decades. Object references in
code are treated as "old": the referenced objects are in regions that
rarely get moved. We have to be able to scan live stacks and code and
a variety of other places, and compared with that unpacking address
constants is a fairly easy case.
>>> Andrew, do you know what causes all those large constant addresses
>>> you're seeing?
>>
>> Very common constants are classes used in expressions, such as
>>
>> new File("filename")
>>
>> or casts between types
>>
>> File x = (File)anObject;
>>
>> Casts are more common than is immediately apparent, because the
>> compiler inserts them when it can't prove the type of an object.
>
> Interesting, thanks. So it is indeed the JIT case suggested by Anton!
Yes.
>> The other important case is metadata. We gather a lot of information
>> about running code: branch frequency, type frequency, and so on. Every
>> metadata address is a constant in the generated code. After a while,
>> code is recompiled and we use all the gathered informtion to drive
>> inlining and other optimizations.
>
> Hmm... even more interesting: since the metadata-gathering is part of
> the code that's generated cheaply,
Why is emitting three instructions harder than emitting a PC-relative
load and storing a pointer somewhere?
> I'd have expected that code to use a simple approach that always
> loads the large constants from some data section rather than "use
> whatever form of address constants is fastest" (which I would have
> expected is used only for the high-performance version of the code).
Metadata is very rarely moved, so there's no disadvantage to just
embedding addresses in the code. Sure, we could put address constants
in a local pool instead, but it wouldn't save any space.
Andrew.