Skip to content

Experiment with compressing individual method contexts - #59000

Closed
jakobbotsch wants to merge 2 commits into
dotnet:mainfrom
jakobbotsch:compress-spmi-collections
Closed

jakobbotsch wants to merge 2 commits into
dotnet:mainfrom
jakobbotsch:compress-spmi-collections

Conversation

@jakobbotsch

@jakobbotsch jakobbotsch commented Sep 12, 2021 •

Copy link
Copy Markdown
Member

This is an experiment to support Brotli compression in method contexts for SPMI collections.
I picked Brotli because we already have the source in the tree so I believe there are no license considerations.
The main benefit is to reduce the size of collections stored on the disk. I had a hypothesis that for HDDs,
this would also help replay speed because the decompression is faster than reading the full thing. My experiments
seem to indicate that this is true for parallel runs, but I don't think it's for the reason that I expected.

Currently the way Brotli is added is pretty unfortunate as I put it in superpmi-shared and then build it in all the superpmi projects that needs MethodContext. It would probably be better to build it once and statically link it, but I don't have the necessary cmake knowledge to do that for this experiment. In total, we are building it separately 5 times (6 times if you include System.IO.Compression).

I measured some of the characteristics of compression vs no compression. For all the experiments I cleared my file cache between each run using RAMMap (Empty -> Empty Standby List).

Collect time Ratio .mch size Ratio .mch.zip size Ratio Par replay, SSD Ratio Seq replay, SSD Ratio Par replay, HDD Ratio Seq replay, HDD Ratio
No compression 320.3s 1.00 3026.9 MiB 1.00 333.5 MiB 1.00 10.6s 1.00 155.4s 1.00 169.6s 1.00 155.2s 1.00
Brotli with quality 5 606.4s 1.89 500.1 MiB 0.17 495.5 MiB 1.49 10.6s 1.00 161.4s 1.04 10.6s 0.06 162.3s 1.05

The collect command was:

py .\src\coreclr\scripts\superpmi.py collect --pmi -assemblies C:\dev\dotnet\runtime\artifacts\tests\coreclr\windows.x64.Checked\Tests\Core_Root\ -output_mch_path <out_path>

Note that the time for collect includes the time for the collection itself, merging/cleaning, building the TOC and doing a test SPMI replay. Also the merging/cleaning steps are quite inefficient because they repeatedly decompress and recompress each method context even when it could just be copied directly from the input to the output. I have not bothered optimizing this but it would probably reduce the time taken for collections significantly.

For the replay commands I invoked superpmi.exe directly with a checked JIT. For parallel runs I passed -p. I think the parallel HDD runs are so much faster because the smaller file means the OS keeps it in memory longer. It seems the striding we use with parallel runs is terrible for HDD performance.

The PR allows method contexts to individually select compression/no compression so even if we don't want it for the smaller collections, we could potentially use it in case we want to have much larger collections in the future. I also made sure that old collections still load correctly so that transitioning would be smooth.

Are there any comments on whether we want this and what we want for the default (compression or no compression)? And any tips for how to handle the Brotli source files in CMake?

cc @dotnet/jit-contrib

@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Sep 12, 2021
@ghost

ghost commented Sep 12, 2021

Copy link
Copy Markdown

Tagging subscribers to this area: @JulieLeeMSFT
See info in area-owners.md if you want to be subscribed.

Issue Details

This is an experiment to support Brotli compression in method contexts for SPMI collections.
I picked Brotli because we already have the source in the tree so I believe there are no license considerations.
The main benefit is to reduce the size of collections stored on the disk. I had a hypothesis that for HDDs,
this would also help replay speed because the decompression is faster than reading the full thing. My experiments
seem to indicate that this is true for parallel runs, but I don't think it's for the reason that I expected.

Currently the way Brotli is added is pretty unfortunate as I put it in superpmi-shared and then build it in all the superpmi projects that needs MethodContext. It would probably be better to build it once and statically link it, but I don't have the necessary cmake knowledge to do that for this experiment. In total, we are building it separately 5 times (6 times if you include System.IO.Compression).

I measured some of the characteristics of compression vs no compression. For all the experiments I cleared my file cache between each run using RAMMap (Empty -> Empty Standby List).

Collect time Ratio .mch size Ratio .mch.zip size Ratio Par replay, SSD Ratio Seq replay, SSD Ratio Par replay, HDD Ratio Seq replay, HDD Ratio
No compression 320.3s 1.00 3026.9 MiB 1.00 333.5 MiB 1.00 10.6s 1.00 155.4s 1.00 169.6s 1.00 155.2s 1.00
Brotli with quality 5 606.4s 1.89 500.1 MiB 0.17 495.5 MiB 1.49 10.6s 1.00 161.4s 1.04 10.6s 0.06 162.3s 1.05

The collect command was:

py .\src\coreclr\scripts\superpmi.py collect --pmi -assemblies C:\dev\dotnet\runtime\artifacts\tests\coreclr\windows.x64.Checked\Tests\Core_Root\ -output_mch_path <out_path>

Note that the time for collect includes the time for the collection itself, merging/cleaning, building the TOC and doing a test SPMI replay. Also the merging/cleaning steps are quite inefficient because they repeatedly decompress and recompress each method context even when it could just be copied directly from the input to the output. I have not bothered optimizing this but it would probably reduce the time taken for collections significantly.

For the replay commands I invoked superpmi.exe directly with a checked JIT. For parallel runs I passed -p. I think the parallel HDD runs are so much faster because the smaller file means the OS keeps it in memory longer. It seems the striding we use with parallel runs is terrible for HDD performance.

The PR allows method contexts to individually select compression/no compression so even if we don't want it for the smaller collections, we could potentially use it in case we want to have much larger collections in the future.

Are there any comments on whether we want this and what we want for the default (compression or no compression)? And any tips for how to handle the Brotli source files in CMake?

cc @dotnet/jit-contrib

Author: jakobbotsch
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

@jakobbotsch
jakobbotsch marked this pull request as draft September 12, 2021 13:46
@jkoritzinsky

Copy link
Copy Markdown
Member

Instead of adding Brotli into the source tree twice, you should move the brotli implementation from src/libraries/Native/AnyOS/brotli to src/native/brotli and update both the superpmi and libraries CMake scripts to pull it from there.

@BruceForstall

Copy link
Copy Markdown
Contributor

Presumably you could create a static lib out of the brotli files and link them. The JIT does this with the gcinfo lib in src\coreclr\gcinfo, so that might provide a template.

@BruceForstall BruceForstall left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This generally looks great.

Some questions about the user experience:

  1. Should there be options to select the compression mode? The shims don't take command-line arguments, so there would need to be a SUPERPMI_* environment variable to transmit this information.
  2. There should probably be an mcs command to (a) compress a file (using some chosen compression method/level), (b) decompress a file, (c) output the compression statistics.
  3. Do all the commands like "mcs -merge -dedup -thin", "mcs -integ", etc., "just work"?
  4. Does the TOC ("mcs -toc", use with "superpmi -c NNN") "just work"?


Assert(buffIndex == totalFileSize);
return data;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this do a full copy of the array? If so, that could be expensive.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No, even if RVO is not used I believe the copy should be elided and a move operation will happen instead.


WriteFile(hFile, buff2, totalFileSize, &bytesWritten, NULL);
delete[] buff2;
static std::vector<unsigned char> applyCompression(const std::vector<unsigned char>&& data, MethodContextCompression compression)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be a MethodContext static (private) member? Same for the decompression function.

@ghost ghost closed this Nov 3, 2021
@ghost

ghost commented Nov 3, 2021

Copy link
Copy Markdown

Draft Pull Request was automatically closed for inactivity. Please let us know if you'd like to reopen it.

@jakobbotsch

Copy link
Copy Markdown
Member Author

Sorry, I have missed your questions above @BruceForstall.

This generally looks great.

Some questions about the user experience:

  1. Should there be options to select the compression mode? The shims don't take command-line arguments, so there would need to be a SUPERPMI_* environment variable to transmit this information.

My hope would be to get this to a place where the overhead of the compression is so negligible that you would never want to turn it off so that something like this would not be necessary (and we would always default to compression on).

  1. There should probably be an mcs command to (a) compress a file (using some chosen compression method/level), (b) decompress a file, (c) output the compression statistics.

Given the above, I was hoping something like this would not be useful. One of my thoughts was that we would support the compression end-to-end so that we never needed to have uncompressed collections/contexts anywhere, to ensure that we could support really big collections without problems.

  1. Do all the commands like "mcs -merge -dedup -thin", "mcs -integ", etc., "just work"?

They worked yes, since the decompression happens before the method contexts are returned. But I believe some of the operations could be optimized to be made faster.

  1. Does the TOC ("mcs -toc", use with "superpmi -c NNN") "just work"?

Yes, this part worked too. To be honest I'm not sure that we want a separate TOC file instead of keeping it in the file if we continue to evolve on it. I guess it makes the collection process itself use a little less disk space because there are no TOCs for the intermediate files, but it does complicate other things.

My current plans for my next work on this is to use the "chunked" approach we discussed, i.e. where we compress multiple contexts as part of one blob, to get better compression ratio. But this requires changes to how we do parallel SPMI replays/diffs and more, and having to do all of this in C++ (and using PAL) makes it that much harder. Not sure when I'll have time to work on this again, but I'm keeping it in the back of my mind.

@BruceForstall

Copy link
Copy Markdown
Contributor

One of the original design points of the "MCH" files is that they are simply concatenations of individual method contexts. Changing the file format to include a TOC, and compressing groups of method contexts as one, might break this, although perhaps it would be worth doing so, as well.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2021
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants