Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 08 Aug 2012
Offline Last Active Feb 26 2015 05:25 PM

#5064819 Optimizing code statements into expressions?

Posted by on 25 May 2013 - 12:53 PM

Then close it, you will have the opportunity to see that this actually works in the near future. I prefer seeing my thread closed than my mind to new ideas. Cheers! ;)

#5064775 Optimizing code statements into expressions?

Posted by on 25 May 2013 - 07:44 AM


Now I'm ready for the nanoseconds. Why doing this? Because it's cheap doing it with the automated inlining tool I have built.


You know that the time you spent actually developing this tool and feeding your code into it probably outweighs any such performance benefit by a factor of hundreds, right?




Unless you spent less than a couple seconds or so developing this tool, you've already wasted your time working on something that is costing more time and effort than it is saving you. And that's without paraphrasing the above members with your fundamentally broken optimization approach.


Let's see, it took me about 10 months to produce 9,000+ effective lines of code, and I expect to produce twice as much when I finish the project. Starting took me more effort (more time thinking on future implications, and less code production) because I was building the foundations. I'm making the optimization workflow part of the foundation, so I can define the guidelines to write further code and get the most of source processing. Now, it took me 1 week to build, test and fix the inlining tool in Perl. The tool takes around 3 seconds to shave these 9,000+ lines of code. Now, by defining the optimization guidelines I can put the razor aside, and work normally. That means I don't shave until the next 18,000+ lines. I'll get a Duck Dynasty's beard, but I can always shave at the end without cutting my nose smile.png It's a good moment to optimize.

#5064758 Optimizing code statements into expressions?

Posted by on 25 May 2013 - 06:29 AM

Unfortunately no one who has read this thread seems to know the answer to my question, and people started asking "why doing it". Well I'm giving you people a little light on the purpose of transforming statements into expressions, and one of the reasons is that there is no way to avoid boxing in a large project. I'm seeing it with a decompiler, I'm seeing it in the final build. I already tried to find different ways to remove boxing, but that makes the code un-elegant and difficult to maintain. There is no design flaw. So the way to further optimize is creating a workflow that allows me to work on the code elegantly, and an automated tool to do the dirty job by helping the compiler to do a deeper optimization. I'm already doing the inlining and I'm seeing the benefits, at the moment I just want to find if C# allows converting every statement into an expression. Too bad I probably touched a "language as religion" nerve here, because I'm getting lots of negativity from a simple question. You people can have lots of fun pushing the -1 vote, solving this problem is way more important to me. If you want to learn with me, welcome :)


Using another language is not an option (come on dry.png ). Using Expression Trees is not an option because it makes code unelegant, adds another library, and is not fully support on various platforms (Mono/Unity here). CodeDom in not an option because not being fully supported neither. Standard expressions with the language operators is available everywhere, are rather easy to generate, and not doubt are optimal. Any reason not to use them?

#5064535 Optimizing code statements into expressions?

Posted by on 24 May 2013 - 10:51 AM

How is this actually slowing your program?
How is this something that actually needs a programmer to optimize it?


You might manage to save a cycle or two if you hunt for things like this, but hunting for spare nanoseconds is not generally a good use of time.


Code optimization is generally about saving microseconds or bigger time units.


At the level I'm currently optimizing the system, everything is slowing it down. I already designed an optimized solution, and profiled the system to the "smallest" millisecond. Now I'm ready for the nanoseconds. Why doing this? Because it's cheap doing it with the automated inlining tool I have built.



You are right that boxing is somewhat slow, so DON'T DO IT.  If you end up needing to routinely convert integers and bools into objects, you are likely doing something wrong at the algorithm level.  


It's almost impossible not to generate boxing, not in a system with 9000+ lines of code. The way to actually get rid of it is inlining, so I'm going to do it.



Similarly, the 'is' operator is not exactly fast, and its presence is a generally symptom of a more significant fundamental design problem.  Casting an object to some type and then discarding the results is horrible anyway; if you cannot fix your algorithm's underlying problem that requires the cast, at least have the decency to perform the cast with 'as' and keep the successful results around.


