Skip to content

Commit

Permalink
Use PMR allocator with a memory pool for PolyTextOut data since we're…
Browse files Browse the repository at this point in the history
… just going to use it again and again.
  • Loading branch information
miniksa committed Dec 19, 2020
1 parent fc7b052 commit c174ba6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/inc/LibraryIncludes.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <deque>
#include <list>
#include <memory>
#include <memory_resource>
#include <map>
#include <mutex>
#include <shared_mutex>
Expand Down
7 changes: 7 additions & 0 deletions src/renderer/gdi/gdirenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ namespace Microsoft::Console::Render
COLORREF _lastBg;
bool _lastFontItalic;

// Memory pooling to save alloc/free work to the OS for things
// frequently created and dropped.
// It's important the pool is first so it can be given to the others on construction.
std::pmr::unsynchronized_pool_resource _pool;
std::pmr::vector<std::pmr::wstring> _polyStrings;
std::pmr::vector<std::pmr::basic_string<int>> _polyWidths;

[[nodiscard]] HRESULT _InvalidCombine(const RECT* const prc) noexcept;
[[nodiscard]] HRESULT _InvalidOffset(const POINT* const ppt) noexcept;
[[nodiscard]] HRESULT _InvalidRestrict() noexcept;
Expand Down
45 changes: 17 additions & 28 deletions src/renderer/gdi/paint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ using namespace Microsoft::Console::Render;
{
try
{

const auto cchLine = clusters.size();

// Exit early if there are no lines to draw.
Expand All @@ -308,14 +309,12 @@ using namespace Microsoft::Console::Render;

const auto pPolyTextLine = &_pPolyText[_cPolyText];

auto pwsPoly = std::make_unique<wchar_t[]>(cchLine);
RETURN_IF_NULL_ALLOC(pwsPoly);
auto& polyString = _polyStrings.emplace_back(cchLine, UNICODE_NULL);

COORD const coordFontSize = _GetFontSize();

auto rgdxPoly = std::make_unique<int[]>(cchLine);
RETURN_IF_NULL_ALLOC(rgdxPoly);

auto& polyWidth = _polyWidths.emplace_back(cchLine, 0);

// Sum up the total widths the entire line/run is expected to take while
// copying the pixel widths into a structure to direct GDI how many pixels to use per character.
size_t cchCharWidths = 0;
Expand All @@ -327,9 +326,9 @@ using namespace Microsoft::Console::Render;

// Our GDI renderer hasn't and isn't going to handle things above U+FFFF or sequences.
// So replace anything complicated with a replacement character for drawing purposes.
pwsPoly[i] = cluster.GetTextAsSingle();
rgdxPoly[i] = gsl::narrow<int>(cluster.GetColumns()) * coordFontSize.X;
cchCharWidths += rgdxPoly[i];
polyString[i] = cluster.GetTextAsSingle();
polyWidth[i] = gsl::narrow<int>(cluster.GetColumns()) * coordFontSize.X;
cchCharWidths += polyWidth[i];
}

// Detect and convert for raster font...
Expand All @@ -338,15 +337,15 @@ using namespace Microsoft::Console::Render;
// dispatch conversion into our codepage

// Find out the bytes required
int const cbRequired = WideCharToMultiByte(_fontCodepage, 0, pwsPoly.get(), (int)cchLine, nullptr, 0, nullptr, nullptr);
int const cbRequired = WideCharToMultiByte(_fontCodepage, 0, polyString.data(), (int)cchLine, nullptr, 0, nullptr, nullptr);

if (cbRequired != 0)
{
// Allocate buffer for MultiByte
auto psConverted = std::make_unique<char[]>(cbRequired);

// Attempt conversion to current codepage
int const cbConverted = WideCharToMultiByte(_fontCodepage, 0, pwsPoly.get(), (int)cchLine, psConverted.get(), cbRequired, nullptr, nullptr);
int const cbConverted = WideCharToMultiByte(_fontCodepage, 0, polyString.data(), (int)cchLine, psConverted.get(), cbRequired, nullptr, nullptr);

// If successful...
if (cbConverted != 0)
Expand All @@ -356,22 +355,22 @@ using namespace Microsoft::Console::Render;

if (cchRequired != 0)
{
auto pwsConvert = std::make_unique<wchar_t[]>(cchRequired);
std::pmr::wstring polyConvert(cchRequired, UNICODE_NULL, &_pool);

// Then do the actual conversion.
int const cchConverted = MultiByteToWideChar(CP_ACP, 0, psConverted.get(), cbRequired, pwsConvert.get(), cchRequired);
int const cchConverted = MultiByteToWideChar(CP_ACP, 0, psConverted.get(), cbRequired, polyConvert.data(), cchRequired);

if (cchConverted != 0)
{
// If all successful, use this instead.
pwsPoly.swap(pwsConvert);
polyString.swap(polyConvert);
}
}
}
}
}

pPolyTextLine->lpstr = pwsPoly.release();
pPolyTextLine->lpstr = polyString.data();
pPolyTextLine->n = gsl::narrow<UINT>(clusters.size());
pPolyTextLine->x = ptDraw.x;
pPolyTextLine->y = ptDraw.y;
Expand All @@ -380,7 +379,7 @@ using namespace Microsoft::Console::Render;
pPolyTextLine->rcl.top = pPolyTextLine->y;
pPolyTextLine->rcl.right = pPolyTextLine->rcl.left + ((SHORT)cchCharWidths * coordFontSize.X);
pPolyTextLine->rcl.bottom = pPolyTextLine->rcl.top + coordFontSize.Y;
pPolyTextLine->pdx = rgdxPoly.release();
pPolyTextLine->pdx = polyWidth.data();

if (trimLeft)
{
Expand Down Expand Up @@ -417,20 +416,10 @@ using namespace Microsoft::Console::Render;
hr = E_FAIL;
}

for (size_t iPoly = 0; iPoly < _cPolyText; iPoly++)
{
if (nullptr != _pPolyText[iPoly].lpstr)
{
delete[] _pPolyText[iPoly].lpstr;
_pPolyText[iPoly].lpstr = nullptr;
}
_polyStrings.clear();
_polyWidths.clear();

if (nullptr != _pPolyText[iPoly].pdx)
{
delete[] _pPolyText[iPoly].pdx;
_pPolyText[iPoly].pdx = nullptr;
}
}
ZeroMemory(_pPolyText, sizeof(_pPolyText));

_cPolyText = 0;
}
Expand Down
5 changes: 4 additions & 1 deletion src/renderer/gdi/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ GdiEngine::GdiEngine() :
_lastFontItalic(false),
_fPaintStarted(false),
_hfont(nullptr),
_hfontItalic(nullptr)
_hfontItalic(nullptr),
_pool{}, // It's important the pool is first so it can be given to the others on construction.
_polyStrings{&_pool},
_polyWidths{&_pool}
{
ZeroMemory(_pPolyText, sizeof(POLYTEXTW) * s_cPolyTextCache);
_rcInvalid = { 0 };
Expand Down

1 comment on commit c174ba6

@github-actions
Copy link

Choose a reason for hiding this comment

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

New misspellings found, please review:

  • pmr
To accept these changes, run the following commands
perl -e '
my @expect_files=qw('".github/actions/spell-check/expect/5757ec679b03a4240130c3c53766c91bbc5cd6a7.txt
.github/actions/spell-check/expect/655f007265b351e140d20b3976792523ad689241.txt
.github/actions/spell-check/expect/alphabet.txt
.github/actions/spell-check/expect/expect.txt
.github/actions/spell-check/expect/web.txt"');
@ARGV=@expect_files;
my @stale=qw('"AAAAA Bopomofo Checkboxes CParams cso CSV DOWNSCALE GENERATEPROJECTPRIFILE hhhh Horiz Inlines INPATHROOT intersectors Kode MAKEINTRESOURCEA mousemode msixbundle psin Reserialize reso RETROII rgdx rgus sbri scanbri scol SGRXY spe sph spherefunctions tcon UDK UDKs Unfocus Viginetting wz xe xlang XWV xyw yzx "');
my $re=join "|", @stale;
my $suffix=".".time();
my $previous="";
sub maybe_unlink { unlink($_[0]) if $_[0]; }
while (<>) {
  if ($ARGV ne $old_argv) { maybe_unlink($previous); $previous="$ARGV$suffix"; rename($ARGV, $previous); open(ARGV_OUT, ">$ARGV"); select(ARGV_OUT); $old_argv = $ARGV; }
  next if /^($re)(?:$| .*)/; print;
}; maybe_unlink($previous);'
perl -e '
my $new_expect_file=".github/actions/spell-check/expect/c174ba6a5690acc397eca572eb046c6aa6dec3da.txt";
open FILE, q{<}, $new_expect_file; chomp(my @words = <FILE>); close FILE;
my @add=qw('"checkboxes csv horiz inlines pmr reserialize udk unfocus "');
my %items; @items{@words} = @words x (1); @items{@add} = @add x (1);
@words = sort {lc($a) cmp lc($b)} keys %items;
open FILE, q{>}, $new_expect_file; for my $word (@words) { print FILE "$word\n" if $word =~ /\w/; };
close FILE;'
git add .github/actions/spell-check/expect || echo '... you want to ensure .github/actions/spell-check/expect/c174ba6a5690acc397eca572eb046c6aa6dec3da.txt is added to your repository...'
✏️ Contributor please read this

By default the command suggestion will generate a file named based on your commit. That's generally ok as long as you add the file to your commit. Someone can reorganize it later.

⚠️ The command is written for posix shells. You can copy the contents of each perl command excluding the outer ' marks and dropping any '"/"' quotation mark pairs into a file and then run perl file.pl from the root of the repository to run the code. Alternatively, you can manually insert the items...

If the listed items are:

  • ... misspelled, then please correct them instead of using the command.
  • ... names, please add them to .github/actions/spell-check/dictionary/names.txt.
  • ... APIs, you can add them to a file in .github/actions/spell-check/dictionary/.
  • ... just things you're using, please add them to an appropriate file in .github/actions/spell-check/expect/.
  • ... tokens you only need in one place and shouldn't generally be used, you can add an item in an appropriate file in .github/actions/spell-check/patterns/.

See the README.md in each directory for more information.

🔬 You can test your commits without appending to a PR by creating a new branch with that extra change and pushing it to your fork. The :check-spelling action will run in response to your push -- it doesn't require an open pull request. By using such a branch, you can limit the number of typos your peers see you make. 😉

⚠️ Reviewers

At present, the action that triggered this message will not show its ❌ in this PR unless the branch is within this repository.
Thus, you should make sure that this comment has been addressed before encouraging the merge bot to merge this PR.

Please sign in to comment.