mindcode icon indicating copy to clipboard operation
mindcode copied to clipboard

Optimizer - condense constants into a single string

Open tom-leys opened this issue 1 year ago • 9 comments

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

tom-leys avatar Sep 04 '24 21:09 tom-leys

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.)

cardillan avatar Sep 05 '24 09:09 cardillan

(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 avatar Sep 05 '24 20:09 tom-leys

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.

tom-leys avatar Sep 05 '24 20:09 tom-leys

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 avatar Sep 06 '24 07:09 cardillan

@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 :)

cardillan avatar Sep 10 '24 21:09 cardillan

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 avatar Sep 12 '24 21:09 tom-leys

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

tom-leys avatar Sep 12 '24 21:09 tom-leys

Thanks and good luck with your new contract! :)

cardillan avatar Sep 13 '24 10:09 cardillan

Thanks @cardillan :)

tom-leys avatar Sep 13 '24 20:09 tom-leys

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

cardillan avatar Oct 20 '24 09:10 cardillan