YesNoOk
avatar

Arithmetic optimization: what does it mean in MUGEN and does it matter? (Read 4025 times)

Started by Jesuszilla, March 22, 2017, 05:31:15 am
Share this topic:
Arithmetic optimization: what does it mean in MUGEN and does it matter?
#1  March 22, 2017, 05:31:15 am
  • ******
  • Loyal to the Game
    • USA
    • http://jesuszilla.trinitymugen.net/
First, let me give you a little bit of context as to what the following simplified buffering helper snippet does:
Code:
[State 10380, Charge Back]
type = VarAdd
triggerall = var(24) = [256,255+48]
trigger1 = p2dist X >= 0
trigger1 = command = "holdback"
trigger2 = p2dist X < 0
trigger2 = command = "holdfwd"
var(24) = -1

Now if you don't know why I'm starting at 256, read my previous tutorial on binary logic. But basically, I'm using the lower 8 bits to store the buffer time, meaning I can store a buffer time of up to 255 ticks. Powers of 2 are easier to align but I digress.

Four years ago, I would've gone "Don't make the machine do unnecessary work! Just type:"
Code:
triggerall = var(24) = (255,303]

But years of experience has taught me one thing: who gives a shit? Computers certainly don't. Addition is typically one assembly instruction. Has been for a long time. Hell, so is multiplication. What's really tough for computers is division, but unless you're 3D modeling or performing complex matrix operations (which I challenge you to try in MUGEN, and I'm not just being a smartass, it would legitimately be interesting to see something as cool as Bia's Bézier curves again), it really doesn't matter. This is MUGEN, after all.

We hear a lot about optimization in MUGEN, as we do in all of computing. However, what many seem to misunderstand is that optimization in MUGEN is less about time performance and more about readability and using as few variables as possible. The latter is why many of creators use bitwise operators. But this can make readability a bit of a pain.... to people who don't know much about computing. Which is nearly everyone, even if they've been doing MUGEN creation for several years.

So we have to compensate for this usage with readability. This is why even statements such as:
Code:
[State 10380, Charge Back]
type = VarAdd
triggerall = var(24) = [256,255+48] ; 48 ticks
trigger1 = p2dist X >= 0
trigger1 = (var(3)&68) > 0  ; holdback
trigger2 = p2dist X < 0
trigger2 = (var(3)&136) > 0 ; holdfwd
var(24) = -1

... are actually much more readable than statements like:
Code:
[State 10380, Charge Back]
type = VarAdd
triggerall = var(24) = [256,303]
trigger1 = p2dist X >= 0
trigger1 = command = "holdback"
trigger2 = p2dist X < 0
trigger2 = command = "holdfwd"
var(24) = -1

And guess what? They have minimal, if any effect, on performance! Even though MUGEN is a scripting language, its parser isn't that bad. Just get over it, it's easier to modify and understand in this context.

"But I can do basic addition and subtraction in my head!"
Good for you. But it's much more readable when I tell you exactly how many ticks are being used, and it's clearer when I show this with using the exact number of ticks from the base value. And it costs your processor nothing.

"But the binary statements for commands are scary!"
I tell you what they are and it's clear what they do. It's just like reading "command = whatever." You'd be surprised how quickly you can memorize (and how long you can retain) numbers! Besides, you don't have to do this, nor do I recommend it if you don't understand it completely (when Warusaki switched to using bitwise operators, I didn't switch immediately either; it wasn't until I took university courses that I fully understood the use of these functions).
Last Edit: March 22, 2017, 04:03:14 pm by Jesuszilla
Re: Arithmetic optimization: what does it mean in MUGEN and does it matter?
#2  March 22, 2017, 02:30:05 pm
  • ******
  • Double-Crosser
  • I'm not standing out. This isn't weird at all.
    • USA
To an outsider, those commands look nothing alike unless you just happen to know that the "easy" one references your command buffering system, and if someone just takes your word for it and just copy-pastes it without implementing said system, they'll find their attack doesn't work.

ink

Re: Arithmetic optimization: what does it mean in MUGEN and does it matter?
#3  March 22, 2017, 03:16:07 pm
  • ****
  • inktrebuchet
    • USA
    • s236.photobucket.com/albums/ff271/inktrebuchet/?
"But the binary statements for commands are scary!"
I tell you what they are and it's clear what they do. It's just like reading "command = whatever." You'd be surprised how quickly you can memorize (and how long you can retain) numbers! Besides, you don't have to do this, nor do I recommend it if you don't understand it completely (when Warusaki switched to using bitwise operators, I didn't switch immediately either; it wasn't until I took university courses that I fully understood the use of these functions).
This is me. No matter how many times I have read through those topics, I still can not wrap my head around this idea. It's just over my head at this point and it drives me crazy! I think I may need those university courses too...

Re: Arithmetic optimization: what does it mean in MUGEN and does it matter?
#4  March 22, 2017, 09:25:10 pm
  • *****
  • Shame on you!
    • USA
This reminds me a lot of the 6502 stuff I was working with on my Metroid.nes hack. I was working with 8bits to control the title screen's effects.

One thing that's not really explained is the "&"
trigger2 = (var(3)&136) > 0 ; holdfwd

In your demystified thread you have
[State -2, Powers of Two]
type = VarAdd
trigger1 = !(Var(4)&2)
var(4) = 2

But it still doesn't really explain what & does.
vVv Wheat Stage Released vVv
Re: Arithmetic optimization: what does it mean in MUGEN and does it matter?
#5  March 23, 2017, 01:33:11 am
  • ******
  • Loyal to the Game
    • USA
    • http://jesuszilla.trinitymugen.net/
