Hacker News new | past | comments | ask | show | jobs | submit login

Hi Jasper,

well, I'm also guilty of a Haskell dcpu16 emulator ;).

The memory access using Memory should be pretty fast, but what is the overhead of the MonadEmulator type class, of their load/store functions?

I used a Vector of unboxed Word16 for the ram and the ST monad to modfiy them.

   import qualified Data.Vector.Unboxed as V
   import qualified Data.Vector.Unboxed.Mutable as VM
   
   set :: Integral a => V.Vector Word16 -> a -> Word16 -> V.Vector Word16
   set vector index value = runST (setM vector (fromIntegral index) value)
      where
         setM vec i val = do
            mVec <- V.unsafeThaw vec
            VM.write mVec i val
            V.unsafeFreeze mVec 

Have you a feeling, can you estimate the performance difference of using a Vector like this and your Memory?

Greetings, Daniel




I'm pretty sure there will be no overhead from the type class when the appropriate SPECIALIZE and INLINE pragmas are added, and compiled with -O2. I haven't taken the time to optimize it yet because I haven't had performance problems so far.

Your Vector implementation should offer roughly the same performance. It might be an unnoticeable bit slower because Vector does bounds checking, and my Memory module doesn't (out-of-bounds access is impossible because of the ADT I added).

One thing I'd like to comment on is that I think you should revise your use of unsafeThaw/unsafeFreeze: you don't easily get guarantees of determinism when using these unsafe functions.


"One thing I'd like to comment on is that I think you should revise your use of unsafeThaw/unsafeFreeze: you don't easily get guarantees of determinism when using these unsafe functions."

Unfortunately that seems to be the only way to get a mutable Vector from a immutable one without copying it. As long as the mutable version of the vector isn't used anymore after calling set, it should be safe. But yes, guarantees are always nicer.

Nice to learn about the SPECIALIZE pragma. :)




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: