0

I've read that dynamically typed language are slower because they store variable names as string, but can't they use something else? I'm asking this question as a follow up of this question:

Why are dynamically typed languages slow?

Aren't there other methods to access variable than using a name hash lookup ? Wouldn't this be an opportunity to use a template programming technique ?

Community
  • 1
  • 1
jokoon
  • 6,207
  • 11
  • 48
  • 85
  • If you can do `foo = 'bar' + rand(); echo baz[foo];`, and none of that is known until runtime, it's impossible to optimise that to anything but a string lookup of some sort or another. – deceze Feb 05 '16 at 16:21
  • 1
    I think that either the answer to that question is unclear, or you don't get its point. In statically typed languages, there's a lot of analysis a compiler can do in guaranteed finite time. In dynamically typed languages, you don't even have the guarantee that you can automatically derive the type(s) of each variable; reasoning about the program during compile time is quite hard if the compiler doesn't even know that. –  Feb 05 '16 at 17:01
  • All statically typed language compiles down to a dynamic type language (well almost all, x86 machine code and ARM machine code are dynamically typed - RAM has no type - but there were CPUs in the past that had tagged memory, where RAM had types but they were failures in the market). Even if they compile down to a statically typed language (is JVM statically typed? I have my doubts) they run an a dynamically typed CPU. – slebetman Feb 10 '16 at 16:33
  • Also, x86 assembly, ARM assembly, PowerPC assembly, MIPS assembly, 68k assembly etc. are all dynamically typed. You can execute any integer or floating point operations on any variable. – slebetman Feb 10 '16 at 16:35
  • It is misleading to claim that assembly is "dynamically typed" in a discussion about programming languages. Dynamic typing means that the language will do a bunch of low level things for you like copying data to a different memory location, changing the raw value, etc... It's a high level concept that refers to a type of abstraction. Assembly is not dynamically typed in that sense. It's not typed at all. It has one type, raw data. – Chris Rollins Oct 27 '16 at 05:23

1 Answers1

1

There are several reasons, next are just a couple. Suppose, there is a code that receives a variable x and accesses a field foo on it, and one time this variable is attached to an object of type Bar, other time it is attached to an object of type Baz, both have other fields and there is no relationship between the two. The access x.foo is valid in both cases, but because the classes are completely unrelated, it's difficult to map the identifier foo to some integer that can be later used to quickly access the desired field: its location and its type may be unpredictable. To add some reality, imagine that some other classes may come and go in the system, and they also may have the field foo.

Another scenario has nothing to do with object-oriented approaches and was learned at the time when LISP and similar languages were invented. They allow to compute not only what is statically written in the code, but also to evaluate some terms at run-time. The terms may be retrieved, e.g. as plain strings, from external files, databases, network, etc. and then interpreted with a special function, often called eval. Of course, the dynamically computed terms may refer to existing variables. And there is no other way to do it except by their names.

However it seems reasonable that some variable accesses are optimized at run-time. The key issue is that it can be done only in some specific circumstances, not most of the time like in statically-typed languages.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35