Cosmos icon indicating copy to clipboard operation
Cosmos copied to clipboard

Long division is extremly slow

Open terminal-cs opened this issue 2 years ago • 13 comments

Area of Cosmos - What area of Cosmos are we dealing with?

Division, (implemented in x#)

Expected Behaviour - What do you think that should happen?

numbers should divide fast without much performance lost

Actual Behaviour - What unexpectedly happens?

significant speed impact (from 600 fps all the way down to 40)

Reproduction - How did you get this error to appear?

34671 / 1024 every frame

Version - Were you using the User Kit or Dev Kit? And what User Kit version or Dev Kit commit (Cosmos, IL2CPU, X#)?

devkit

terminal-cs avatar Jul 26 '22 17:07 terminal-cs

it looks like we are using the div opcode from nasm i dont know if we can make it faster in a way that makes sense https://github.com/CosmosOS/XSharp/blob/master/source/XSharp/XSharp/Assembler/Gen1/x86/Divide.cs

zarlo avatar Jul 27 '22 05:07 zarlo

The issue is with long not int, where we cannot use the div assembly instruction.

quajak avatar Jul 27 '22 06:07 quajak

Is this really a bug? Division works, it's just slow, so I don't think it can be considered a bug. @terminal-cs @quajak

AnErrupTion avatar Dec 18 '22 16:12 AnErrupTion

Well if its too slow to be practical then I would consider it a bug

quajak avatar Dec 18 '22 16:12 quajak

I'm not able to reproduce this bug. I'm even using C# division and have set fields to long, yet it did not impact performance. So, I'm assuming that using X# directly will not have any impact either. Could you give a bit more details please? @terminal-cs

AnErrupTion avatar Dec 18 '22 21:12 AnErrupTion

I'm not able to reproduce this bug. I'm even using C# division and have set fields to long, yet it did not impact performance. So, I'm assuming that using X# directly will not have any impact either. Could you give a bit more details please? @terminal-cs

I don't know what else i need to say here, all i know is that i divided a big number every frame and it had a huge performance impact

terminal-cs avatar Dec 18 '22 21:12 terminal-cs

I'm not able to reproduce this bug. I'm even using C# division and have set fields to long, yet it did not impact performance. So, I'm assuming that using X# directly will not have any impact either. Could you give a bit more details please? @terminal-cs

I don't know what else i need to say here, all i know is that i divided a big number every frame and it had a huge performance impact

Is the division that you gave (34671 / 1024) the actual one you did every frame or is it another one?

AnErrupTion avatar Dec 18 '22 22:12 AnErrupTion

I'm not able to reproduce this bug. I'm even using C# division and have set fields to long, yet it did not impact performance. So, I'm assuming that using X# directly will not have any impact either. Could you give a bit more details please? @terminal-cs

I don't know what else i need to say here, all i know is that i divided a big number every frame and it had a huge performance impact

Is the division that you gave (34671 / 1024) the actual one you did every frame or is it another one?

another one I belive, I was dividing some memory variable so get it in mb

terminal-cs avatar Dec 18 '22 23:12 terminal-cs

I'm not able to reproduce this bug. I'm even using C# division and have set fields to long, yet it did not impact performance. So, I'm assuming that using X# directly will not have any impact either. Could you give a bit more details please? @terminal-cs

I don't know what else i need to say here, all i know is that i divided a big number every frame and it had a huge performance impact

Is the division that you gave (34671 / 1024) the actual one you did every frame or is it another one?

another one I belive, I was dividing some memory variable so get it in mb

Ah, so you were getting the amount of memory... I'll try that soon.

AnErrupTion avatar Dec 18 '22 23:12 AnErrupTion

I tried with Result = GCImplementation.GetUsedRAM() / 1024; and yet my performance is practically the same (no major drop in performance).

AnErrupTion avatar Dec 19 '22 02:12 AnErrupTion

GetUsedRAM returns an uint not a long

quajak avatar Dec 19 '22 02:12 quajak

GetUsedRAM returns an uint not a long

Yeah but isn't it the way to get used RAM? It's also what OP used (at least, getting used RAM) for their division.

AnErrupTion avatar Dec 19 '22 02:12 AnErrupTion

Practically, 64-bit division while running in protected mode will always be slower than 32-bit division - just like 32-bit division is slower while in real mode. The only practical solution would be to use long mode instead. Until then, we can take a look at the GCC function _divdi3 and try to implement it in IL2CPU.

ascpixi avatar Feb 22 '23 13:02 ascpixi