<s>
Inline	B-Application
caching	I-Application
is	O
an	O
optimization	B-Application
technique	I-Application
employed	O
by	O
some	O
language	B-Device
runtimes	I-Device
,	O
and	O
first	O
developed	O
for	O
Smalltalk	B-Application
.	O
</s>
<s>
The	O
goal	O
of	O
inline	B-Application
caching	I-Application
is	O
to	O
speed	O
up	O
runtime	O
method	O
binding	O
by	O
remembering	O
the	O
results	O
of	O
a	O
previous	O
method	O
lookup	O
directly	O
at	O
the	O
call	B-Language
site	I-Language
.	O
</s>
<s>
Inline	B-Application
caching	I-Application
is	O
especially	O
useful	O
for	O
dynamically	O
typed	O
languages	O
where	O
most	O
if	O
not	O
all	O
method	O
binding	O
happens	O
at	O
runtime	O
and	O
where	O
virtual	B-Language
method	I-Language
tables	I-Language
often	O
cannot	O
be	O
used	O
.	O
</s>
<s>
The	O
following	O
ECMAScript	B-Language
function	O
receives	O
an	O
object	O
,	O
invokes	O
its	O
toString-method	O
and	O
displays	O
the	O
results	O
on	O
the	O
page	O
the	O
script	O
is	O
embedded	O
in	O
.	O
</s>
<s>
Since	O
the	O
type	O
of	O
the	O
object	O
is	O
not	O
specified	O
and	O
because	O
of	O
potential	O
method	B-Language
overloading	I-Language
,	O
it	O
is	O
impossible	O
to	O
decide	O
ahead	O
of	O
time	O
which	O
concrete	O
implementation	O
of	O
the	O
toString-method	O
is	O
going	O
to	O
be	O
invoked	O
.	O
</s>
<s>
In	O
language	B-Device
runtimes	I-Device
that	O
do	O
not	O
employ	O
some	O
form	O
of	O
caching	O
,	O
this	O
lookup	O
is	O
performed	O
every	O
time	O
a	O
method	O
is	O
invoked	O
.	O
</s>
<s>
Because	O
methods	O
may	O
be	O
defined	O
several	O
steps	O
down	O
the	O
inheritance	B-Language
chain	I-Language
,	O
a	O
dynamic	O
lookup	O
can	O
be	O
an	O
expensive	O
operation	O
.	O
</s>
<s>
To	O
achieve	O
better	O
performance	O
,	O
many	O
language	B-Device
runtimes	I-Device
employ	O
some	O
form	O
of	O
non-inline	O
caching	O
where	O
the	O
results	O
of	O
a	O
limited	O
number	O
of	O
method	O
lookups	O
are	O
stored	O
in	O
an	O
associative	O
data	O
structure	O
.	O
</s>
<s>
The	O
concept	O
of	O
inline	B-Application
caching	I-Application
is	O
based	O
on	O
the	O
empirical	O
observation	O
that	O
the	O
objects	O
that	O
occur	O
at	O
a	O
particular	O
call	B-Language
site	I-Language
are	O
often	O
of	O
the	O
same	O
type	O
.	O
</s>
<s>
directly	O
at	O
the	O
call	B-Language
site	I-Language
.	O
</s>
<s>
To	O
facilitate	O
this	O
process	O
,	O
call	B-Language
sites	I-Language
are	O
assigned	O
different	O
states	O
.	O
</s>
<s>
Initially	O
,	O
a	O
call	B-Language
site	I-Language
is	O
considered	O
to	O
be	O
"	O
uninitialized	O
"	O
.	O
</s>
<s>
Once	O
the	O
language	O
runtime	O
reaches	O
a	O
particular	O
uninitialized	O
call	B-Language
site	I-Language
,	O
it	O
performs	O
the	O
dynamic	O
lookup	O
,	O
stores	O
the	O
result	O
at	O
the	O
call	B-Language
site	I-Language
and	O
changes	O
its	O
state	O
to	O
"	O
monomorphic	O
"	O
.	O
</s>
<s>
If	O
the	O
language	O
runtime	O
reaches	O
the	O
same	O
call	B-Language
site	I-Language
again	O
,	O
it	O
retrieves	O
the	O
callee	O
from	O
it	O
and	O
invokes	O
it	O
directly	O
without	O
performing	O
any	O
more	O
lookups	O
.	O
</s>
<s>
To	O
account	O
for	O
the	O
possibility	O
that	O
objects	O
of	O
different	O
types	O
may	O
occur	O
at	O
the	O
same	O
call	B-Language
site	I-Language
,	O
the	O
language	O
runtime	O
also	O
has	O
to	O
insert	O
guard	B-Language
conditions	I-Language
into	O
the	O
code	O
.	O
</s>
<s>
Most	O
commonly	O
,	O
these	O
are	O
inserted	O
into	O
the	O
preamble	O
of	O
the	O
callee	O
rather	O
than	O
at	O
the	O
call	B-Language
site	I-Language
to	O
better	O
exploit	O
branch	B-General_Concept
prediction	I-General_Concept
and	O
to	O
save	O
space	O
due	O
to	O
one	O
copy	O
in	O
the	O
preamble	O
versus	O
multiple	O
copies	O
at	O
each	O
call	B-Language
site	I-Language
.	O
</s>
<s>
If	O
a	O
call	B-Language
site	I-Language
that	O
is	O
in	O
the	O
"	O
monomorphic	O
"	O
state	O
encounters	O
a	O
type	O
other	O
than	O
the	O
one	O
it	O
expects	O
,	O
it	O
has	O
to	O
change	O
back	O
to	O
the	O
"	O
uninitialized	O
"	O
state	O
and	O
perform	O
a	O
full	O
dynamic	O
lookup	O
again	O
.	O
</s>
<s>
The	O
run-time	O
routine	O
then	O
rewrites	O
the	O
instructions	O
,	O
changing	O
the	O
load	O
instruction	O
to	O
load	O
the	O
register	O
with	O
the	O
type	O
of	O
the	O
current	O
receiver	O
,	O
and	O
the	O
call	O
instruction	O
to	O
call	O
the	O
preamble	O
of	O
the	O
target	O
method	O
,	O
now	O
"	O
linking	O
"	O
the	O
call	B-Language
site	I-Language
to	O
the	O
target	O
method	O
.	O
</s>
<s>
The	O
performance	O
gains	O
come	O
from	O
having	O
to	O
do	O
one	O
type	O
comparison	O
,	O
instead	O
of	O
at	O
least	O
a	O
type	O
comparison	O
and	O
a	O
selector	O
comparison	O
for	O
the	O
first-level	O
method	O
lookup	O
cache	O
,	O
and	O
from	O
using	O
a	O
direct	O
call	O
(	O
which	O
will	O
benefit	O
from	O
instruction	O
prefetch	O
and	O
pipe-lining	O
)	O
as	O
opposed	O
to	O
the	O
indirect	O
call	O
in	O
a	O
method-lookup	O
or	O
a	O
vtable	B-Language
dispatch	O
.	O
</s>
<s>
If	O
a	O
particular	O
call	B-Language
site	I-Language
frequently	O
sees	O
different	O
types	O
of	O
objects	O
,	O
the	O
performance	O
benefits	O
of	O
inline	B-Application
caching	I-Application
can	O
easily	O
be	O
nullified	O
by	O
the	O
overhead	O
induced	O
by	O
the	O
frequent	O
changes	O
in	O
state	O
of	O
the	O
call	B-Language
site	I-Language
.	O
</s>
<s>
The	O
following	O
example	O
constitutes	O
a	O
worst-case	O
scenario	O
for	O
monomorphic	O
inline	B-Application
caching	I-Application
:	O
</s>
<s>
A	O
naive	O
implementation	O
of	O
monomorphic	O
inline	B-Application
caching	I-Application
would	O
therefore	O
constantly	O
cycle	O
through	O
the	O
"	O
uninitialized	O
"	O
and	O
"	O
monomorphic	O
"	O
states	O
.	O
</s>
<s>
In	O
order	O
to	O
prevent	O
this	O
from	O
happening	O
,	O
most	O
implementations	O
of	O
monomorphic	O
inline	B-Application
caching	I-Application
support	O
a	O
third	O
state	O
often	O
referred	O
to	O
as	O
the	O
"	O
megamorphic	O
"	O
state	O
.	O
</s>
<s>
This	O
state	O
is	O
entered	O
when	O
a	O
particular	O
call	B-Language
site	I-Language
has	O
seen	O
a	O
predetermined	O
number	O
of	O
different	O
types	O
.	O
</s>
<s>
Once	O
a	O
call	B-Language
site	I-Language
has	O
entered	O
the	O
"	O
megamorphic	O
"	O
state	O
,	O
it	O
will	O
behave	O
just	O
as	O
it	O
did	O
in	O
the	O
"	O
uninitialized	O
"	O
state	O
with	O
the	O
exception	O
that	O
it	O
will	O
not	O
enter	O
the	O
"	O
monomorphic	O
"	O
state	O
ever	O
again	O
(	O
some	O
implementations	O
of	O
monomorphic	O
inline	B-Application
caching	I-Application
will	O
change	O
"	O
megamorphic	O
"	O
call	B-Language
sites	I-Language
back	O
to	O
being	O
"	O
uninitialized	O
"	O
after	O
a	O
certain	O
amount	O
of	O
time	O
has	O
passed	O
or	O
once	O
a	O
full	O
garbage	B-General_Concept
collection	I-General_Concept
cycle	O
is	O
performed	O
)	O
.	O
</s>
<s>
To	O
better	O
deal	O
with	O
call	B-Language
sites	I-Language
that	O
frequently	O
see	O
a	O
limited	O
number	O
of	O
different	O
types	O
,	O
some	O
language	B-Device
runtimes	I-Device
employ	O
a	O
technique	O
called	O
polymorphic	O
inline	B-Application
caching	I-Application
.	O
</s>
<s>
With	O
polymorphic	O
inline	B-Application
caching	I-Application
,	O
once	O
a	O
call	B-Language
site	I-Language
that	O
is	O
in	O
its	O
"	O
monomorphic	O
"	O
state	O
sees	O
its	O
second	O
type	O
,	O
rather	O
than	O
reverting	O
to	O
the	O
"	O
uninitialized	O
"	O
state	O
it	O
switches	O
to	O
a	O
new	O
state	O
called	O
"	O
polymorphic	O
"	O
.	O
</s>
<s>
A	O
"	O
polymorphic	O
"	O
call	B-Language
site	I-Language
decides	O
which	O
of	O
a	O
limited	O
set	O
of	O
known	O
methods	O
to	O
invoke	O
based	O
on	O
the	O
type	O
that	O
it	O
is	O
currently	O
presented	O
with	O
.	O
</s>
<s>
In	O
other	O
words	O
,	O
with	O
polymorphic	O
inline	B-Application
caching	I-Application
,	O
multiple	O
method	O
lookup	O
results	O
can	O
be	O
recorded	O
at	O
the	O
same	O
call	B-Language
site	I-Language
.	O
</s>
<s>
Because	O
every	O
call	B-Language
site	I-Language
in	O
a	O
program	O
can	O
potentially	O
see	O
every	O
type	O
in	O
the	O
system	O
,	O
there	O
usually	O
is	O
an	O
upper	O
bound	O
to	O
how	O
many	O
lookup	O
results	O
are	O
recorded	O
at	O
each	O
call	B-Language
site	I-Language
.	O
</s>
<s>
Once	O
that	O
upper	O
bound	O
is	O
reached	O
,	O
call	B-Language
sites	I-Language
become	O
"	O
megamorphic	O
"	O
and	O
no	O
more	O
inline	B-Application
caching	I-Application
is	O
performed	O
.	O
</s>
<s>
The	O
observation	O
that	O
together	O
,	O
monomorphic	O
and	O
polymorphic	O
inline	B-Application
caches	I-Application
collect	O
per-call-site	O
receiver	O
type	O
information	O
as	O
a	O
side-effect	O
of	O
optimizing	O
program	O
execution	O
led	O
to	O
the	O
development	O
of	O
adaptive	O
optimization	O
in	O
Self	B-Operating_System
,	O
where	O
the	O
run-time	O
optimizes	O
"	O
hot	O
spots	O
"	O
in	O
the	O
program	O
using	O
the	O
type	O
information	O
in	O
inline	B-Application
caches	I-Application
to	O
guide	O
speculative	O
inlining	O
decisions	O
.	O
</s>
<s>
If	O
a	O
run-time	O
uses	O
both	O
monomorphic	O
and	O
polymorphic	O
inline	B-Application
caching	I-Application
then	O
in	O
the	O
steady	O
state	O
the	O
only	O
unlinked	O
sends	O
occurring	O
will	O
be	O
those	O
from	O
sends	O
falling-off	O
the	O
ends	O
of	O
polymorphic	O
inline	B-Application
caches	I-Application
.	O
</s>
<s>
A	O
megamorphic	O
inline	B-Application
cache	I-Application
can	O
be	O
implemented	O
by	O
creating	O
code	O
to	O
perform	O
a	O
first-level	O
method	O
lookup	O
for	O
a	O
particular	O
call-site	O
.	O
</s>
<s>
In	O
this	O
scheme	O
once	O
a	O
send	O
falls-off	O
the	O
end	O
of	O
a	O
polymorphic	O
inline	B-Application
cache	I-Application
a	O
megamorphic	O
cache	O
specific	O
to	O
the	O
call	B-Language
site	I-Language
's	O
selector	O
is	O
created	O
(	O
or	O
shared	O
if	O
one	O
already	O
exists	O
)	O
,	O
and	O
the	O
send	O
site	O
is	O
relinked	O
to	O
call	O
it	O
.	O
</s>
<s>
The	O
code	O
can	O
be	O
significantly	O
more	O
efficient	O
than	O
a	O
normal	O
first-level	O
method	O
lookup	O
probe	O
since	O
the	O
selector	O
is	O
now	O
a	O
constant	O
,	O
which	O
decreases	O
register	O
pressure	O
,	O
the	O
code	O
for	O
the	O
lookup	O
and	O
dispatch	O
is	O
executed	O
without	O
calling	O
into	O
the	O
run-time	O
,	O
and	O
the	O
dispatch	O
can	O
benefit	O
from	O
branch	B-General_Concept
prediction	I-General_Concept
.	O
</s>
<s>
Empirical	O
measurements	O
show	O
that	O
in	O
large	O
Smalltalk	B-Application
programs	O
about	O
1/3	O
of	O
all	O
send	O
sites	O
in	O
active	O
methods	O
remain	O
unlinked	O
,	O
and	O
of	O
the	O
remaining	O
2/3	O
,	O
90%	O
are	O
monomorphic	O
,	O
9%	O
polymorphic	O
and	O
1%	O
(	O
0.9	O
%	O
)	O
are	O
megamorphic	O
.	O
</s>
