Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<bitset>: Investigate further performance improvements #3858

Closed
StephanTLavavej opened this issue Jul 11, 2023 · 1 comment · Fixed by #3960
Closed

<bitset>: Investigate further performance improvements #3858

StephanTLavavej opened this issue Jul 11, 2023 · 1 comment · Fixed by #3960
Labels
fixed Something works now, yay! performance Must go faster

Comments

@StephanTLavavej
Copy link
Member

Followup to #3838 which is adding a benchmark - thanks @achabense!

  • @AlexGuteniev suggested using basic_string::resize_and_overwrite, creating an _Ugly version for unconditional use internally. (We generally avoid adding totally novel secret machinery to Standard classes as it can become a maintenance burden, but simply having _Ugly names to access Future Technology is easy and common.)
  • @AlexGuteniev also suggested investigating whether branchless codegen for assigning _Elem0 vs. _Elem1 would be superior. (Note: we cannot assume that their values are consecutive, they could be 'M' and 'E'.)
  • I suggest investigating SIMD, as this would seem to be highly amenable to vectorization.

Regardless of what's investigated, I recommend profiling each change in isolation.

@AlexGuteniev
Copy link
Contributor

Vectorization approach suggested by @Alcaro :

void bits_to_str16_walrus(char* dest, uint16_t src)
{
	__m128i bits = _mm_cvtsi32_si128(src);
	bits = _mm_unpacklo_epi8(bits, bits);
	bits = _mm_unpacklo_epi8(bits, bits);
	bits = _mm_unpacklo_epi8(bits, bits);
	bits = _mm_and_si128(bits, _mm_set1_epi64x(0x8040201008040201));
	bits = _mm_cmpeq_epi8(bits, _mm_setzero_si128());
	bits = _mm_and_si128(bits, _mm_set1_epi8(0x01));
	bits = _mm_xor_si128(bits, _mm_set1_epi8(0x31));
	_mm_storeu_si128((__m128i*)dest, bits);
}

https://godbolt.org/z/hK1YaY8fK
(saving it here for now, so that it is not lost while reviewing the current PR)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed Something works now, yay! performance Must go faster
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants