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

Very slow compiling when computing big constant arrays #19075

Closed
tsoj opened this issue Oct 31, 2021 · 1 comment · Fixed by #21318
Closed

Very slow compiling when computing big constant arrays #19075

tsoj opened this issue Oct 31, 2021 · 1 comment · Fixed by #21318
Labels
Performance VM see also `const` label

Comments

@tsoj
Copy link

tsoj commented Oct 31, 2021

The following example takes almost 4 minutes to compile on an i5-8250U.

Example

const size = 50_000

const stuff = block:
    var a: array[size, int]
    a

const zeugs = block:
    var zeugs: array[size, int]
    for i in 0..<size:
        zeugs[i] = stuff[i]
    zeugs

echo zeugs[0]

Current Output

$ time nim c --run main.nim
Hint: used config file '/home/tsoj/.choosenim/toolchains/nim-#devel/config/nim.cfg' [Conf]
Hint: used config file '/home/tsoj/.choosenim/toolchains/nim-#devel/config/config.nims' [Conf]
.........................................................
CC: stdlib_digitsutils.nim
CC: stdlib_assertions.nim
CC: stdlib_dollars.nim
CC: stdlib_io.nim
CC: stdlib_system.nim
CC: main.nim
Hint:  [Link]
Hint: gc: refc; opt: none (DEBUG BUILD, `-d:release` generates faster code)
26631 lines; 237.035s; 145.992MiB peakmem; proj: /home/tsoj/Dokumente/Projects/nim-slow-compile-test/main.nim; out: /home/tsoj/Dokumente/Projects/nim-slow-compile-test/main [SuccessX]
Hint: /home/tsoj/Dokumente/Projects/nim-slow-compile-test/main  [Exec]
0

________________________________________________________
Executed in  237.21 secs    fish           external
   usr time  236.79 secs    1.02 millis  236.79 secs
   sys time    0.23 secs    0.59 millis    0.23 secs

Expected Output

I would expect the compilation to take less time. The following example compiles in less than one second, despite having equally big constant arrays:

import random

const size = 50_000

const stuff = block:
    var a: array[size, int]
    a

const zeugs = block:
    var zeugs: array[size, int]
    for i in 0..<size:
        zeugs[i] = i
    zeugs

echo stuff[rand(0..<size)]
echo zeugs[rand(0..<size)]

Additional information

$ nim -v
Nim Compiler Version 1.7.1 [Linux: amd64]
Compiled at 2021-10-31
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 2bda4a30a6b12dd0840dc347e454e54fe26721e7
active boot switches: -d:release
@jackhftang
Copy link
Contributor

Not a solution, but to mitigate the problem, move the const stuff outside for loop.

const size = 50_000

const stuff = block:
    var a: array[size, int]
    a

const zeugs = block:
    let inline = stuff
    var zeugs: array[size, int]
    for i in 0..<size:
        zeugs[i] = inline[i]
    zeugs

echo zeugs[1]

@ringabout ringabout added Performance VM see also `const` label labels Feb 8, 2022
ringabout added a commit that referenced this issue Jan 31, 2023
Araq pushed a commit that referenced this issue Jan 31, 2023
…t inline them in the VM; big performance boost (#21318)

* don't inline arrays in VM

* add a test for #19075
survivorm pushed a commit to survivorm/Nim that referenced this issue Feb 28, 2023
…stant seqs; don't inline them in the VM; big performance boost (nim-lang#21318)

* don't inline arrays in VM

* add a test for nim-lang#19075
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
…stant seqs; don't inline them in the VM; big performance boost (nim-lang#21318)

* don't inline arrays in VM

* add a test for nim-lang#19075
narimiran pushed a commit that referenced this issue Apr 26, 2023
…t inline them in the VM; big performance boost (#21318)

* don't inline arrays in VM

* add a test for #19075

(cherry picked from commit b5f64f5)
narimiran pushed a commit that referenced this issue Apr 26, 2023
…t inline them in the VM; big performance boost (#21318)

* don't inline arrays in VM

* add a test for #19075

(cherry picked from commit b5f64f5)
bung87 pushed a commit to bung87/Nim that referenced this issue Jul 29, 2023
…stant seqs; don't inline them in the VM; big performance boost (nim-lang#21318)

* don't inline arrays in VM

* add a test for nim-lang#19075
narimiran added a commit that referenced this issue Sep 14, 2023
…qs; don't inline them in the VM; big performance boost (#21318)"

This reverts commit 7ad8c44.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Performance VM see also `const` label
Projects
None yet
3 participants