Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile time Table[A, B] lookup performance #12195

Closed
zevv opened this issue Sep 15, 2019 · 1 comment
Closed

Compile time Table[A, B] lookup performance #12195

zevv opened this issue Sep 15, 2019 · 1 comment
Assignees
Labels
Standard Library VM see also `const` label

Comments

@zevv
Copy link
Contributor

zevv commented Sep 15, 2019

Trying to pinpoint performance issues in my code I found that table lookups at compile are suffering from some strange behaviour: the snippet below places 1000 objects in a table and looks them up again. Filling the table is fast, but the 1000 lookups take about 12 seconds on my machine. The interesting part is that lookup time does not seem related to the key, but only to the object size: doubling the size of array a in the object Flop will result in roughly double the compile time.

import tables

type Flop = object 
  a: array[128, int]  # <-- compile time is proportional to array size

proc hop(): bool =
  var v: Table[int, Flop]

  echo "create"
  for i in 1..1000:
    v.add i, Flop()

  echo "search"
  for i in 1..1000:
    discard contains(v, i)

  echo "done"

const r = hop()
@mratsim mratsim added Standard Library VM see also `const` label labels Sep 15, 2019
@krux02
Copy link
Contributor

krux02 commented Sep 16, 2019

I did some investigations, and it seems that the compiler uses most of the time copying values around. This seems to be related to value types in Nim and the VM. You can work around this problem my not using value types (TableRef).

import tables

type Flop = object
  a: array[128, int]

proc hop(): bool =
  var v = newTable[int, Flop]()

  echo "create"
  for i in 1..1000:
    v.add i, Flop()

  echo "search"
  for i in 1..1000:
    discard hasKey(v, i)

  echo "done"

const r = hop()

This program is now instant again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Standard Library VM see also `const` label
Projects
None yet
Development

No branches or pull requests

3 participants