Using the "is" operator is just a trick to convert the statement into a expression, the idea is finding a way for the compiler to remove it later, so it doesn't represent any cost.



Next up, chaining doesn't necessarily mean better performance -- each item is dependent on the previous results which is bad for the pipeline.  You can get pipeline bubbles or poor use of speculative execution and branch prediction if you make it too tight.  On modern hardware keeping it loose is usually better.  


We are not talking of the same thing here. I might have used a wrong term when referring to the AND operator, that "chains/adds/joins" expressions, so the execution sequence is respected.

#5064520 Optimizing code statements into expressions?

Posted by on 24 May 2013 - 10:04 AM

Because C# doesn't support code inlining, and converting statements into expressions will remove boxing/unboxing which is pretty expensive and fragments the heap (in Unity this causes brain cancer), will remove calls to functions, and will allow the compiler to optimize some of the code of the inlined routine. I built a code processor that inlines C# code, so the optimization will be deeper if I can convert more statements smile.png

#5064505 Optimizing code statements into expressions?

Posted by on 24 May 2013 - 09:19 AM

Hi! I'm looking for ideas on how to convert code into expressions in C# for Unity. We have this basic list of statements:
Simple statements:
  • assignment: A:= A + 5
  • call: CLEARSCREEN()
  • return: return 5;
Compound statements:
  • if-statement: if A > 3 then WRITELN(A) else WRITELN("NOT YET"); end
  • switch-statement: switch © { case 'a': alert(); break; case 'q': quit(); break; }
  • while-loop: while NOT EOF DO begin READLN end
  • do-loop: do { computation(&i); } while (i < 10);
  • for-loop: for A:=1 to 10 do WRITELN(A) end
The expressions that replace these statements should be chained, for example using And operators (expression1 && expression2 && .. expresionN), so the order of execution is respected. Also the ?: operator is allowed.
If we have this statement:
int a = 100;
Can be converted into:
((a = 100) == a)
Yet not perfect, since the comparison is not optimized in IL code. I also tried:
((a = 100) is int)
This throws a warning, the comparison is optimized but the assignment is removed too. Any other idea that only produces the assignment in IL code?
A call is automatically converted into a expression, right? Well, not exactly. How do you chain a void function?
Also if you pass parameters by reference, you will need a variable external to the expression.
This one is directly converted:
return 5;
If and Switch statements
The ?: operator replaces them:
if (a == 5) { b = "yes"; } else { b = "no"; }
((a == 5) ? "yes" : "no")
While, Do and For loops
Loop unwinding could be used here, if the number of iterations is fixed. Any ideas for variable length loops?

#5016444 My libraries and engines [various languages]

Posted by on 01 January 2013 - 01:11 PM

Hi skullfire! It's been a long time indeed :)


Teleport died long ago as well as my old nickname, but I keep moving in the shadows ph34r.png Now I'm working on new projects as you can see, mostly in Unity, HTML5 and Flash. You can find me (as "marcoantap") at unityspain.com forums. Are you and your brothers still working at Fair Play?


The toolkit will be high-level this time, the engine is Unity. Everything is written from scratch as always.


Nice to see you here again. Happy new year!

#5016411 My libraries and engines [various languages]

Posted by on 01 January 2013 - 11:33 AM

I'm very happy since this year I'll be working on the most ambitious project of my entire life, so I'm releasing my previous projects as Open Source, mostly game development libraries and engines. I'm a native Spanish speaker, but you will find some code in English:




Old projects:

gamelix.com/opensource/modensambla - 2D libraries in Assembly x86, for Turbo Assembler (DOS)
gamelix.com/opensource/modtpascal - 2D libraries in Pascal, for Turbo Pascal (DOS)
gamelix.com/opensource/roleworld - 2D engine in Object Pascal, for Delphi (Windows)

Recent projects:

