> > Types like seq and string do it...
> 
> Actually they don't....

Yea, seems like so, made similar benchmark for `seq`, (for `table` results are 
similar). Couple orders of magnitude faster with `ref` in `proc get_prices*(): 
ref seq[float] =`.
    
    
    const N = 100_000
    
    proc load_prices_slow*(): seq[float] =
      for i in 1..N: result.add 1.0
    
    var cached_stock_prices: ref seq[float] = nil
    proc get_prices*(): seq[float] =
      if cached_stock_prices.is_nil:
        cached_stock_prices.new
        cached_stock_prices[] = load_prices_slow()
        echo "loaded"
      cached_stock_prices[]
    
    # Usage - the `get_prices` functino called many times in different places.
    echo "starting calculations"
    for _ in 1..1000:
      for i in 1..100:
        discard get_prices()[i]
    
    echo "done"
    
    
    Run

Reply via email to