I still wonder what the disadvantages of static proc parameters are.

Is it increase in compile time, or are there other issues?

Because, I recently suggested in

[https://github.com/nim-lang/Nim/issues/10910#issuecomment-490977890](https://github.com/nim-lang/Nim/issues/10910#issuecomment-490977890)
    
    
    proc `^`*[T](x: T, y: static[Natural]): T {.inline.} =
      when y < 7:
        when y == 0:
          result = T(1)
        when y == 1:
          result = x
        when y == 2:
          result = x * x
        when y == 3:
          result = x * x * x
        when y == 4:
          result = x * x
          result *= result
        when y == 5:
          result = x * x
          result *= (result * x)
        when y == 6:
          result = x * x
          result *= (result * result)
      else:
        result = math.`^`(x, y)
    
    # or
    
    proc `^`*[T](x: T, y: static[Natural]): T {.inline.} =
      when y < 10:
        result = T(1)
        var i = y
        while i > 0:
          result *= x
          dec(i)
      else:
        result = math.`^`(x, y)
    
    
    Run

to make small integer powers more nimish.

Miran' s choise was instead adding this code for small powers:
    
    
    case y
      of 0: result = 1
      of 1: result = x
      of 2: result = x * x
    of 3: result = x * x * x
    
    
    Run

in 
[https://github.com/nim-lang/Nim/blob/devel/lib/pure/math.nim#L966](https://github.com/nim-lang/Nim/blob/devel/lib/pure/math.nim#L966)

Which seems to be an improvement, but I can not imagine that it leads to 
optimal code. (Maybe the case statement is covered by a cmov instruction 
avoiding a slow branch, but still the ^ proc is not inlined, as long as we do 
not compile with -flto.)

Reply via email to