Search code examples
pythonhashmapinterpreterrhinov8

How do scripting language interpreters reference their underlying functions?


When it comes to script interpreters, like Rhino, Google V8, Python, etc. - is there any general approach to determining the underlying native methods, given only a string of scripting language?

At some point, do these interpreters use hash maps with strings for keys? Or is there a lot of string equality testing and branches?


Solution

  • They typically use hash maps with string keys, but the result of a function lookup is typically cached, to avoid having to do the exact same lookup again a few nanoseconds later.

    Of course the cache must be cleared if something crazy happens, like the program assigns to or deletes the function.

    JIT compilers can use inline caching to make predictable function calls run very fast once the cache is populated.

    The compiler can even just spit out machine code that directly calls the underlying function. Again, if the program replaces or deletes that function, the compiled code would then become invalid; so the interpreter must have a way to detect that situation and update or discard the invalid JIT code.