Skip to content

Trivial patch: Break a cycle - #226

Merged
asvetlov merged 1 commit into
aio-libs:masterfrom
Martiusweb:break-cycle
Jan 10, 2015
Merged

asvetlov merged 1 commit into
aio-libs:masterfrom
Martiusweb:break-cycle

Conversation

@Martiusweb

Copy link
Copy Markdown
Contributor

Once the connection is closed, remove the references from the protocol to the
reader and writer objects. It will effectively break a references cycle and
allow a large group of objects to be freed thanks to the reference counting
mechanism, instead of be garbage collected.

This obviously a really trivial patch, but in a large application emitting
a large amount of requests, it reduces the working time of the gc, and thus,
the latency.

@popravich

Copy link
Copy Markdown
Member

Its trivial, but it breaks tests

@Martiusweb

Copy link
Copy Markdown
Contributor Author

My bad, I should have been more careful.

I pushed a second commit which updates the tests. I checked the travis build report : the failure seems unrelated to my patch. I tested on my computer with a fresh build of python 3.3.6, the tests passed.

@fafhrd91

fafhrd91 commented Jan 9, 2015

Copy link
Copy Markdown
Member

I am willing to breake test, but I need to see cycle references. Could you
provide more information what creates reference cycle

On Friday, January 9, 2015, Martin Richard [email protected] wrote:

My bad, I should have been more careful.

I pushed a second commit which updates the tests. I checked the travis
build report : the failure seems unrelated to my patch. I tested on my
computer with a fresh build of python 3.3.6, the tests passed.

—
Reply to this email directly or view it on GitHub
#226 (comment).

Once the connection is closed, remove the references from the protocol to the
reader and writer objects. It will effectively break a references cycle and
allow a large group of objects to be freed thanks to the reference counting
mechanism, instead of be garbage collected.

This obviously a really trivial patch, but in a large application emitting
a large amount of requests, it reduces the working time of the gc, and thus,
the latency.
@Martiusweb

Copy link
Copy Markdown
Contributor Author

I've got

  • aiohttp.parsers.StreamProtocol.writer referring to a StreamWriter object
  • asyncio.StreamWriter._protocol referring to the protocol

so protocol => writer => protocol.

Actually, the reader does not create a cycle, I made another patch, squashed the 3 commits, and now, the commit is trivial again - sorry for the noise.

Thanks to objgraph this can be observed:
ref_1

The source to generate the graph is here https://gist.github.com/Martiusweb/82fa852c1ceebe730307
I am going to submit a set of patches to tulip which would allow to disable the garbage collector on a case like this one.

@fafhrd91

fafhrd91 commented Jan 9, 2015

Copy link
Copy Markdown
Member

When did you take this graph, before or after connection_close get called?

On Friday, January 9, 2015, Martin Richard [email protected] wrote:

I've got

  • aiohttp.parsers.StreamProtocol.writer referring to a StreamWriter
    object
  • asyncio.StreamWriter._protocol referring to the protocol

so protocol => writer => protocol.

Actually, the reader does not create a cycle, I made another patch,
squashed the 3 commits, and now, the commit is trivial again - sorry for
the noise.

Thanks to objgraph this can be observed:
[image: ref_1]
https://cloud.githubusercontent.com/assets/195352/5682457/6b3faa3e-9820-11e4-9d44-4048a93c03a3.png

(source to generate the graph:
https://gist.github.com/Martiusweb/82fa852c1ceebe730307)

—
Reply to this email directly or view it on GitHub
#226 (comment).

@fafhrd91

fafhrd91 commented Jan 9, 2015

Copy link
Copy Markdown
Member

Sort, connection_lost

@Martiusweb

Copy link
Copy Markdown
Contributor Author

While it's not explicit, it's after connection_lost() since I read the whole response and (afaik) the default request mode is "Connection: close". When my patch is applied, the cycle is broken, and the only things which are garbage collected are caused by the way asyncio implements getaddrinfo().

@asvetlov

Copy link
Copy Markdown
Member

LGTM

asvetlov added a commit that referenced this pull request Jan 10, 2015
@asvetlov
asvetlov merged commit df3153a into aio-libs:master Jan 10, 2015
@ludovic-gasc

Copy link
Copy Markdown
Contributor

@Martiusweb Thank you for your graph script, vey interesting.

@fafhrd91

Copy link
Copy Markdown
Member

I'd prefer to fix cycle in first place. Writer should release protocol
reference after connection_lost

On Saturday, January 10, 2015, Ludovic Gasc [email protected]
wrote:

@Martiusweb https://github.com/Martiusweb Thank you for your graph
script, vey interesting.

—
Reply to this email directly or view it on GitHub
#226 (comment).

@asvetlov

Copy link
Copy Markdown
Member

I've filed https://code.google.com/p/tulip/issues/detail?id=217 and will make a patch after preliminary agreement.

@Martiusweb

Copy link
Copy Markdown
Contributor Author

@asvetlov I think you should open it on bugs.python.org, there is a dedicated component to asyncio and gvanrossum, haypo and al answer usually pretty fast.

@asvetlov

Copy link
Copy Markdown
Member

I think doesn't matter.

@lock

lock Bot commented Oct 30, 2019

Copy link
Copy Markdown

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock Bot added the outdated label Oct 30, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Oct 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants