Eliminate a bunch of memory allocations when getting column widths
This commit is contained in:
parent
8c1b20e651
commit
bddf44ddde
2 changed files with 11 additions and 0 deletions
|
@ -31,7 +31,17 @@ int WidthHelper::operator()(boost::flyweight<std::string> const& str) {
|
|||
if (str.get().empty()) return 0;
|
||||
auto it = widths.find(str);
|
||||
if (it != end(widths)) return it->second;
|
||||
|
||||
#ifdef _WIN32
|
||||
wxMBConvUTF8 conv;
|
||||
size_t len = conv.ToWChar(nullptr, 0, str.get().c_str(), str.get().size());
|
||||
scratch.resize(len);
|
||||
conv.ToWChar(const_cast<wchar_t *>(scratch.wx_str()), len, str.get().c_str(), str.get().size());
|
||||
int width = dc.GetTextExtent(scratch).GetWidth();
|
||||
#else
|
||||
int width = dc.GetTextExtent(to_wx(str)).GetWidth();
|
||||
#endif
|
||||
|
||||
widths[str] = width;
|
||||
return width;
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ namespace std {
|
|||
struct WidthHelper {
|
||||
wxDC &dc;
|
||||
std::unordered_map<boost::flyweight<std::string>, int> widths;
|
||||
wxString scratch;
|
||||
|
||||
int operator()(boost::flyweight<std::string> const& str);
|
||||
int operator()(std::string const& str);
|
||||
|
|
Loading…
Reference in a new issue