Skip to content

Python to CLR string marshaling LRU cache. #538

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

Closed
wants to merge 19 commits into from

Conversation

dmitriyse
Copy link
Contributor

What does this implement/fix? Explain your changes.

Adds string marshaling LRU cache (10k entries per marshaling type).
This greatly speedups mixed clr/py code. + Saves big amount of memory on large structures translation.

Does this close any currently open issues?

...

Any other comments?

This code is well tested in the high-load project. Memory usage was dramatically decreased after this improvement.

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Add yourself to AUTHORS
  • Updated the CHANGELOG

@mention-bot
Copy link

@dmitriyse, thanks! @vmuriart, @tonyroberts, @tiran, @cgohlke and @hsoft, please review this.

@codecov
Copy link

codecov bot commented Sep 7, 2017

Codecov Report

Merging #538 into master will decrease coverage by 0.01%.
The diff coverage is 79.33%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #538      +/-   ##
=========================================
- Coverage   77.12%   77.1%   -0.02%     
=========================================
  Files          65      71       +6     
  Lines        5547    5857     +310     
  Branches      889     933      +44     
=========================================
+ Hits         4278    4516     +238     
- Misses        981    1033      +52     
- Partials      288     308      +20
Flag Coverage Δ
#setup_linux 73.64% <73.8%> (-2.07%) ⬇️
#setup_windows 76.28% <75.48%> (-0.19%) ⬇️
Impacted Files Coverage Δ
src/runtime/nativecall.cs 100% <ø> (+2.22%) ⬆️
src/runtime/pythonengine.cs 80.35% <ø> (+1.66%) ⬆️
src/runtime/pyscope.cs 57.92% <100%> (-0.79%) ⬇️
src/runtime/assemblymanager.cs 89.83% <100%> (+0.05%) ⬆️
src/runtime/genericutil.cs 86.36% <100%> (+0.42%) ⬆️
src/runtime/metatype.cs 71.55% <100%> (ø) ⬆️
src/runtime/moduleobject.cs 85% <100%> (-0.06%) ⬇️
src/runtime/delegatemanager.cs 88.18% <100%> (-0.42%) ⬇️
src/runtime/typemanager.cs 84.16% <100%> (+0.13%) ⬆️
src/runtime/classderived.cs 88.41% <100%> (+0.07%) ⬆️
... and 25 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update af62a1a...f7bf1dd. Read the comment docs.

@codecov
Copy link

codecov bot commented Sep 7, 2017

Codecov Report

Merging #538 into master will decrease coverage by 0.43%.
The diff coverage is 64.77%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #538      +/-   ##
==========================================
- Coverage    76.9%   76.47%   -0.44%     
==========================================
  Files          63       69       +6     
  Lines        5798     6015     +217     
  Branches      950      991      +41     
==========================================
+ Hits         4459     4600     +141     
- Misses       1025     1081      +56     
- Partials      314      334      +20
Flag Coverage Δ
#setup_linux 68.55% <ø> (ø) ⬆️
#setup_windows 75.71% <64.77%> (-0.41%) ⬇️
Impacted Files Coverage Δ
src/runtime/assemblymanager.cs 87.03% <ø> (ø) ⬆️
src/runtime/pyobject.cs 39.01% <0%> (-1.33%) ⬇️
src/runtime/runtime.cs 86.95% <100%> (+0.23%) ⬆️
src/runtime/perf_utils/RawImmutableMemBlock.cs 48.14% <48.14%> (ø)
src/runtime/CustomMarshaler.cs 58.09% <48.93%> (+0.56%) ⬆️
src/runtime/perf_utils/RawMemoryFifoDictionary.cs 65.21% <65.21%> (ø)
...runtime/perf_utils/EncodedStringsFifoDictionary.cs 70.37% <70.37%> (ø)
src/runtime/perf_utils/FifoDictionary.cs 70.83% <70.83%> (ø)
...rc/runtime/perf_utils/EncodingGetStringPolyfill.cs 75% <75%> (ø)
src/runtime/perf_utils/RawMemUtils.cs 84.48% <84.48%> (ø)
... and 7 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5ee234a...49d0d89. Read the comment docs.

@vmuriart
Copy link
Contributor

Can you rebase this PR? looks like it was started out of a different PR that was already merged in.

@dmitriyse dmitriyse force-pushed the string-marshaling-cache branch from c02197d to a729ea4 Compare September 22, 2017 01:19
@dmitriyse
Copy link
Contributor Author

This PR depends on one non merged PR #534

Also I rebased all PR that depends on merged PRs #518 and #519

@den-run-ai
Copy link
Contributor

@jakrivan can you add your comments as inline according to the code changes in this PR:

https://github.com/pythonnet/pythonnet/pull/538/files

JanKrivanek added a commit to JanKrivanek/pythonnet that referenced this pull request Feb 12, 2018
@JanKrivanek
Copy link
Contributor

@denfromufa - inline comment added: #625

However it's just one from many things that would need to be adjusted to support multi-domain usages. So feel free to reject - not to spoil the code with irrelevant comment.

filmor pushed a commit that referenced this pull request Oct 16, 2018
filmor pushed a commit to filmor/pythonnet that referenced this pull request Oct 16, 2018
@filmor
Copy link
Member

filmor commented Oct 17, 2018

Note to self: This PR needs to be adjusted to the changes I made to PR #534, I renamed the TestsSuite class.

}
catch
{
// Do nothing with this. Very strange problem.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When does this happen?

Marshal.FreeHGlobal(mem);
throw;
stringBytesCount = PyEncoding.GetByteCount(s);
mem = Marshal.AllocHGlobal(stringBytesCount + 4);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why +4, wouldn't one additional byte be enough?

return Runtime.IsPython3
? Instance.MarshalManagedToNative(s)
: Marshal.StringToHGlobalAnsi(s);
if (Runtime.IsPython3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that your optimisation will only work at all for Python 3, is that necessary?

@filmor filmor added the next label Nov 15, 2018
@filmor filmor added this to the 2.4.0 milestone Nov 15, 2018
@filmor filmor mentioned this pull request Jan 18, 2019
@filmor filmor modified the milestones: 2.4.0, 2.4.1 Apr 12, 2019
@lostmsu
Copy link
Member

lostmsu commented Mar 3, 2020

As a performance change this needs to be accompanied by a performance test, that sets up a baseline.

E.g. first, a test must be merged, that sets the baseline performance goal without this change, then this PR should update the goal with the improved value.

@filmor
Copy link
Member

filmor commented Mar 4, 2020

This is also probably unmergable in its current form, I have just kept it open for reference.

@lostmsu lostmsu removed this from the 2.4.1 milestone Mar 4, 2020
@filmor filmor removed the next label Jun 9, 2020
@filmor filmor closed this Aug 9, 2021
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.

7 participants