0c93b2ebbe
## Summary of the Pull Request Caches vectors in the class and uses a new helper to opportunistically shrink/grow as viewport sizes change in order to save performance on alloc/free of commonly used vectors. ## PR Checklist * [x] Scratches a perf itch. * [x] I work here. * [x] wil tests added * [x] No add'l doc. * [x] Am core contributor. ## Detailed Description of the Pull Request / Additional comments Two fixes: 1. For outputting lots of text, the base renderer class spent a lot of time allocating and freeing and reallocating the `Cluster` vector that adapts the text buffer information into render clusters. I've now cached this vector in the base render class itself and I shrink/grow it based on the viewport update that happens at the top of every frame. To prevent too much thrashing in the downward/shrink direction, I wrote the `til::manage_vector` helper that contains a threshold to only shrink if it asks for small enough of a size relative to the existing one. I used 80% of the existing size as the threshold for this one. 2. For outputting lots of changing colors, the VT graphics output engine spent a bunch of time allocating and reallocating the vector for `GraphicsOptions`. This one doesn't really have a predictable size, but I never expect it to get extremely big. So I just held it in the base class. ## Validation Steps Performed * [x] Ran the til unit test * [x] Checked render cluster vector time before/after against `big.txt` from #1064 * [x] Checked VT graphics output vector time before/after against `cacafire` Case | Before | After ---|---|---| `big.txt` | ![image](https://user-images.githubusercontent.com/18221333/84088632-cbaa8400-a9a1-11ea-8932-04b2e12a0477.png) | ![image](https://user-images.githubusercontent.com/18221333/84088996-b6822500-a9a2-11ea-837c-5e32a110156e.png) `cacafire` | ![image](https://user-images.githubusercontent.com/18221333/84089153-22648d80-a9a3-11ea-8567-c3d80efa16a6.png) | ![image](https://user-images.githubusercontent.com/18221333/84089190-34463080-a9a3-11ea-98e5-a236b12330d6.png)
86 lines
3.7 KiB
C++
86 lines
3.7 KiB
C++
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT license.
|
|
|
|
#pragma once
|
|
|
|
#define _TIL_INLINEPREFIX __declspec(noinline) inline
|
|
|
|
#include "til/at.h"
|
|
#include "til/color.h"
|
|
#include "til/math.h"
|
|
#include "til/some.h"
|
|
#include "til/size.h"
|
|
#include "til/point.h"
|
|
#include "til/operators.h"
|
|
#include "til/rectangle.h"
|
|
#include "til/bitmap.h"
|
|
#include "til/u8u16convert.h"
|
|
|
|
namespace til // Terminal Implementation Library. Also: "Today I Learned"
|
|
{
|
|
template<typename T>
|
|
void manage_vector(std::vector<T>& vector, typename std::vector<T>::size_type requestedSize, float shrinkThreshold)
|
|
{
|
|
const auto existingCapacity = vector.capacity();
|
|
const auto requiredCapacity = requestedSize;
|
|
|
|
// Check by integer first as float math is way more expensive.
|
|
if (requiredCapacity < existingCapacity)
|
|
{
|
|
// Now check if it's even worth shrinking. We don't want to shrink by 1 at a time, so meet a threshold first.
|
|
if (requiredCapacity <= gsl::narrow_cast<size_t>((static_cast<float>(existingCapacity) * shrinkThreshold)))
|
|
{
|
|
// There's no real way to force a shrink, so make a new one.
|
|
vector = std::vector<T>{};
|
|
}
|
|
}
|
|
|
|
// Reserve won't shrink on its own and won't grow if we have enough space.
|
|
vector.reserve(requiredCapacity);
|
|
}
|
|
}
|
|
|
|
// These sit outside the namespace because they sit outside for WIL too.
|
|
|
|
// Inspired from RETURN_IF_WIN32_BOOL_FALSE
|
|
// WIL doesn't include a RETURN_BOOL_IF_FALSE, and RETURN_IF_WIN32_BOOL_FALSE
|
|
// will actually return the value of GLE.
|
|
#define RETURN_BOOL_IF_FALSE(b) \
|
|
do \
|
|
{ \
|
|
const bool __boolRet = wil::verify_bool(b); \
|
|
if (!__boolRet) \
|
|
{ \
|
|
return __boolRet; \
|
|
} \
|
|
} while (0, 0)
|
|
|
|
// Due to a bug (DevDiv 441931), Warning 4297 (function marked noexcept throws exception) is detected even when the throwing code is unreachable, such as the end of scope after a return, in function-level catch.
|
|
#define CATCH_LOG_RETURN_FALSE() \
|
|
catch (...) \
|
|
{ \
|
|
__pragma(warning(suppress : 4297)); \
|
|
LOG_CAUGHT_EXCEPTION(); \
|
|
return false; \
|
|
}
|
|
|
|
// MultiByteToWideChar has a bug in it where it can return 0 and then not set last error.
|
|
// WIL has a fit if the last error is 0 when a bool false is returned.
|
|
// This macro doesn't have a fit. It just reports E_UNEXPECTED instead.
|
|
#define THROW_LAST_ERROR_IF_AND_IGNORE_BAD_GLE(condition) \
|
|
do \
|
|
{ \
|
|
if (condition) \
|
|
{ \
|
|
const auto gle = ::GetLastError(); \
|
|
if (gle) \
|
|
{ \
|
|
THROW_WIN32(gle); \
|
|
} \
|
|
else \
|
|
{ \
|
|
THROW_HR(E_UNEXPECTED); \
|
|
} \
|
|
} \
|
|
} while (0, 0)
|