nimc.rst 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. ===================================
  2. Nim Compiler User Guide
  3. ===================================
  4. :Author: Andreas Rumpf
  5. :Version: |nimversion|
  6. .. contents::
  7. "Look at you, hacker. A pathetic creature of meat and bone, panting and
  8. sweating as you run through my corridors. How can you challenge a perfect,
  9. immortal machine?"
  10. Introduction
  11. ============
  12. This document describes the usage of the *Nim compiler*
  13. on the different supported platforms. It is not a definition of the Nim
  14. programming language (which is covered in the `manual <manual.html>`_).
  15. Nim is free software; it is licensed under the
  16. `MIT License <http://www.opensource.org/licenses/mit-license.php>`_.
  17. Compiler Usage
  18. ==============
  19. Command line switches
  20. ---------------------
  21. Basic command line switches are:
  22. Usage:
  23. .. include:: basicopt.txt
  24. ----
  25. Advanced command line switches are:
  26. .. include:: advopt.txt
  27. List of warnings
  28. ----------------
  29. Each warning can be activated individually with ``--warning[NAME]:on|off`` or
  30. in a ``push`` pragma.
  31. ========================== ============================================
  32. Name Description
  33. ========================== ============================================
  34. CannotOpenFile Some file not essential for the compiler's
  35. working could not be opened.
  36. OctalEscape The code contains an unsupported octal
  37. sequence.
  38. Deprecated The code uses a deprecated symbol.
  39. ConfigDeprecated The project makes use of a deprecated config
  40. file.
  41. SmallLshouldNotBeUsed The letter 'l' should not be used as an
  42. identifier.
  43. EachIdentIsTuple The code contains a confusing ``var``
  44. declaration.
  45. User Some user defined warning.
  46. ========================== ============================================
  47. List of hints
  48. -------------
  49. Each hint can be activated individually with ``--hint[NAME]:on|off`` or in a
  50. ``push`` pragma.
  51. ========================== ============================================
  52. Name Description
  53. ========================== ============================================
  54. CC Shows when the C compiler is called.
  55. CodeBegin
  56. CodeEnd
  57. CondTrue
  58. Conf A config file was loaded.
  59. ConvToBaseNotNeeded
  60. ConvFromXtoItselfNotNeeded
  61. Dependency
  62. Exec Program is executed.
  63. ExprAlwaysX
  64. ExtendedContext
  65. GCStats Dumps statistics about the Garbage Collector.
  66. GlobalVar Shows global variables declarations.
  67. LineTooLong Line exceeds the maximum length.
  68. Link Linking phase.
  69. Name
  70. Path Search paths modifications.
  71. Pattern
  72. Performance
  73. Processing Artifact being compiled.
  74. QuitCalled
  75. Source The source line that triggered a diagnostic
  76. message.
  77. StackTrace
  78. Success, SuccessX Successful compilation of a library or a binary.
  79. User
  80. UserRaw
  81. XDeclaredButNotUsed Unused symbols in the code.
  82. ========================== ============================================
  83. Verbosity levels
  84. ----------------
  85. ===== ============================================
  86. Level Description
  87. ===== ============================================
  88. 0 Minimal output level for the compiler.
  89. 1 Displays compilation of all the compiled files, including those imported
  90. by other modules or through the `compile pragma
  91. <manual.html#implementation-specific-pragmas-compile-pragma>`_.
  92. This is the default level.
  93. 2 Displays compilation statistics, enumerates the dynamic
  94. libraries that will be loaded by the final binary and dumps to
  95. standard output the result of applying `a filter to the source code
  96. <filters.html>`_ if any filter was used during compilation.
  97. 3 In addition to the previous levels dumps a debug stack trace
  98. for compiler developers.
  99. ===== ============================================
  100. Compile time symbols
  101. --------------------
  102. Through the ``-d:x`` or ``--define:x`` switch you can define compile time
  103. symbols for conditional compilation. The defined switches can be checked in
  104. source code with the `when statement
  105. <manual.html#statements-and-expressions-when-statement>`_ and
  106. `defined proc <system.html#defined,untyped>`_. The typical use of this switch is
  107. to enable builds in release mode (``-d:release``) where optimizations are
  108. enabled for better performance. Another common use is the ``-d:ssl`` switch to
  109. activate SSL sockets.
  110. Additionally, you may pass a value along with the symbol: ``-d:x=y``
  111. which may be used in conjunction with the `compile time define
  112. pragmas<manual.html#implementation-specific-pragmas-compile-time-define-pragmas>`_
  113. to override symbols during build time.
  114. Compile time symbols are completely **case insensitive** and underscores are
  115. ignored too. ``--define:FOO`` and ``--define:foo`` are identical.
  116. Compile time symbols starting with the ``nim`` prefix are reserved for the
  117. implementation and should not be used elsewhere.
  118. Configuration files
  119. -------------------
  120. **Note:** The *project file name* is the name of the ``.nim`` file that is
  121. passed as a command line argument to the compiler.
  122. The ``nim`` executable processes configuration files in the following
  123. directories (in this order; later files overwrite previous settings):
  124. 1) ``$nim/config/nim.cfg``, ``/etc/nim/nim.cfg`` (UNIX) or ``<Nim's installation director>\config\nim.cfg`` (Windows). This file can be skipped with the ``--skipCfg`` command line option.
  125. 2) If environment variable ``XDG_CONFIG_HOME`` is defined, ``$XDG_CONFIG_HOME/nim/nim.cfg`` or ``~/.config/nim/nim.cfg`` (POSIX) or ``%APPDATA%/nim/nim.cfg`` (Windows). This file can be skipped with the ``--skipUserCfg`` command line option.
  126. 3) ``$parentDir/nim.cfg`` where ``$parentDir`` stands for any parent directory of the project file's path. These files can be skipped with the ``--skipParentCfg`` command line option.
  127. 4) ``$projectDir/nim.cfg`` where ``$projectDir`` stands for the project file's path. This file can be skipped with the ``--skipProjCfg`` command line option.
  128. 5) A project can also have a project specific configuration file named ``$project.nim.cfg`` that resides in the same directory as ``$project.nim``. This file can be skipped with the ``--skipProjCfg`` command line option.
  129. Command line settings have priority over configuration file settings.
  130. The default build of a project is a `debug build`:idx:. To compile a
  131. `release build`:idx: define the ``release`` symbol::
  132. nim c -d:release myproject.nim
  133. To compile a `dangerous release build`:idx: define the ``danger`` symbol::
  134. nim c -d:danger myproject.nim
  135. Search path handling
  136. --------------------
  137. Nim has the concept of a global search path (PATH) that is queried to
  138. determine where to find imported modules or include files. If multiple files are
  139. found an ambiguity error is produced.
  140. ``nim dump`` shows the contents of the PATH.
  141. However before the PATH is used the current directory is checked for the
  142. file's existence. So if PATH contains ``$lib`` and ``$lib/bar`` and the
  143. directory structure looks like this::
  144. $lib/x.nim
  145. $lib/bar/x.nim
  146. foo/x.nim
  147. foo/main.nim
  148. other.nim
  149. And ``main`` imports ``x``, ``foo/x`` is imported. If ``other`` imports ``x``
  150. then both ``$lib/x.nim`` and ``$lib/bar/x.nim`` match but ``$lib/x.nim`` is used
  151. as it is the first match.
  152. Generated C code directory
  153. --------------------------
  154. The generated files that Nim produces all go into a subdirectory called
  155. ``nimcache``. Its full path is
  156. - ``$XDG_CACHE_HOME/nim/$projectname(_r|_d)`` or ``~/.cache/nim/$projectname(_r|_d)``
  157. on Posix
  158. - ``$HOME/nimcache/$projectname(_r|_d)`` on Windows.
  159. The ``_r`` suffix is used for release builds, ``_d`` is for debug builds.
  160. This makes it easy to delete all generated files.
  161. The ``--nimcache``
  162. `compiler switch <#compiler-usage-command-line-switches>`_ can be used to
  163. to change the ``nimcache`` directory.
  164. However, the generated C code is not platform independent. C code generated for
  165. Linux does not compile on Windows, for instance. The comment on top of the
  166. C file lists the OS, CPU and CC the file has been compiled for.
  167. Compiler Selection
  168. ==================
  169. To change the compiler from the default compiler (at the command line)::
  170. nim c --cc:llvm_gcc --compile_only myfile.nim
  171. This uses the configuration defined in ``config\nim.cfg`` for ``lvm_gcc``.
  172. If nimcache already contains compiled code from a different compiler for the same project,
  173. add the ``-f`` flag to force all files to be recompiled.
  174. The default compiler is defined at the top of ``config\nim.cfg``.
  175. Changing this setting affects the compiler used by ``koch`` to (re)build Nim.
  176. Cross compilation
  177. =================
  178. To cross compile, use for example::
  179. nim c --cpu:i386 --os:linux --compileOnly --genScript myproject.nim
  180. Then move the C code and the compile script ``compile_myproject.sh`` to your
  181. Linux i386 machine and run the script.
  182. Another way is to make Nim invoke a cross compiler toolchain::
  183. nim c --cpu:arm --os:linux myproject.nim
  184. For cross compilation, the compiler invokes a C compiler named
  185. like ``$cpu.$os.$cc`` (for example arm.linux.gcc) and the configuration
  186. system is used to provide meaningful defaults. For example for ``ARM`` your
  187. configuration file should contain something like::
  188. arm.linux.gcc.path = "/usr/bin"
  189. arm.linux.gcc.exe = "arm-linux-gcc"
  190. arm.linux.gcc.linkerexe = "arm-linux-gcc"
  191. Cross compilation for Windows
  192. =============================
  193. To cross compile for Windows from Linux or macOS using the MinGW-w64 toolchain::
  194. nim c -d:mingw myproject.nim
  195. Use ``--cpu:i386`` or ``--cpu:amd64`` to switch the CPU architecture.
  196. The MinGW-w64 toolchain can be installed as follows::
  197. Ubuntu: apt install mingw-w64
  198. CentOS: yum install mingw32-gcc | mingw64-gcc - requires EPEL
  199. OSX: brew install mingw-w64
  200. Cross compilation for Android
  201. =============================
  202. There are two ways to compile for Android: terminal programs (Termux) and with
  203. the NDK (Android Native Development Kit).
  204. First one is to treat Android as a simple Linux and use
  205. `Termux <https://wiki.termux.com>`_ to connect and run the Nim compiler
  206. directly on android as if it was Linux. These programs are console only
  207. programs that can't be distributed in the Play Store.
  208. Use regular ``nim c`` inside termux to make Android terminal programs.
  209. Normal Android apps are written in Java, to use Nim inside an Android app
  210. you need a small Java stub that calls out to a native library written in
  211. Nim using the `NDK <https://developer.android.com/ndk>`_. You can also use
  212. `native-acitivty <https://developer.android.com/ndk/samples/sample_na>`_
  213. to have the Java stub be auto generated for you.
  214. Use ``nim c -c --cpu:arm --os:android -d:androidNDK --noMain:on`` to
  215. generate the C source files you need to include in your Android Studio
  216. project. Add the generated C files to CMake build script in your Android
  217. project. Then do the final compile with Android Studio which uses Gradle
  218. to call CMake to compile the project.
  219. Because Nim is part of a library it can't have its own c style ``main()``
  220. so you would need to define your own ``android_main`` and init the Java
  221. environment, or use a library like SDL2 or GLFM to do it. After the Android
  222. stuff is done, it's very important to call ``NimMain()`` in order to
  223. initialize Nim's garbage collector and to run the top level statements
  224. of your program.
  225. .. code-block:: Nim
  226. proc NimMain() {.importc.}
  227. proc glfmMain*(display: ptr GLFMDisplay) {.exportc.} =
  228. NimMain() # initialize garbage collector memory, types and stack
  229. Cross compilation for iOS
  230. =========================
  231. To cross compile for iOS you need to be on a MacOS computer and use XCode.
  232. Normal languages for iOS development are Swift and Objective C. Both of these
  233. use LLVM and can be compiled into object files linked together with C, C++
  234. or Objective C code produced by Nim.
  235. Use ``nim c -c --os:ios --noMain:on`` to generate C files and include them in
  236. your XCode project. Then you can use XCode to compile, link, package and
  237. sign everything.
  238. Because Nim is part of a library it can't have its own c style ``main()`` so you
  239. would need to define `main` that calls ``autoreleasepool`` and
  240. ``UIApplicationMain`` to do it, or use a library like SDL2 or GLFM. After
  241. the iOS setup is done, it's very important to call ``NimMain()`` in order to
  242. initialize Nim's garbage collector and to run the top level statements
  243. of your program.
  244. .. code-block:: Nim
  245. proc NimMain() {.importc.}
  246. proc glfmMain*(display: ptr GLFMDisplay) {.exportc.} =
  247. NimMain() # initialize garbage collector memory, types and stack
  248. Note: XCodes "make clean" gets confused about the genreated nim.c files,
  249. so you need to clean those files manually to do a clean build.
  250. Cross compilation for Nintendo Switch
  251. =====================================
  252. Simply add --os:nintendoswitch
  253. to your usual ``nim c`` or ``nim cpp`` command and set the ``passC``
  254. and ``passL`` command line switches to something like:
  255. .. code-block:: console
  256. nim c ... --passC="-I$DEVKITPRO/libnx/include" ...
  257. --passL="-specs=$DEVKITPRO/libnx/switch.specs -L$DEVKITPRO/libnx/lib -lnx"
  258. or setup a nim.cfg file like so:
  259. .. code-block:: Nim
  260. #nim.cfg
  261. --passC="-I$DEVKITPRO/libnx/include"
  262. --passL="-specs=$DEVKITPRO/libnx/switch.specs -L$DEVKITPRO/libnx/lib -lnx"
  263. The DevkitPro setup must be the same as the default with their new installer
  264. `here for Mac/Linux <https://github.com/devkitPro/pacman/releases>`_ or
  265. `here for Windows <https://github.com/devkitPro/installer/releases>`_.
  266. For example, with the above mentioned config::
  267. nim c --os:nintendoswitch switchhomebrew.nim
  268. This will generate a file called ``switchhomebrew.elf`` which can then be turned into
  269. an nro file with the ``elf2nro`` tool in the DevkitPro release. Examples can be found at
  270. `the nim-libnx github repo <https://github.com/jyapayne/nim-libnx.git>`_.
  271. There are a few things that don't work because the DevkitPro libraries don't support them.
  272. They are:
  273. 1. Waiting for a subprocess to finish. A subprocess can be started, but right
  274. now it can't be waited on, which sort of makes subprocesses a bit hard to use
  275. 2. Dynamic calls. DevkitPro libraries have no dlopen/dlclose functions.
  276. 3. Command line parameters. It doesn't make sense to have these for a console
  277. anyways, so no big deal here.
  278. 4. mqueue. Sadly there are no mqueue headers.
  279. 5. ucontext. No headers for these either. No coroutines for now :(
  280. 6. nl_types. No headers for this.
  281. DLL generation
  282. ==============
  283. Nim supports the generation of DLLs. However, there must be only one
  284. instance of the GC per process/address space. This instance is contained in
  285. ``nimrtl.dll``. This means that every generated Nim DLL depends
  286. on ``nimrtl.dll``. To generate the "nimrtl.dll" file, use the command::
  287. nim c -d:release lib/nimrtl.nim
  288. To link against ``nimrtl.dll`` use the command::
  289. nim c -d:useNimRtl myprog.nim
  290. **Note**: Currently the creation of ``nimrtl.dll`` with thread support has
  291. never been tested and is unlikely to work!
  292. Additional compilation switches
  293. ===============================
  294. The standard library supports a growing number of ``useX`` conditional defines
  295. affecting how some features are implemented. This section tries to give a
  296. complete list.
  297. ====================== =========================================================
  298. Define Effect
  299. ====================== =========================================================
  300. ``release`` Turns on the optimizer.
  301. More aggressive optimizations are possible, eg:
  302. ``--passC:-ffast-math`` (but see issue #10305)
  303. ``danger`` Turns off all runtime checks and turns on the optimizer.
  304. ``useFork`` Makes ``osproc`` use ``fork`` instead of ``posix_spawn``.
  305. ``useNimRtl`` Compile and link against ``nimrtl.dll``.
  306. ``useMalloc`` Makes Nim use C's `malloc`:idx: instead of Nim's
  307. own memory manager, albeit prefixing each allocation with
  308. its size to support clearing memory on reallocation.
  309. This only works with ``gc:none`` and
  310. with ``--newruntime``.
  311. ``useRealtimeGC`` Enables support of Nim's GC for *soft* realtime
  312. systems. See the documentation of the `gc <gc.html>`_
  313. for further information.
  314. ``logGC`` Enable GC logging to stdout.
  315. ``nodejs`` The JS target is actually ``node.js``.
  316. ``ssl`` Enables OpenSSL support for the sockets module.
  317. ``memProfiler`` Enables memory profiling for the native GC.
  318. ``uClibc`` Use uClibc instead of libc. (Relevant for Unix-like OSes)
  319. ``checkAbi`` When using types from C headers, add checks that compare
  320. what's in the Nim file with what's in the C header
  321. (requires a C compiler with _Static_assert support, like
  322. any C11 compiler)
  323. ``tempDir`` This symbol takes a string as its value, like
  324. ``--define:tempDir:/some/temp/path`` to override the
  325. temporary directory returned by ``os.getTempDir()``.
  326. The value **should** end with a directory separator
  327. character. (Relevant for the Android platform)
  328. ``useShPath`` This symbol takes a string as its value, like
  329. ``--define:useShPath:/opt/sh/bin/sh`` to override the
  330. path for the ``sh`` binary, in cases where it is not
  331. located in the default location ``/bin/sh``.
  332. ``noSignalHandler`` Disable the crash handler from ``system.nim``.
  333. ====================== =========================================================
  334. Additional Features
  335. ===================
  336. This section describes Nim's additional features that are not listed in the
  337. Nim manual. Some of the features here only make sense for the C code
  338. generator and are subject to change.
  339. LineDir option
  340. --------------
  341. The ``lineDir`` option can be turned on or off. If turned on the
  342. generated C code contains ``#line`` directives. This may be helpful for
  343. debugging with GDB.
  344. StackTrace option
  345. -----------------
  346. If the ``stackTrace`` option is turned on, the generated C contains code to
  347. ensure that proper stack traces are given if the program crashes or an
  348. uncaught exception is raised.
  349. LineTrace option
  350. ----------------
  351. The ``lineTrace`` option implies the ``stackTrace`` option. If turned on,
  352. the generated C contains code to ensure that proper stack traces with line
  353. number information are given if the program crashes or an uncaught exception
  354. is raised.
  355. DynlibOverride
  356. ==============
  357. By default Nim's ``dynlib`` pragma causes the compiler to generate
  358. ``GetProcAddress`` (or their Unix counterparts)
  359. calls to bind to a DLL. With the ``dynlibOverride`` command line switch this
  360. can be prevented and then via ``--passL`` the static library can be linked
  361. against. For instance, to link statically against Lua this command might work
  362. on Linux::
  363. nim c --dynlibOverride:lua --passL:liblua.lib program.nim
  364. Backend language options
  365. ========================
  366. The typical compiler usage involves using the ``compile`` or ``c`` command to
  367. transform a ``.nim`` file into one or more ``.c`` files which are then
  368. compiled with the platform's C compiler into a static binary. However there
  369. are other commands to compile to C++, Objective-C or JavaScript. More details
  370. can be read in the `Nim Backend Integration document <backends.html>`_.
  371. Nim documentation tools
  372. =======================
  373. Nim provides the `doc`:idx: and `doc2`:idx: commands to generate HTML
  374. documentation from ``.nim`` source files. Only exported symbols will appear in
  375. the output. For more details `see the docgen documentation <docgen.html>`_.
  376. Nim idetools integration
  377. ========================
  378. Nim provides language integration with external IDEs through the
  379. idetools command. See the documentation of `idetools <idetools.html>`_
  380. for further information.
  381. ..
  382. Nim interactive mode
  383. ====================
  384. The Nim compiler supports an interactive mode. This is also known as
  385. a `REPL`:idx: (*read eval print loop*). If Nim has been built with the
  386. ``-d:nimUseLinenoise`` switch, it uses the GNU readline library for terminal
  387. input management. To start Nim in interactive mode use the command
  388. ``nim secret``. To quit use the ``quit()`` command. To determine whether an input
  389. line is an incomplete statement to be continued these rules are used:
  390. 1. The line ends with ``[-+*/\\<>!\?\|%&$@~,;:=#^]\s*$`` (operator symbol followed by optional whitespace).
  391. 2. The line starts with a space (indentation).
  392. 3. The line is within a triple quoted string literal. However, the detection
  393. does not work if the line contains more than one ``"""``.
  394. Nim for embedded systems
  395. ========================
  396. While the default Nim configuration is targeted for optimal performance on
  397. modern PC hardware and operating systems with ample memory, it is very well
  398. possible to run Nim code and a good part of the Nim standard libraries on small
  399. embedded microprocessors with only a few kilobytes of memory.
  400. A good start is to use the ``any`` operating target together with the
  401. ``malloc`` memory allocator and the ``arc`` garbage collector. For example:
  402. ``nim c --os:any --gc:arc -d:useMalloc [...] x.nim``
  403. - ``--gc:arc`` will enable the reference counting memory management instead
  404. of the default garbage collector. This enables Nim to use heap memory which
  405. is required for strings and seqs, for example.
  406. - The ``--os:any`` target makes sure Nim does not depend on any specific
  407. operating system primitives. Your platform should support only some basic
  408. ANSI C library ``stdlib`` and ``stdio`` functions which should be available
  409. on almost any platform.
  410. - The ``-d:useMalloc`` option configures Nim to use only the standard C memory
  411. manage primitives ``malloc()``, ``free()``, ``realloc()``.
  412. If your platform does not provide these functions it should be trivial to
  413. provide an implementation for them and link these to your program.
  414. For targets with very restricted memory, it might be beneficial to pass some
  415. additional flags to both the Nim compiler and the C compiler and/or linker
  416. to optimize the build for size. For example, the following flags can be used
  417. when targeting a gcc compiler:
  418. ``--opt:size --passC:-flto --passL:-flto``
  419. The ``--opt:size`` flag instructs Nim to optimize code generation for small
  420. size (with the help of the C compiler), the ``flto`` flags enable link-time
  421. optimization in the compiler and linker.
  422. Check the `Cross compilation` section for instructions how to compile the
  423. program for your target.
  424. Nim for realtime systems
  425. ========================
  426. See the documentation of Nim's soft realtime `GC <gc.html>`_ for further
  427. information.
  428. Signal handling in Nim
  429. ======================
  430. The Nim programming language has no concept of Posix's signal handling
  431. mechanisms. However, the standard library offers some rudimentary support
  432. for signal handling, in particular, segmentation faults are turned into
  433. fatal errors that produce a stack trace. This can be disabled with the
  434. ``-d:noSignalHandler`` switch.
  435. Optimizing for Nim
  436. ==================
  437. Nim has no separate optimizer, but the C code that is produced is very
  438. efficient. Most C compilers have excellent optimizers, so usually it is
  439. not needed to optimize one's code. Nim has been designed to encourage
  440. efficient code: The most readable code in Nim is often the most efficient
  441. too.
  442. However, sometimes one has to optimize. Do it in the following order:
  443. 1. switch off the embedded debugger (it is **slow**!)
  444. 2. turn on the optimizer and turn off runtime checks
  445. 3. profile your code to find where the bottlenecks are
  446. 4. try to find a better algorithm
  447. 5. do low-level optimizations
  448. This section can only help you with the last item.
  449. Optimizing string handling
  450. --------------------------
  451. String assignments are sometimes expensive in Nim: They are required to
  452. copy the whole string. However, the compiler is often smart enough to not copy
  453. strings. Due to the argument passing semantics, strings are never copied when
  454. passed to subroutines. The compiler does not copy strings that are a result from
  455. a procedure call, because the callee returns a new string anyway.
  456. Thus it is efficient to do:
  457. .. code-block:: Nim
  458. var s = procA() # assignment will not copy the string; procA allocates a new
  459. # string already
  460. However it is not efficient to do:
  461. .. code-block:: Nim
  462. var s = varA # assignment has to copy the whole string into a new buffer!
  463. For ``let`` symbols a copy is not always necessary:
  464. .. code-block:: Nim
  465. let s = varA # may only copy a pointer if it safe to do so
  466. If you know what you're doing, you can also mark single string (or sequence)
  467. objects as `shallow`:idx:\:
  468. .. code-block:: Nim
  469. var s = "abc"
  470. shallow(s) # mark 's' as shallow string
  471. var x = s # now might not copy the string!
  472. Usage of ``shallow`` is always safe once you know the string won't be modified
  473. anymore, similar to Ruby's `freeze`:idx:.
  474. The compiler optimizes string case statements: A hashing scheme is used for them
  475. if several different string constants are used. So code like this is reasonably
  476. efficient:
  477. .. code-block:: Nim
  478. case normalize(k.key)
  479. of "name": c.name = v
  480. of "displayname": c.displayName = v
  481. of "version": c.version = v
  482. of "os": c.oses = split(v, {';'})
  483. of "cpu": c.cpus = split(v, {';'})
  484. of "authors": c.authors = split(v, {';'})
  485. of "description": c.description = v
  486. of "app":
  487. case normalize(v)
  488. of "console": c.app = appConsole
  489. of "gui": c.app = appGUI
  490. else: quit(errorStr(p, "expected: console or gui"))
  491. of "license": c.license = UnixToNativePath(k.value)
  492. else: quit(errorStr(p, "unknown variable: " & k.key))