nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.55k stars 1.47k forks source link

Slow compile time objects with seq[int] field for C/C++ target in release mode #23480

Open nitely opened 6 months ago

nitely commented 6 months ago

Description

C/C++ backend takes +30 min to compile the below example in release mode. For JS backend it takes 0.5s to compile. It only takes so long in release mode. Debug mode takes 15s. Original issue reported in the nim-regex package.

type
  Node = object
    s: seq[int]
    s1: seq[int]
    s2: seq[int]
    s3: seq[int]
    s4: seq[int]

proc bigRegex(): seq[Node] =
  for x in 0 .. 20_000:
    result.add Node()

proc main() =
  const br = bigRegex()
  static: echo br[0]
  echo br[0]
main()

Nim Version

Nim Compiler Version 2.1.1 [Linux: amd64] Compiled at 2024-04-06 Copyright (c) 2006-2024 by Andreas Rumpf

git hash: 8c9fde76b547f19e3663d71d4ea644cee700d130 active boot switches: -d:release

Current Output

No response

Expected Output

No response

Possible Solution

No response

Additional Information

Replacing seq[int] by seq[int8] is also slow. Replacing seq[int8] by string, which should be a fairly similar data structure compiles fast-ish.

ringabout commented 6 months ago

It is not the Nim compiler that is slow to generate C code. The C compiler seems to have a hard time compiling the huge array generated in the c code. The other problem is that Nim generate massive types for the elements of seqs.

It generates 300k lines C code

Araq commented 6 months ago

@ringabout But it still indicates that the Nim compiler produces unreasonable C code.

ringabout commented 6 months ago

Quit using const specifier for array/seq seems to make it faster

https://github.com/crystal-lang/crystal/issues/2485

https://stackoverflow.com/questions/9110511/compilation-time-is-huge-for-statically-initialized-large-arraye-g-65536-entry