cardillan / mindcode

A high level language for Mindustry Logic (mlog) and Mindustry Schematics.
http://mindcode.herokuapp.com/
MIT License
85 stars 13 forks source link

Optimizer - condense constants into a single string #139

Closed tom-leys closed 1 week ago

tom-leys commented 1 month ago

When the compiler (or previous optimization steps in this case) outputs this MLOG

print "Reactor "
print 1
print " in emergency shutdown mode\n"

The optimizer should condense it to

print "Reactor 1 in emergency shutdown mode\n"

This was the result of unrolling a loop which calls a function (now with a constant) and printfs in that function.

The full example is here #136

cardillan commented 1 month ago

Print merging merges non-string constants only on aggressive optimization level. The default optimization level is basic in the web app and aggressive in the command-line compiler.

Adding #set optimization = aggressive at the beginning of your program does the trick for the web application.

(Note: I'm not very happy with the way the optimization levels are implemented right now - it seems it generates a lot of confusion. I think it needs some overhaul, but I don't have a clear idea for a better solution at the moment.)

tom-leys commented 1 month ago

(Note: I'm not very happy with the way the optimization levels are implemented right now - it seems it generates a lot of confusion. I think it needs some overhaul, but I don't have a clear idea for a better solution at the moment.)

How about the compiler output contains hints

Hint: 10 optimiszations did not run, add #set optimization = aggressive to run them. Alternatively turn on optimizations x,y,z by adding #turn on one optimization

tom-leys commented 1 month ago

Also, I didn't install the command line compiler because you make it sound quite hard - Java, a database, docker ...

If you had a package with a compiled command line compiler and clear instructions on how to get a JVM (link to a suitable open one) that would have been more attractive and would have reduced your server load.

Also a watch command (though you'd probably need to output to a file in that case) would be useful. Watch, compile, show errors and a keybind that copies the last compile output to the clipboard might work.

cardillan commented 1 month ago

How about the compiler output contains hints

Hint: 10 optimiszations did not run, add #set optimization = aggressive to run them. Alternatively turn on optimizations x,y,z by adding #turn on one optimization

That would be nice. On the other hand, it is not always easy to figure out whether the aggressive optimization would actually be effective, so the code would become more complex and harder to maintain.

I'll probably add optimization level selection to the web app, plus a generic hint that aggressive optimization might produce better code when run on basic. That'll hopefully draw enough attention to it.

If you had a package with a compiled command line compiler and clear instructions on how to get a JVM (link to a suitable open one) that would have been more attractive and would have reduced your server load.

Yeah, I should do that. Good idea!

Also a watch command (though you'd probably need to output to a file in that case) would be useful. Watch, compile, show errors and a keybind that copies the last compile output to the clipboard might work.

Currently the compiler has a switch that copies the resulting code to the clipboard when the compilation is successful.

Watch does sound like a better alternative, but I don't know Linux that well. If you wished to provide some supporting tools or scripts, I'll gladly include them, but I won't be able to support them.

cardillan commented 1 month ago

@tom-leys I've just published a new release, which includes compiled command line tool and installation instructions in the readme. I'd be glad to hear any feedback you might have :)

tom-leys commented 1 month ago

Wonderful!

I'm currently taking a break from writing mindcode, preparing to start a new contract shortly so studying Go instead to add it to my very long list of known languages. I'll let you know once I've tried it out (but it might be a month or more). In the meantime I hope other authors benefit from your efforts.

I really appreciate your rapid support.

tom-leys commented 1 month ago

Just to add, I reviewed your documentation and that sounds ideal. Assuming it works as described that is perfect.

cardillan commented 1 month ago

Thanks and good luck with your new contract! :)

tom-leys commented 1 month ago

Thanks @cardillan :)

cardillan commented 1 week ago

All the ideas from this thread have been implemented in 2.3.0.