Well it was not easy trying to get this working and after several segfaults and 
illegal indexes i (finally! RTFM!) looked into some more documentation but 
really couldn't find much about working with dynamic libraries created in Nim. 
What I did see is that more people had the same problem in this forum and there 
I found a 'magic' switch: use --gc:regions (mentioned by Araq) when you compile 
and this worked out great! I even can use the seq memory layout 'as is' : first 
int is the length, second the capacity and then the rest of the ints of my 
seq[int] follow. I. can easily use the FFi interface of the scripting language 
(newLisp in my case).

What was also nice is that my slightly enhanced function (thanks to a tip in 
mratsim's number_theory library) now is _almost_ as fast as the super duper bit 
vector version from mratsim (compiled with -d:release).

My version generates all primes upto 1_000_000_000 in about 6.8 seconds 
(cpuTime()) , super duper bit vector version in about 6.4 seconds on my MacBook 
Air.

For reference here is my final function, compiled with: nim c -d:release 
--gc:regions --app:lib sieve.nim
    
    
    func sieve2* (n:int):seq[int]{.exportc.} =
      result = newSeqOfCap[int](int(n.float/ln(n.float)*1.2))
      result.add(2)
      var arr = newSeq[bool](n+1)
      for x in countup(3, n, 2):
        if not(arr[x]):
          result.add(x)
          for y in countup(x*x, n, 2*x):
            arr[y] = true
    
    
    Run

Reply via email to