Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable tail call optimization in couch_replicator_docs #505

Closed
wants to merge 1 commit into from

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
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.

None yet

2 participants