EgonOlsen71 / basicv2

A Commodore (CBM) BASIC V2 interpreter/compiler written in Java
https://egonolsen71.github.io/basicv2/
The Unlicense
84 stars 15 forks source link

optimize real variable assignment from constant #20

Closed nippur72 closed 5 years ago

nippur72 commented 5 years ago

The statement B=5 is rendered as (opts=true):

LDA #<CONST_0R
LDY #>CONST_0R
JSR REALFAC
LDX #<VAR_B
LDY #>VAR_B
JSR FACMEM

that is: load constant into FAC and then from FAC to var B (if my understanding is right)

I think that could be optimized with the equivalent:

ldx #4
loop:
  lda CONST_0R, x
  sta VAR_B, x
  dex
  bpl loop

which is faster and also shorter (11 vs 14 bytes). The only downside would be the use of the X register.

EgonOlsen71 commented 5 years ago

Seems reasonable. But it's pretty obvious, so I wonder why I don't do it already. Maybe there's a catch like the value actually needed in the FAC in some cases or something like that. I'll look into it...

EgonOlsen71 commented 5 years ago

I've added the optimization as suggested. I don't see any negative side effects of it, so all is well. It doesn't really help much performance wise though. In my fractal test case, it improved performance by 0.1% but it also shaved off a few bytes so there's no reason not to apply it.

nippur72 commented 5 years ago

great thanks!