gamelix.com/opensource/mundano_engine - 3D engine in Object Pascal, for FPC/Lazarus (Windows, MacOS, Linux)
gamelix.com/opensource/morpho_engine - 3D engine in Object Pascal, for FPC/Lazarus (Windows, MacOS, Linux, Netscape plugin)
gamelix.com/opensource/gamelix_engine_flash - 2D library (planned engine) in Actionscript 3, for Flash (Flash player)
gamelix.com/opensource/gamelix_engine_xcode - 2D (3D based) library (planned engine) in Objective C, for XCode (MacOS, iOS)


Feel free to share your feelings about the old days of game development smile.png

#5011041 Character Customization, the best engine?

Posted by on 15 December 2012 - 03:22 PM

Here is the character customization tutorial:


#5010994 Ideas for lockpicking mechanic?

Posted by on 15 December 2012 - 12:22 PM

I remember in Metal Gear Solid you had a memory alloy key that changed its shape depending on the environment temperature. You had to visit a cold and a hot room to get the right shape and open a given lock. That was a very simple yet interesting mechanic.

I'm developing my own lockpicking mini-game with construction kit in Unity:

Posted Image

It has 4 tools, and 6 different components.

#5006933 is object pascal a good language to use?

Posted by on 03 December 2012 - 09:03 PM

I loved Object Pascal and everything related to it: Delphi, the FPC and Lazarus. Indeed I was building a multiplatform engine with OGL and DX support a few years ago, but unfortunately I finally realized that the libraries are a huge mess, full of bugs and missing important features, and then I moved to something else. That was very sad to me :(

#5006090 Is a new age of bad design coming? (MMOs)

Posted by on 01 December 2012 - 02:48 PM

I'm silently reading all the great contributions to this post, thanks guys! :)

And now I found this recent article. Quote: "Production Problems Made it Tough to Find the Fun" ... "We really underestimated how long things would take to get right, and how much would have to be invested in... the core loops and making it fun."

#5004564 Is a new age of bad design coming? (MMOs)

Posted by on 27 November 2012 - 10:59 AM

I hope to create some controversy, with probably wrong and biased ideas, to produce a nice debate :) So it seems to me that MMOs are going the wrong way, and that's because we are being able to reach the "massive" part of it. I'll explain in sections:

Too massive for so few content

The designer will never be able to generate enough content to cope with the population of the game. For example if you are supposed to destroy some computer terminals, but some other people are there and destroyed them first, you are forced sit there until they rematerialized, so you could destroy them again. Bottlenecks.

Procedural scramblers

Procedural generation could help the designer. But the problem with this content is that the designer is limited with a given set of parts that match together. Basically he is just scrambling pieces of terrain, buildings, characters, items, quests, dialogs, etc. After playing a few times the player will find out that the game is always the same, just things are placed in different positions. And the player will focus on finding where did the useful pieces fall, like in a hide and seek game, making the game repetitive. If you go to the extreme of procedural generation, the world will become too weird for the player to understand.

Perpetual tutorial

So to avoid bottlenecks there must be more roads, then the complexity of the world increases. Now, the designer is required to ease the gameplay with guided tours thru the world, but in increasingly complex worlds this is being taken to the extreme of transforming the whole game experience into the longest tutorials. Basically you get a message that tells you to go somewhere and do something. You do this, you get a pat on the back, then you're told to go do something else. Ad infinitum.


Is people actually interested in seen an overcrowded game? Maybe having overly crowded games just make them real in all the wrong ways. Maybe it's the ambition of game designers that actually turned into bad design, something that we are realizing until now that technology is capable of taking us to this extreme. And maybe MMOs should be more single-player. Even if the game is an MMO, you should be able to play alone and affect your own copy of the world. You could still invite your friends to play in your copy of the world to socialize, which to me it's the important feature of an MMO.


#5003738 Design: Hellish Administrator. Feedback?

Posted by on 24 November 2012 - 07:28 AM

It would be fun to have something like Dungeon Keeper's torture chambers and prisions Posted Image

#5000049 What techniques do you use to balance your game?

Posted by on 11 November 2012 - 06:25 PM

This might be interesting for you: