# Why does my code run significantly faster on BC than CS?

**URL:** https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720
**Category:** General
**Created:** [February 14, 2024, 11:40pm UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720 "2024-02-14T23:40:13Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![default.kramer](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@default.kramer](https://racket.discourse.group/u/default.kramer)
#### Post date: [February 14, 2024, 11:40pm UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/1 "2024-02-14T23:40:13Z")

</div>

I'm working on a NES emulator in Typed Racket and I've just gotten it complete enough to do some realistic performance testing. Racket 8.12 BC can run the emulator at 105 FPS, but the same code using 8.12 CS peaks at 45 FPS. (This is "headless" emulation speed, unaffected by racket/gui.)

The profiler showed me that it is the CPU emulation which is taking the majority of the time. This code really only does these few things:

1. Read and write RAM, using `unsafe-bytes-set!` and `unsafe-bytes-ref`
2. Fixnum arithmetic (eg. `unsafe-fx+` and `unsafe-fxior`)
3. Conditionals based on fixnum comparisons

Is it possible that CS is actually more than 2x slower than BC for this kind of workload? Are there any CS-specific performance pitfalls to be aware of? If BC is compiled and CS is interpreted it would seem to explain things, but I thought CS was compiled also.

I'll be happy to share the code once I do a little cleanup if anyone is interested. Thanks in advance!

---

<div class="post-metadata">

### Author: ![jjsimpso](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jjsimpso/32/602_2.png) [@jjsimpso](https://racket.discourse.group/u/jjsimpso)
#### Post date: [February 15, 2024, 4:11am UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/2 "2024-02-15T04:11:35Z")

</div>

I'm no expert, but what platform did you run the performance tests on? CS compiles to native machine code(generally speaking, at least) while BC compiles to bytecode(again, generally speaking).

From what I understand, CS's native code is not as compact as BC bytecode. Perhaps the BC JIT compiler is more efficient in this case. Someone who knows more than me will have to comment on that possibility though.

---

<div class="post-metadata">

### Author: ![default.kramer](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@default.kramer](https://racket.discourse.group/u/default.kramer)
#### Post date: [February 15, 2024, 5:59am UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/3 "2024-02-15T05:59:34Z")

</div>

I ran it on Windows 10, x64 (Intel Core i7). Thanks for confirming that CS does compile to native machine code.

---

<div class="post-metadata">

### Author: ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)
#### Post date: [February 15, 2024, 6:00am UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/4 "2024-02-15T06:00:16Z")

</div>

I'm wondering if there's an unfortunate interaction between Typed Racket, CS, and unsafe operations. How hard would it be to strip the types out, just to see what difference it makes?

---

<div class="post-metadata">

### Author: ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)
#### Post date: [February 15, 2024, 6:01am UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/5 "2024-02-15T06:01:24Z")

</div>

Another random check: are you running this code using DrRacket, or by running it at the command-line?

---

<div class="post-metadata">

### Author: ![default.kramer](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@default.kramer](https://racket.discourse.group/u/default.kramer)
#### Post date: [February 15, 2024, 6:05am UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/6 "2024-02-15T06:05:54Z")

</div>

I tested it from the command line like `racket my-file.rkt`. When you say "strip the types out", do you mean change the #lang to racket? I don't think that will be too difficult, I'll try it out soon.

---

<div class="post-metadata">

### Author: ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)
#### Post date: [February 15, 2024, 6:08am UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/7 "2024-02-15T06:08:08Z")

</div>

Yes, that's what I mean.

---

<div class="post-metadata">

### Author: ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)
#### Post date: [February 15, 2024, 4:26pm UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/8 "2024-02-15T16:26:27Z")

</div>

Are you using the FFI at all? There are some ways of writing FFI code that would end up making copies of byte strings on CS, but not copying on BC.

---

<div class="post-metadata">

### Author: ![default.kramer](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@default.kramer](https://racket.discourse.group/u/default.kramer)
#### Post date: [February 15, 2024, 10:38pm UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/9 "2024-02-15T22:38:20Z")

</div>

Updated times:

- Typed, CS: 45 FPS (within 1 FPS every run)
- Untyped, CS: 50FPS (within 1 FPS every run)
- Typed, BC: 124-130 FPS
- Untyped, BC: 122-128 FPS

So it would seem that Typed Racket + CS is causing a bit of slowdown, but nothing major.

---

<div class="post-metadata">

### Author: ![default.kramer](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@default.kramer](https://racket.discourse.group/u/default.kramer)
#### Post date: [February 15, 2024, 10:39pm UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/10 "2024-02-15T22:39:17Z")

</div>

No, I'm not using FFI yet. But that's good to know as I expect I will need FFI if/when I try to implement the audio output.

---

<div class="post-metadata">

### Author: ![EmEf](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@EmEf](https://racket.discourse.group/u/EmEf)
#### Post date: [February 16, 2024, 1:36am UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/11 "2024-02-16T01:36:15Z")

</div>

John meant something different: whether TR and R interact in your program. If a program mixes R and TR, there are bad cases where the type-protection scheme imposes serious penalties (order of magnitude). This is not the case with your program.

;; - - -

Is it possible that your installation of Racket/CS did not compile the libraries?

---

<div class="post-metadata">

### Author: ![default.kramer](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@default.kramer](https://racket.discourse.group/u/default.kramer)
#### Post date: [February 16, 2024, 7:57pm UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/12 "2024-02-16T19:57:20Z")

</div>

How could I check whether the libraries were compiled or not?

---

<div class="post-metadata">

### Author: ![default.kramer](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@default.kramer](https://racket.discourse.group/u/default.kramer)
#### Post date: [February 16, 2024, 8:06pm UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/13 "2024-02-16T20:06:35Z")

</div>

I found the [Inspecting Compiler Passes](https://docs.racket-lang.org/reference/compiler.html#%28part._compiler-inspect%29) documentation and was able to view the linklet and the machine code that CS generates. Nothing jumps out at me, but that's mostly because my `emulate-one-instruction` procedure is very large and hard to read. Maybe BC is better than CS at optimizing large procedures? In any case, with this tool in hand I think I should be able to refactor the code starting with smaller, simpler functions and verifying the machine code at each step.

---

<div class="post-metadata">

### Author: ![jjsimpso](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jjsimpso/32/602_2.png) [@jjsimpso](https://racket.discourse.group/u/jjsimpso)
#### Post date: [February 16, 2024, 8:25pm UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/14 "2024-02-16T20:25:40Z")

</div>

Please share your results if you are able to improve the CS performance(or even if not).

---

<div class="post-metadata">

### Author: ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)
#### Post date: [February 16, 2024, 9:17pm UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/15 "2024-02-16T21:17:16Z")

</div>

For example: do the packages you have installed have `compiled` directories? You could try running `raco setup` with your CS installation to make sure everything is compiled.

---

<div class="post-metadata">

### Author: ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)
#### Post date: [February 17, 2024, 1:03am UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/16 "2024-02-17T01:03:47Z")

</div>

Whether the files are compiled or not - that will only affect the startup time.  
Here the issue is that the number of fps dropped.

---

<div class="post-metadata">

### Author: ![EmEf](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@EmEf](https://racket.discourse.group/u/EmEf)
#### Post date: [February 17, 2024, 1:40am UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/17 "2024-02-17T01:40:55Z")

</div>

It depends how the per-s is measured, say if it includes the start-up time and is about short runs.

---

<div class="post-metadata">

### Author: ![mfandl](https://avatars.discourse-cdn.com/v4/letter/m/7feea3/32.png) [@mfandl](https://racket.discourse.group/u/mfandl)
#### Post date: [February 17, 2024, 10:34am UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/18 "2024-02-17T10:34:08Z")

</div>

I do not know the details, but seeing "Maybe BC is better than CS at optimizing large procedures?" made this jump out of the depths of my memory:

> If you write the same program in Racket and in Chez, it will run at almost exactly the same speed, unless it has very very large functions that are nonetheless important to compile efficiently, in which case there is interpretation overhead

[discord post by samth](https://discord.com/channels/571040468092321801/618895179343986688/1152244683775430685)

but I guess from what I read above the long function was indeed compiled? maybe you could try setting `PLT_CS_COMPILE_LIMIT` to something larger and see if it makes any difference.

see [18.7&nbsp;Controlling and Inspecting Compilation](https://docs.racket-lang.org/reference/compiler.html#(part._cs-compiler-modes))

---

<div class="post-metadata">

### Author: ![default.kramer](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@default.kramer](https://racket.discourse.group/u/default.kramer)
#### Post date: [February 17, 2024, 5:54pm UTC](https://racket.discourse.group/t/why-does-my-code-run-significantly-faster-on-bc-than-cs/2720/19 "2024-02-17T17:54:48Z")

</div>

The `PLT_CS_COMPILE_LIMIT` did it! I bumped it up to 20000 and it now it runs very fast. Thanks everyone! I will make the code public pretty soon.
