treeform / flippy

Flippy is a simple 2d image and drawing library.
MIT License
59 stars 9 forks source link

Loops in flippy access memory discontinuously #26

Closed demotomohiro closed 4 years ago

demotomohiro commented 4 years ago

In procs in src/flippy.nim, most loop change y coordinate in inner loop and change x coordinate in outer loop. That means these loops access memory discontinuously. I think inner loop and outer loop in src/flippy.nim should be swapped for continuous memory access and better performance.

I swapped inner loop and outer loop of flippy.blit and measured time.

test.nim:

import flippy, times, vmath, chroma

template measure(body: untyped) =
  let before = epochTime()
  body
  let after = epochTime()

  echo after - before, "sec"

#changed inner loop and outer loop of flippy.blit for continuous memory access
proc blit2(destImage: Image, srcImage: Image, pos: Vec2) =
  for y in 0..<int(srcImage.height):
    for x in 0..<int(srcImage.width):
      var rgba = srcImage.getRgba(x, y)
      destImage.putRgbaSafe(int(pos.x) + x, int(pos.y) + y, rgba)

proc main =
  const
    width = 4000
    height = 4000

  var
    src = newImage(width, height, 4)
    dst = newImage(width, height, 4)

  for y in 0..<height:
    for x in 0..<width:
      let v = uint8((x xor y) and 0xff)
      src.putRgba(x, y, ColorRGBA(r: v, g: v, b: v, a: 255))

  measure:
    blit(dst, src, vec2(0.0, 0.0))
    #blit2(dst, src, vec2(0.0, 0.0))

  dst.save("test.png")

main()

I compiled it with following command:

nim c -r -d:danger test.nim

When I measured flippy.blit, time was about 0.42 seconds. When I measured blit2, time was about 0.083 seconds.

>nim -v
Nim Compiler Version 1.2.6 [Windows: amd64]
Compiled at 2020-07-29
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: bf320ed172f74f60fd274338e82bdc9ce3520dd9
active boot switches: -d:release
treeform commented 4 years ago

You are totally right!

Committed a fix and start working on benchmark file:

https://github.com/treeform/flippy/commit/7bf61d3fe81f9d51cb3bc7fe56165daa79ede8d8

treeform commented 4 years ago

I have speed it blit speed another x5 by using copyMem:

https://github.com/treeform/flippy/commit/ff3ef9d5a74f44835928674cc017c3cee5508d10