Skip to content

Enable tail call optimization in couch_replicator_docs#505

Closed
nickva wants to merge 1 commit intoapache:masterfrom
cloudant:couchdb-3324-enable-tco
Closed

Enable tail call optimization in couch_replicator_docs#505
nickva wants to merge 1 commit intoapache:masterfrom
cloudant:couchdb-3324-enable-tco

Conversation

@nickva
Copy link
Contributor

@nickva nickva commented May 3, 2017

update_docs/4 function was recursively called from catch part of a
try ... catch block. That construct prevented tail call optimization to
take place.

Jira: COUCHDB-3324

`update_docs/4` function was recursively called from `catch` part of a
try ... catch block. That construct prevented tail call optimization to
take place.

Jira: COUCHDB-3324
@nickva
Copy link
Contributor Author

nickva commented May 3, 2017

@davisp pointed out that current code already has TCO and this is not needed

Used this script from him (with a modification to just print the memory of the process) to verify:

-module(foo).
-compile(export_all).
a() ->
    a(0).
a(I) ->
    if (I+1) rem 1000 /= 0 -> ok; true ->
        io:format("~p~n", [process_info(self(), memory)])
    end,
    try
        throw(bar)
    catch throw:bar ->
        c(a, I+1)
    end.
b() ->
    b(0).
b(I) ->
    if (I+1) rem 1000 /= 0 -> ok; true ->
        io:format("~p~n", [process_info(self(), memory)])
    end,
    try
        c(b, I+1)
    catch throw:bar ->
        foo
    end.
c(F, I) ->
    ?MODULE:F(I).

foo:b() ends up producing

...
{memory,102684144}
{memory,102684144}
{memory,102684144}
{memory,102684144}

foo:a() is stable

...
{memory,16672}
{memory,24576}
{memory,11784}
{memory,16672}

@iilyak
Copy link
Contributor

iilyak commented May 3, 2017

Thank you Paul and Nick for pointing out that I was wrong. The code would work as is. @nickva could you close the PR?

@nickva
Copy link
Contributor Author

nickva commented May 4, 2017

@iilyak ah no worries, I assumed it needed TCO as well, so I think it was a useful exercise to understand how it works.

@nickva nickva closed this May 4, 2017
@nickva nickva deleted the couchdb-3324-enable-tco branch May 4, 2017 00:03
nickva pushed a commit to nickva/couchdb that referenced this pull request Sep 7, 2022
Co-authored-by: Joan Touzet <wohali@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants