Skip to content

weird creation of closures where they don't need to exist #1260

@johnynek

Description

@johnynek

We are compiling this code:

def length_String(s: String) -> Int:
  def loop(s, acc):
    recur s:
      case "": acc
      case "$.{_}${tail}": loop(tail, acc.add(1))

  loop(s, 0)

to:

ef ___bloop0(s, acc):
    ___t12 = True
    (___t7, ___t8) = (s, acc)
    ___t13 = ()
    while ___t12:
        ___t12 = False
        if ___t7 == "":
            ___t13 = ___t8
        else:
            ___t9 = 0
            ___t11 = False
            if ___t9 < ___t7.__len__():
                ___t9 = ___t9 + 1
                ___t10 = False
                ___a7 = ___t7[___t9:]
                ___t10 = True
                ___t11 = ___t10
            ___btail0 = ___a7
            ___t12 = True
            (___t7, ___t8) = (___btail0, ___t8 + 1)
            ___t13 = ()
    return ___t13
___t15 = (___bloop0,)
def ___t14(s):
    return ___t15[0](s, 0)
length_String = ___t14

There are two things wrong here:

  1. we shouldn't do def __t14(s) and then assign length_String = ___t14 we should just do def length_String(s)
  2. there is no need to make a closure on bloop0

I guess what is happening is that we are lifting the def loop above to be something like:
let length_String = (let loop = ... in s -> loop(s, 0))

which isn't a terrible compilation, but we should be able to lift loop all the way to the top level and not create a static closure which is less efficient. An issue is that TypedNormalization currently maps expression to expression, where here we really want to map an Expression onto 1 or more expressions (since we may lift some expressions to top level)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions