# Do symbol references always go through a hash table?

- Thread: https://externals.io/message/93373
- Original archive: https://news-web.php.net/php.internals/93373
- Started by Jesse Schalken on 2016-05-17T13:03:08+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #93373 — Jesse Schalken — 2016-05-17T13:03:08+00:00

URL: https://externals.io/message/93373#93373

Hi Internals,

I'd like to know whether references to
classes/functions/methods/properties/variables always go through a hash
table at runtime, or if the name is optimised into a pointer or fixed
offset. I recall seeing such an optimisation for local variables, but what
about the others?

Thanks

---

## #93376 — Nikita Popov — 2016-05-17T14:28:14+00:00

URL: https://externals.io/message/93373#93376 · In reply to: #93373

On Tue, May 17, 2016 at 3:03 PM, Jesse Schalken <me@jesseschalken.com>
wrote:

> Hi Internals,
>
> I'd like to know whether references to
> classes/functions/methods/properties/variables always go through a hash
> table at runtime, or if the name is optimised into a pointer or fixed
> offset. I recall seeing such an optimisation for local variables, but what
> about the others?
>
> Thanks
>

A property access $foo->bar (where "bar" is a literal and not the result of
an expression) uses a polymorphic runtime cache, which will store the class
entry and property offset the first time the instruction is executed.
Afterwards, if $foo is an instance of the same class, we will directly use
this offset. Otherwise, a full lookup from the property name table with
visibility checks and all those other bells an whistles has to be performed.

The same holds for pretty much all other symbol references as well, so
classes, functions, methods, etc.

Nikita

---

## #93389 — Jesse Schalken — 2016-05-18T02:51:10+00:00

URL: https://externals.io/message/93373#93389 · In reply to: #93376

Thanks Nikita,

Does that mean a function which accepts an instance of an interface and
calls a method on it will have to do a full lookup of the method whenever
the concrete class isn't the same as when that function was first executed?
(or was last executed, depending on how the cache works)




> On Tue, May 17, 2016 at 3:03 PM, Jesse Schalken <me@jesseschalken.com>
> wrote:
>
> > […]
>
> A property access $foo->bar (where "bar" is a literal and not the result
> of an expression) uses a polymorphic runtime cache, which will store the
> class entry and property offset the first time the instruction is executed.
> Afterwards, if $foo is an instance of the same class, we will directly use
> this offset. Otherwise, a full lookup from the property name table with
> visibility checks and all those other bells an whistles has to be performed.
>
> The same holds for pretty much all other symbol references as well, so
> classes, functions, methods, etc.
>
> Nikita

---

## #93390 — Sara Golemon — 2016-05-18T03:06:01+00:00

URL: https://externals.io/message/93373#93390 · In reply to: #93389

> Does that mean a function which accepts an instance of an interface and
> calls a method on it will have to do a full lookup of the method whenever
> the concrete class isn't the same as when that function was first executed?
> (or was last executed, depending on how the cache works)
>
Yes, but that doesn't mean you should micro-optimize around it.  Just
write code that's easy to maintain and trust the compiler to do the
best job at making it not-slow. :)

-Sara

---

## #93391 — Jesse Schalken — 2016-05-18T04:10:12+00:00

URL: https://externals.io/message/93373#93391 · In reply to: #93390

> Yes, but that doesn't mean you should micro-optimize around it.  Just
> write code that's easy to maintain and trust the compiler to do the
> best job at making it not-slow. :)
>
>
Do you happen to know whether or not HHVM does a better job in the case of
a function being called repeatedly with different implementations of a
class/interface?

I don't optimised prematurely, but if I've got performance problems today
and getting a more sophisticated compiler/runtime isn't an option, and I
can't work around it by some other means then I don't really have a choice.

---

## #93392 — Sara Golemon — 2016-05-18T06:42:50+00:00

URL: https://externals.io/message/93373#93392 · In reply to: #93391

> 
> > […]
>
> Do you happen to know whether or not HHVM does a better job in the case of a
> function being called repeatedly with different implementations of a
> class/interface?
>
Yes.  In the case of HHVM, different types coming into a function
generate different tracelets and each tracelet is able to burn its own
target method into the native code.  I say *able*, though.  There are
also translation paths which result in simply punting to a lookup at
runtime.  The decision making process the compiler goes through is...
complex.

> I don't optimised prematurely, but if I've got performance problems today
> and getting a more sophisticated compiler/runtime isn't an option, and I
> can't work around it by some other means then I don't really have a choice.
>
Saying for the list archive benefit as much as yours.  It's only too
easy for this to show up on StackOverflow with a recommendation to
"always break oop conventions by wrapping method calls in a chain of
if (instanceof) checks to cache the right method pointer, yadda
yadda..." and the next thing you know it's php sadness all over again.

-Sara
