Skip to content

[libc++] Remove redundant assignments in bitset string-like constructors #121424

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

Merged
merged 1 commit into from
Mar 18, 2025

Conversation

winner245
Copy link
Contributor

@winner245 winner245 commented Jan 1, 2025

The following three string-like constructors for std::bitset

  • bitset(const CharT* str, std::size_t n, CharT zero, CharT one);
  • bitset(const std::basic_string<CharT, Traits, Alloc>& str, typename std::basic_string<CharT, Traits, Alloc>::size_type pos, CharT zero, CharT one);
  • bitset(std::basic_string_view<CharT, Traits> str, std::size_t pos, std::size_t n, CharT zero, CharT one)

already initialize the underlying storage array to all zeroes via default-constructor of the base class __bitset. Therefore, re-assigning the storage array to zeroes via std::fill_n in the string-like constructors is truly redundant.

@winner245 winner245 force-pushed the simplify-string-ctor branch from c1a2de2 to da94a03 Compare January 1, 2025 12:55
@winner245 winner245 force-pushed the simplify-string-ctor branch from da94a03 to 52b486d Compare February 12, 2025 16:28
@winner245 winner245 marked this pull request as ready for review March 17, 2025 01:49
@winner245 winner245 requested a review from a team as a code owner March 17, 2025 01:49
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Mar 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 17, 2025

@llvm/pr-subscribers-libcxx

Author: Peng Liu (winner245)

Changes

The following three string-like constructors for std::bitset

  • bitset(const CharT* str, std::size_t n, CharT zero, CharT one);
  • bitset(const std::basic_string&lt;CharT, Traits, Alloc&gt;&amp; str, typename std::basic_string&lt;CharT, Traits, Alloc&gt;::size_type pos, CharT zero, CharT one);
  • bitset(std::basic_string_view&lt;CharT, Traits&gt; str, std::size_t pos, std::size_t n, CharT zero, CharT one)

already initialize the underlying storage array to all zeroes via default-constructor of the base class __bitset. Therefore, re-assigning the storage array to zeroes via std::fill_n in the string-like constructors is truly redundant.


Full diff: https://github.com/llvm/llvm-project/pull/121424.diff

1 Files Affected:

  • (modified) libcxx/include/bitset (-1)
diff --git a/libcxx/include/bitset b/libcxx/include/bitset
index a20842985b3d5..3cd4312e0171d 100644
--- a/libcxx/include/bitset
+++ b/libcxx/include/bitset
@@ -736,7 +736,6 @@ private:
       _CharT __c   = __str[__mp - 1 - __i];
       (*this)[__i] = _Traits::eq(__c, __one);
     }
-    std::fill(__base::__make_iter(__i), __base::__make_iter(_Size), false);
   }
 
   _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT { return __base::__hash_code(); }

@winner245 winner245 merged commit b391e80 into llvm:main Mar 18, 2025
81 checks passed
@winner245 winner245 deleted the simplify-string-ctor branch March 18, 2025 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants