Comments (7)
Mind you that the website documentation is not like a language standards document which has to "define behaviour" or have "undefined behaviour."
I think your given example code is nonsensical due to having multiple cases with the same condition, and it's reasonable to assume that this is undefined behaviour and the compiler has the right to optimise this in some ways. In this case, the compiler does optimise it — maybe for a different reason than why the code is nonsensical — but it has the right to do so either way.
Pluto should maybe raise a warning for the duplicate switch case so you could fix your code (because, again, it's clearly nonsensical) as was already mentioned in #649, although it was kind of forgotten about.
from pluto.
The example is obviously nonsensical, it should just show the point. Maybe it would have been better to replace the last case with a call to some random function which could or could not return 1
.
from pluto.
I get that casecond
allows function calls, and we made sure to make the switch blocks flexible like that, but when it comes to optimisations, it has to be based on real-world considerations, and people simply don't write this kind of code. And if they did, I would say the compiler is right to have done the optimisation because your code made certain implications.
I would document this as a "pitfall", but I don't feel like such theoretical code is enough to justify that.
from pluto.
I would be happy about some form of notice that the order is up to the compiler and that cases can be optimised fully out.
This would actually very nice for an optimisation for the switch statement I was writing. It would allow to move all the constants up and test them first with a table lookup to map each case to an integer and do a binary search to make switches O(log n) and then test in linear time the dynamic cases.
from pluto.
I will reiterate that the website documentation is not a standards document, but I will also point out that at no point is it guaranteed that the conditions are evaluated in the order they are written, and I think the general expectation kind of is that the compiler can and will reorder them if it helps it execute the code faster; the optimisations just have to work as expected in all the "real-world" cases.
from pluto.
There we disagree. I have the expectation (especially for dynamic languages) that things are executed from top to bottom, left to right if not specified otherwise. Maybe this is caused by the languages I have seen.
Furthermore, I would not expect a compiler to just optimise away expressions with side effects, as done here with casevalues that map to default.
Note that this is just my view and others might differ, but there might be other developers out there wo have the same view and for them it would be nice to have a notice that the order is not defined and the default
case is executed if none of the other cases that map to non-default cases are true.
from pluto.
Well, what I have been doing professionally for the last couple of years is C++ and there switches are just notoriously well-optimised and result in pretty unpredictable code. Ideally, we would like to have the same happen with Pluto generating Lua bytecode. And with this as a guideline, we can see that in this case, it's actually an error:
switch (1) {
case 1: default: printf("1 alpha\n"); break;
case 1: printf("1 beta\n"); break; // error : duplicate case value '1'
}
and forget about calling functions inside of the case conditions...
So, I think Pluto is in a bit of a weird situation, where the kind of optimisations that happen to the switch blocks are actually observable due to how flexible they were designed. Maybe we will have to compromise on either the flexibility or the optimisations, but right now I don't see an issue with having both.
from pluto.
Related Issues (20)
- Socket: Get remote address
- Exports return at block end HOT 3
- Failed to compile v0.9.1 on aarch64 linux. HOT 4
- Walrus operator regression with # operator
- Read of uninitialized variable
- New Warning: Attempted necromancy
- unreachable-code false-positive
- Better error message for invalid pipe operator syntax
- Lua header files cannot be included in a C project HOT 1
- Makefile with MinGW doesn't work (missing syslibs, etc.) HOT 2
- Get rid of LUA_USE_{POSIX,_LINUX} macros
- Requesting new feature 'async'. HOT 1
- Switch expression does not reset pinnedreg
- Switch statement parsing confusion
- Single fallthrough not allowed HOT 2
- Switch optimisation HOT 1
- Compatibility mode through executable name HOT 1
- segfault using string.dump and debug.getinfo HOT 1
- Null Coalesce Issue
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pluto.