News for hackers. Stuff that matters.
Re:Is eval evil? Just-in-time compiling by goto11 on Monday November 19, 2018 @12:52PM (#18488200)
But stuff could contain " }; { " or similar weirdness which means you cannot even make assumptions about the semantics of the code in the constants parts of the string. In the second example there is no reason to use eval in the first place when you can just write object[field], so why optimize for eval support?
Re:Is eval evil? Just-in-time compiling by chrisseaton on Monday November 19, 2018 @01:03PM (#18488296)
> But stuff could contain...

So assume it doesn’t and fixit up if it ever does. That’s the basics of JIT compilation!

Re:Is eval evil? Just-in-time compiling by teraflop on Monday November 19, 2018 @01:12PM (#18488356)
Yes, in principle, you could add all kinds of micro-optimizations to eval() that preserve its semantic behavior. However, there are some good reasons not to. ("Your scientists were so preoccupied with whether or not they could, they didn't stop to think if they should.")

One reason is that it makes it significantly harder to reason about the performance of a program, because the behavior of the JIT is based on all kinds of internal state and pattern-matching rules, which are typically hard to expose in a comprehensible way. The more complexity you add to the optimization and deoptimization process, the more inscrutable the performance characteristics become.

Another reason is that it encourages developers to use unsafe patterns. If programmers have a choice between `obj[key]` and `eval("obj." + key)`, why would you put effort into optimizing the one that has the potential to blow up if "key" has funny characters, when a safer, cleaner alternative exists?

Re:Is eval evil? Just-in-time compiling by pizlonator on Monday November 19, 2018 @02:13PM (#18488860)
If people use it (they do use eval) and it is optimizeable (eval is optimizeable) then whichever implementation optimizes it will be able to deliver a better overall experience.
Re:Is eval evil? Just-in-time compiling by teraflop on Monday November 19, 2018 @03:04PM (#18489306)
I disagree, again for a couple of different reasons.

Number 1: you say "eval is optimizeable", but optimizing eval in general is incredibly difficult. It's true that you can add optimizations for specific situations. But every time you do so, you expand the surface area of the boundary between "efficient" and "inefficient", and it becomes harder to maintain a mental model of how any given chunk of code will be executed. (For example, in Python, strings are immutable; when concatenating strings in a loop, the intermediate copies can be optimized away but only sometimes, which means that pattern is generally avoided as a matter of good coding style. Tail-return optimization in Scheme is a useful trick that suffers from similar drawbacks, which is probably why you don't see it implemented in a lot of other languages.)

I'm skeptical that adding this type of complexity results in a better overall developer experience. Remember, code is written not just for the compiler, but also to be read and understood.

And number 2: in practice, most languages don't have multiple competing implementations at the level of complexity you're talking about. When your compiler gets to the point of doing sophisticated JIT optimizations, maintaining it requires a lot of specialized knowledge and experience. It's impractical to fork a project like V8 or Hotspot to add a specific new optimization. So individual runtime environments usually aren't subject to competitive pressure, except from entirely different languages.

Re:Is eval evil? Just-in-time compiling by pizlonator on Monday November 19, 2018 @05:08PM (#18490157)
There is no change to the developer experience when a VM speculates about things like this. Browsers speculate about things that are more complex than this already.

It doesn't matter if eval is optimizeable in the general case. It only matters if this speeds up some cases without hurting others, on average. VMs and compilers are great at doing such optimizations already.

You don't have to fork V8 since there are other JavaScript implementations. I recommend JavaScriptCore because it's the fastest.

Similarly, Ruby, Python, and Lua all have multiple competing implementations and some folks pick them based on performance. Java is special, but that's because HotSpot emerged as the victor in a brutal multi-implementation battle for Java performance supremacy. Java used to have many implementations that all contributed something to our modern knowledge of VM design and this wouldn't have happened without the competition.

Therefore, I think you're wrong on both numbers. Number 1 - this is not a semantically visible optimization; it just makes programs take less time on average; Number 2 - in practice, languages absolutely do have multiple competing implementations.

Re:Is eval evil? Just-in-time compiling by goto11 on Monday November 19, 2018 @01:35PM (#18488557)
Sure but will you get any benefit to offset the cost? Eval is typically used for dynamically generated code, otherwise why use eval in the first place? Evaling a hardcoded string constant seem completely useless to me so what would be the purpose of optimizing for that use case?
Re:Is eval evil? Just-in-time compiling by pizlonator on Monday November 19, 2018 @02:15PM (#18488867)
I’ve definitely written uses of eval that have a large amount of stuff that doesn’t change. Pretty sure it’s common.