trueagi-io / metta-examples

Discussion of MeTTa programming with examples
MIT License
17 stars 16 forks source link

Slow list creation #42

Open mvpeterson opened 6 months ago

mvpeterson commented 6 months ago

It takes about 1 minute to create a list of 24 elements in minimal Metta. Is there a way to make it work faster?


(= (makelist $x)
(if (== () $x) Nil (let $_cdr (cdr-atom $x)
                                (Cons (car-atom $x) (makelist $_cdr)))
                   )
)

!(makelist (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24))
[(Cons 1 (Cons 2 (Cons 3 (Cons 4 (Cons 5 (Cons 6 (Cons 7 (Cons 8 (Cons 9 (Cons 10 (Cons 11 (Cons 12 (Cons 13 (Cons 14 (Cons 15 (Cons 16 (Cons 17 (Cons 18 (Cons 19 (Cons 20 (Cons 21 (Cons 22 (Cons 23 (Cons 24 Nil))))))))))))))))))))))))]
noskill commented 6 months ago

adding types doesn't change the performance

(: List (-> $t Type))
(: Nil (List $t))
(: Cons (-> $t (List $t) (List $t)))

; convert (a b c) to (Cons a (Cons b (Cons c Nil)))
(: makelist (-> Atom (List $t)))
vsbogd commented 6 months ago

Interesting that Python is six times slower than Rust REPL on this task which is strange.

ngeiswei commented 6 months ago

This issue somewhat relates to one I've just created as well https://github.com/trueagi-io/hyperon-experimental/issues/638

vsbogd commented 6 months ago

From the code logic perspective nothing is wrong here. Although I recommend adding types as @noskill suggested in https://github.com/trueagi-io/metta-examples/issues/42#issuecomment-2014866684.

It is a performance issue. So I would suggest closing it as a duplicate of https://github.com/trueagi-io/hyperon-experimental/issues/638

noskill commented 6 months ago

@vsbogd is the issue caused by argument of car-atom being evaluated? If so why it gets evaluated? I remember you told me that built-in metta types are not evaluated

vsbogd commented 6 months ago

@vsbogd is the issue caused by argument of car-atom being evaluated?

It is not evaluated in this context. If you see it is evaluated in log the reason is == has type (-> $t $t Bool).

noskill commented 6 months ago

What do you think about adding differently typed == operator?

noskill commented 6 months ago

works faster, but not much

(: same (-> Atom Atom Bool))

(= (same $X $Y) False)
(= (same $X $X) True)

(= (makelist $x)
    (if (same () $x) Nil (let $_cdr (cdr-atom $x)
                                (Cons (car-atom $x) (makelist $_cdr)))
                   )
)