Then read my bitwise shifting: demystified thread.
Re: Arithmetic optimization: what does it mean in MUGEN and does it matter?
#6  March 26, 2017, 12:24:07 am
  • *
    • Poland
    • mugen.samouczek.com
I do not care ;) hehe, but seriously arithmetic optimization is one of the optimizations that can be made.
Does it matter? - only if you are doing something more complicated than what Mugen was created for.
Re: Arithmetic optimization: what does it mean in MUGEN and does it matter?
#7  March 29, 2017, 11:38:42 pm
  • ******
  • Loyal to the Game
    • USA
    • http://jesuszilla.trinitymugen.net/
That's exactly what I'm saying, you really shouldn't care about optimizing something that doesn't need it. In programming languages that are actually compiled, the compiler usually does that for you, so it's better to have readable code.
Re: Arithmetic optimization: what does it mean in MUGEN and does it matter?
#8  March 30, 2017, 01:00:32 am
  • *
    • Poland
    • mugen.samouczek.com
But I will admit that I mix a bit in the code, sometimes, and sometimes you will notice some speed drops, but rather on a weak computer, I noticed this on a computer where I sometimes had problems with full HD add-ons. So Mugen, despite the complexity of the code, somehow does well. So You can play it a bit, Especially today.
https://www.youtube.com/watch?v=g17TR2uhdWI&list=PLFJWyafHBVM1zF6M88c2RxVkalWOPJElL&index=1
Code:
[State 0, VarSet]
type = VarSet
triggerall = Var(14) = 0 && helper(4),Vel Y > 0
triggerall = helper(4),RootDist Y - helper(4),Const(size.ground.front) - helper(4),Vel Y < RootDist Y + Const(size.height)
triggerall = helper(4),RootDist X*-1 + (helper(4),Const(size.height)/2) > RootDist X*-1
triggerall = helper(4),RootDist X*-1 - (helper(4),Const(size.height)/2) < RootDist X*-1 + Const(size.ground.front)
trigger1 = helper(4),RootDist X*-1 < RootDist X*-1 + (Const(size.ground.front)/2)
trigger1 = helper(4),RootDist Y > RootDist Y + Const(size.height) - ( (Const(size.height)/Const(size.ground.front))*(helper(4),RootDist X*-1 - RootDist X*-1) )
trigger2 = helper(4),RootDist X*-1 >= RootDist X*-1 + (Const(size.ground.front)/2)
trigger2 = helper(4),RootDist Y > RootDist Y + Const(size.height) - ( (Const(size.height)/Const(size.ground.front))*(RootDist X*-1 + Const(size.ground.front) - helper(4),RootDist X*-1) )
v = 14
value = 2


[State 300, blok kierunku w dol]
type = VarSet
trigger1 = 1;root,command = "holddown"
v = 3
value = helper(16),Var(2)+helper(17),Var(2)+helper(18),Var(2)+helper(19),Var(2)+helper(20),Var(2)+helper(21),Var(2)+helper(22),Var(2)+helper(23),Var(2)+helper(24),Var(2)+helper(25),Var(2)+helper(26),Var(2)+helper(27),Var(2)+helper(28),Var(2)+helper(29),Var(2)+helper(30),Var(2)+helper(31),Var(2)+helper(32),Var(2)+helper(33),Var(2)+helper(34),Var(2)+helper(35),Var(2)+helper(36),Var(2)+helper(37),Var(2)+helper(38),Var(2)+helper(39),Var(2)+helper(40),Var(2)+helper(41),Var(2)+helper(42),Var(2)+helper(43),Var(2)+helper(44),Var(2)+helper(45),Var(2)+helper(46),Var(2)+helper(47),Var(2)+helper(48),Var(2)+helper(49),Var(2)+helper(50),Var(2)


[State 401, 13 pocisk leci Y]
type = VarSet
triggerall = Var(22) != 0
trigger1 = (Var(22)-(floor(Var(22)/10)*10)) = 3 || (Var(22)-(floor(Var(22)/10)*10)) = 6 || (Var(22)-(floor(Var(22)/10)*10)) = 7
fv = 25
value = fVar(25) + ( root,const(velocity.run.fwd.x) * IfElse(((Var(22)-(floor(Var(22)/10)*10)) > 4),0.7,1) )


     Posted: March 30, 2017, 10:15:18 pm
Here is an example to check if anyone would like to look ;)

https://www.sendspace.com/file/co0w2b
Re: Arithmetic optimization: what does it mean in MUGEN and does it matter?
#9  March 31, 2017, 07:47:29 pm
  • ******
  • Loyal to the Game
    • USA
    • http://jesuszilla.trinitymugen.net/
I think in cases like that, it would make sense to store the helper info in a variable because RootDist is a function call. Function calling is an expensive operation in most languages, so you're calling a function each time you check helper(4), RootDist X for example. If you stored it in a variable, you'd only be calling the function once. It would also clean up the code a bit so you don't have long lines for triggers.

Also what does helper(x), Var(2) represent in those helpers? I have a feeling that VarSet could be replaced with a ParentVarSet in the helpers using bit flags.
Last Edit: March 31, 2017, 07:51:09 pm by Jesuszilla
Re: Arithmetic optimization: what does it mean in MUGEN and does it matter?
#10  April 01, 2017, 12:53:53 am
  • *
    • Poland
    • mugen.samouczek.com
Play in mugen 1.0 GAMES.rar
It's been a few years and I do not remember now, but I have to have verified data in real time. In this case, it is hard to optimize something. I was creating a engine in the mugen engine.  Everything has purpose there. Of course this is not the full version.