<div dir="ltr">Well Eliot answered before I did :-).<div><br></div><div>Basically the VM caches expects the lookup to be stable: a given receiver class + selector should always answer the same lookup result. Installing or editing new methods invalidates this property and hence part of the cache needs to be flushed in when this happens (based on the method or the selector).<br><div class="gmail_extra"><br><div class="gmail_quote">2015-11-24 19:10 GMT+01:00 Eliot Miranda <span dir="ltr">&lt;<a href="mailto:eliot.miranda@gmail.com" target="_blank">eliot.miranda@gmail.com</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <br><div dir="ltr">Hi Stephane,<div><br></div><div>    the VM caches method lookups in a lookup cache from class,selector pairs to method,primitive pairs, where primitive may be null.  This is called the first-level method lookup cache.  The JIT VM caches message lookups in machine code, so that a particular piece of machine code exists in a state that invokes a method for a specific class very fast by embedding the class reference in a register load and the target method in a call instruction, and having the target method verify this &quot;cache probe&quot; (this is an &quot;in-line cache).  The JIT also caches the translation of a byte coded method to machine code, by hiding a reference to a machine code method in a byte coded method.</div><div><br></div><div>These caches can be invalidated in several circumstances:</div><div><br></div><div>1a. if one adds or removes a method from a class&#39;s method dictionary it may change the correct results of a lookup not merely of the class whose dictionary was updated but also subclasses of the class.</div><div>1b. if one replaces a method in a method dictionary this changes the target method for a lookup of the selector for the class and subclasses</div><div><br></div><div>2. if one wants to rewrite the byte code or literals of a method, for example because a Slot definition has changed, then if the method has been compiled to machine code, the machine code must be discarded before the new code may be executed</div><div><br></div><div>1a &amp; 1b are done via Symbol&gt;&gt;flushCache.  In response the normal VM flushes its first-level method lookup cache, and the JIT also scans all of machine code looking for inline caches with that selector, and voiding them, reverting each send site for that selector to the &quot;unlinked&quot; state.</div><div><br></div><div>There used to be confusion in Squeak, which Pharo inherited, that using CompiledMethod&gt;&gt;flushCache was somehow the right way to void caches when updating method dictionaries, flushing the old method in the dictionary, if any, and the new method.  It isn&#39;t, precisely because adding or removing methods affects the visibility of inherited methods with the same selector.  So MethodDictionary code should use Symbol&gt;&gt;flushCache, and only once, on each update of a method dictionary.  As a result, the VM will ensure that the necessary send caches are flushed for that selector.<br></div><div><br></div><div>2. is done via CompiledMethod&gt;&gt;flushCache.  In response the VM searches the first-level method lookup cache and removes all entries whose target is the method.  In addition the JIT discards the machine code for the method, and searches for all send sites with that method&#39;s machine code as the target and voids them, reverting them to the unlinked state.</div><div><br></div><div>The VM must be told to flush the cached state for a compiled method via CompiledMethod&gt;&gt;flushCache and will /try/ and void the state for that method.  But it can&#39;t always deal with existing activations of that method, because if there are activations running the machine code, that machine code can&#39;t merely be thrown away, and can&#39;t be replaced because its length may change, depending on literals or byte codes.  So this kind of byte coded method manipulation needs to be done with case and some understanding of the total system state.</div><div><br></div><div>HTH</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Nov 24, 2015 at 9:37 AM, stepharo <span dir="ltr">&lt;<a href="mailto:stepharo@free.fr" target="_blank">stepharo@free.fr</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Clement<br>
<br>
do you know what is flushCache and why we need to invoke it?<br>
<br>
Stef<br>
<br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div><div dir="ltr"><div><span style="font-size:small;border-collapse:separate"><div>_,,,^..^,,,_<br></div><div>best, Eliot</div></span></div></div></div>
</div>
<br></blockquote></div><br></div></div></div>