Hi,
After putting cast
here and there in my Typed Racket code, I got curious to know how much it cost me in terms of runtime performance:
#lang typed/racket
(time (for ([i (in-range 10000)])
(+ 1 i)))
(time (for ([i (in-range 10000)])
(+ 1 (cast i Integer))))
cpu time: 0 real time: 0 gc time: 0
cpu time: 18 real time: 18 gc time: 2
Oh. It actually tends to get somewhat worse when I reload the same code multiple times (tested mainly in Racket Mode, but also in DrRacket):
cpu time: 0 real time: 0 gc time: 0
cpu time: 18 real time: 18 gc time: 2
>
cpu time: 0 real time: 0 gc time: 0
cpu time: 20 real time: 20 gc time: 3
>
cpu time: 0 real time: 0 gc time: 0
cpu time: 35 real time: 35 gc time: 18
>
cpu time: 0 real time: 0 gc time: 0
cpu time: 39 real time: 39 gc time: 22
>
cpu time: 0 real time: 0 gc time: 0
cpu time: 69 real time: 69 gc time: 51
>
cpu time: 0 real time: 0 gc time: 0
cpu time: 28 real time: 28 gc time: 12
>
cpu time: 0 real time: 0 gc time: 0
cpu time: 20 real time: 20 gc time: 3
>
cpu time: 0 real time: 0 gc time: 0
cpu time: 26 real time: 26 gc time: 8
Well, the docs did tell me that:
The value is actually protected with two contracts. The second contract checks the new type, but the first contract is put there to enforce the old type, to protect higher-order uses of the value.
As I understand it, the performance penalty is therefore the effect of the contracts cast
imposes.
Is there a way to manage the performance penalty of cast
?
I find myself doing lots of conversions between Integer
and Positive-Integer
or Nonnegative-Integer
, and I am getting vaguely worried.