opencog / atomspace

The OpenCog (hyper-)graph database and graph rewriting system
https://wiki.opencog.org/w/AtomSpace
Other
798 stars 225 forks source link

Provide a ContinuationLink for inf recursion. #2895

Closed linas closed 2 years ago

linas commented 2 years ago

In order to make recursive pattern matches run faster, its best to be tail-recursive. This avoids two costly steps: (1) initializing a new temporary scratch atomspace and (2) buying a new C++ stack frame.

The implementation is pretty small, although a bit tricky to understand: whenever the ContinuationLink is encountered, two c++ exceptions are thrown that return us back to the beginning, ready to do it again. No memleaks, no stack frames, just start it up again.

This is noticeably faster than before, like 10x or 100x faster (I didn't measure); its now less than an eyeblink instead of a heartbeat pause.