VS 2015 std::char_traits<char16_t> operations

150 Views Asked by At

At my workplace, we changed string type (which holds internationalized characters) for from std::wstring to std::u16string after VS 2015(Update 3) compiler upgrade.

Due to this, we are seeing loads of performance regressions such as this.

The profiler analysis reveals that std::u16string's std::char_traits<char16_t> operations such as copy, compare, find and assign are the most hit and are taking longer than std::wstring's std::char_traits<wchar_t> counterparts.

These std::char_traits<wchar_t> operations are written in terms of std::wmem* and std::char_traits<char16_t> operations are written in terms of for loops.

If we change these traits operations for char16_t type (or std::u16string) to use our own customized traits, we are seeing performance improvements with performance comparable to std::wstring.

We are planning to write our own custom traits (until MS fixes it for next version of VS) as follows

struct string_custom_traits : public std::char_traits<char16_t>
{
    static const char16_t * copy(char16_t* dest, const char16_t* src, size_t count)
    {
        return (count == 0 ? src : (char16_t*)std::memcpy(dest, src, count * sizeof(char16_t)));
    }               
};

Would that be OK? Are there any problems with this approach ?

0

There are 0 best solutions below