The following Nim program crashes and I don't understand why:
proc recursiveExplorer(s: openarray[char]) =
if s.len == 0: return
# processing(s[^1])
recursiveExplorer(s[ 0 ..< (s.len-1)])
var data = readFile("someLargeFile.txt")
var converted = randomData.items.toSeq
recursiveExplorer(data)
The crash occurs when using string or a seq[char] like converted.
I would like to recursively recurse through the string without having to copy the whole string, but in my tests, this approach is way slower than a for loop. Is there a way to cleanly pass string slices without copying the string (or passing indices)?
More generally, when using recursive functions and passing string as arguments, the string seems to be copied which makes recursion very slow. Is there a way around this ?
I am using Nim 2.0.0 on Windows.
using [..] creates a seq[char] from your openArray, so your function uses O(n^2) memory and is possibly being killed by the os.
instead of using .. use recursiveExplorer(toOpenArray(s,0,s.len-2)) and you wont produce a copy