Skip to content

Commit 80b6a00

Browse files
laramielcopybara-github
authored andcommitted
Simplify some random/internal types; expose one function to acquire entropy.
PiperOrigin-RevId: 734243580 Change-Id: Ibc8f3642329b1e7a5a6ed54e505b2ced8dec2f4c
1 parent 0bd8438 commit 80b6a00

15 files changed

+210
-469
lines changed

CMake/AbseilDll.cmake

+2-2
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ set(ABSL_INTERNAL_DLL_FILES
237237
"random/internal/nonsecure_base.h"
238238
"random/internal/pcg_engine.h"
239239
"random/internal/platform.h"
240-
"random/internal/pool_urbg.cc"
241-
"random/internal/pool_urbg.h"
240+
"random/internal/entropy_pool.cc"
241+
"random/internal/entropy_pool.h"
242242
"random/internal/randen.cc"
243243
"random/internal/randen.h"
244244
"random/internal/randen_detect.cc"

absl/random/BUILD.bazel

+1-3
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ cc_library(
111111
":seed_gen_exception",
112112
"//absl/base:config",
113113
"//absl/base:nullability",
114-
"//absl/random/internal:pool_urbg",
114+
"//absl/random/internal:entropy_pool",
115115
"//absl/random/internal:salted_seed_seq",
116116
"//absl/random/internal:seed_material",
117117
"//absl/strings:string_view",
@@ -538,9 +538,7 @@ cc_binary(
538538
deps = [
539539
":distributions",
540540
":random",
541-
":seed_sequences",
542541
"//absl/base:core_headers",
543-
"//absl/meta:type_traits",
544542
"//absl/random/internal:fast_uniform_bits",
545543
"//absl/random/internal:randen_engine",
546544
"@google_benchmark//:benchmark_main",

absl/random/CMakeLists.txt

+13-17
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ absl_cc_library(
229229
absl::config
230230
absl::inlined_vector
231231
absl::nullability
232-
absl::random_internal_pool_urbg
232+
absl::random_internal_entropy_pool
233233
absl::random_internal_salted_seed_seq
234234
absl::random_internal_seed_material
235235
absl::random_seed_gen_exception
@@ -582,7 +582,7 @@ absl_cc_library(
582582
${ABSL_DEFAULT_LINKOPTS}
583583
$<$<BOOL:${MINGW}>:-lbcrypt>
584584
DEPS
585-
absl::core_headers
585+
absl::config
586586
absl::optional
587587
absl::random_internal_fast_uniform_bits
588588
absl::raw_logging_internal
@@ -593,11 +593,11 @@ absl_cc_library(
593593
# Internal-only target, do not depend on directly.
594594
absl_cc_library(
595595
NAME
596-
random_internal_pool_urbg
596+
random_internal_entropy_pool
597597
SRCS
598-
"internal/pool_urbg.cc"
598+
"internal/entropy_pool.cc"
599599
HDRS
600-
"internal/pool_urbg.h"
600+
"internal/entropy_pool.h"
601601
COPTS
602602
${ABSL_DEFAULT_COPTS}
603603
LINKOPTS
@@ -606,13 +606,12 @@ absl_cc_library(
606606
absl::base
607607
absl::config
608608
absl::core_headers
609-
absl::endian
609+
absl::random_internal_platform
610610
absl::random_internal_randen
611611
absl::random_internal_seed_material
612-
absl::random_internal_traits
613612
absl::random_seed_gen_exception
614-
absl::raw_logging_internal
615613
absl::span
614+
absl::synchronization
616615
)
617616

618617
# Internal-only target, do not depend on directly.
@@ -738,12 +737,11 @@ absl_cc_library(
738737
LINKOPTS
739738
${ABSL_DEFAULT_LINKOPTS}
740739
DEPS
741-
absl::core_headers
740+
absl::config
742741
absl::inlined_vector
743-
absl::random_internal_pool_urbg
742+
absl::random_internal_entropy_pool
744743
absl::random_internal_salted_seed_seq
745744
absl::random_internal_seed_material
746-
absl::span
747745
absl::type_traits
748746
)
749747

@@ -1052,24 +1050,23 @@ absl_cc_test(
10521050
${ABSL_DEFAULT_LINKOPTS}
10531051
DEPS
10541052
absl::random_internal_seed_material
1053+
absl::span
10551054
GTest::gmock
10561055
GTest::gtest_main
10571056
)
10581057

10591058
# Internal-only target, do not depend on directly.
10601059
absl_cc_test(
10611060
NAME
1062-
random_internal_pool_urbg_test
1061+
random_internal_entropy_pool_test
10631062
SRCS
1064-
"internal/pool_urbg_test.cc"
1063+
"internal/entropy_pool_test.cc"
10651064
COPTS
10661065
${ABSL_TEST_COPTS}
10671066
LINKOPTS
10681067
${ABSL_DEFAULT_LINKOPTS}
10691068
DEPS
1070-
absl::random_internal_pool_urbg
1071-
absl::span
1072-
absl::type_traits
1069+
absl::random_internal_entropy_pool
10731070
GTest::gtest_main
10741071
)
10751072

@@ -1242,7 +1239,6 @@ absl_cc_test(
12421239
${ABSL_DEFAULT_LINKOPTS}
12431240
DEPS
12441241
absl::random_internal_wide_multiply
1245-
absl::bits
12461242
absl::int128
12471243
GTest::gmock
12481244
GTest::gtest_main

absl/random/benchmarks.cc

+16-23
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <vector>
2727

2828
#include "absl/base/macros.h"
29-
#include "absl/meta/type_traits.h"
3029
#include "absl/random/bernoulli_distribution.h"
3130
#include "absl/random/beta_distribution.h"
3231
#include "absl/random/exponential_distribution.h"
@@ -65,10 +64,10 @@ class PrecompiledSeedSeq {
6564
PrecompiledSeedSeq() = default;
6665

6766
template <typename Iterator>
68-
PrecompiledSeedSeq(Iterator begin, Iterator end) {}
67+
PrecompiledSeedSeq(Iterator, Iterator) {}
6968

7069
template <typename T>
71-
PrecompiledSeedSeq(std::initializer_list<T> il) {}
70+
PrecompiledSeedSeq(std::initializer_list<T>) {}
7271

7372
template <typename OutIterator>
7473
void generate(OutIterator begin, OutIterator end) {
@@ -89,30 +88,23 @@ class PrecompiledSeedSeq {
8988
}
9089
};
9190

92-
// use_default_initialization<T> indicates whether the random engine
93-
// T must be default initialized, or whether we may initialize it using
94-
// a seed sequence. This is used because some engines do not accept seed
95-
// sequence-based initialization.
96-
template <typename E>
97-
using use_default_initialization = std::false_type;
91+
// Triggers default constructor initialization.
92+
class DefaultConstructorSeedSeq {};
9893

9994
// make_engine<T, SSeq> returns a random_engine which is initialized,
10095
// either via the default constructor, when use_default_initialization<T>
10196
// is true, or via the indicated seed sequence, SSeq.
102-
template <typename Engine, typename SSeq = PrecompiledSeedSeq>
103-
typename absl::enable_if_t<!use_default_initialization<Engine>::value, Engine>
104-
make_engine() {
105-
// Initialize the random engine using the seed sequence SSeq, which
106-
// is constructed from the precompiled seed data.
107-
SSeq seq(std::begin(kSeedData), std::end(kSeedData));
108-
return Engine(seq);
109-
}
110-
111-
template <typename Engine, typename SSeq = PrecompiledSeedSeq>
112-
typename absl::enable_if_t<use_default_initialization<Engine>::value, Engine>
113-
make_engine() {
114-
// Initialize the random engine using the default constructor.
115-
return Engine();
97+
template <typename Engine, typename SSeq = DefaultConstructorSeedSeq>
98+
Engine make_engine() {
99+
constexpr bool use_default_initialization =
100+
std::is_same_v<SSeq, DefaultConstructorSeedSeq>;
101+
if constexpr (use_default_initialization) {
102+
return Engine();
103+
} else {
104+
// Otherwise, use the provided seed sequence.
105+
SSeq seq(std::begin(kSeedData), std::end(kSeedData));
106+
return Engine(seq);
107+
}
116108
}
117109

118110
template <typename Engine, typename SSeq>
@@ -248,6 +240,7 @@ void BM_Thread(benchmark::State& state) {
248240

249241
// Normal benchmark suite
250242
#define BM_BASIC(Engine) \
243+
BENCHMARK_TEMPLATE(BM_Construct, Engine, DefaultConstructorSeedSeq); \
251244
BENCHMARK_TEMPLATE(BM_Construct, Engine, PrecompiledSeedSeq); \
252245
BENCHMARK_TEMPLATE(BM_Construct, Engine, std::seed_seq); \
253246
BENCHMARK_TEMPLATE(BM_Direct, Engine); \

absl/random/internal/BUILD.bazel

+11-20
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ cc_library(
164164
}),
165165
deps = [
166166
":fast_uniform_bits",
167-
"//absl/base:core_headers",
167+
"//absl/base:config",
168168
"//absl/base:dynamic_annotations",
169169
"//absl/base:raw_logging_internal",
170170
"//absl/strings",
@@ -174,13 +174,9 @@ cc_library(
174174
)
175175

176176
cc_library(
177-
name = "pool_urbg",
178-
srcs = [
179-
"pool_urbg.cc",
180-
],
181-
hdrs = [
182-
"pool_urbg.h",
183-
],
177+
name = "entropy_pool",
178+
srcs = ["entropy_pool.cc"],
179+
hdrs = ["entropy_pool.h"],
184180
copts = ABSL_DEFAULT_COPTS,
185181
linkopts = select({
186182
"@rules_cc//cc/compiler:msvc-cl": [],
@@ -189,14 +185,12 @@ cc_library(
189185
"//conditions:default": ["-pthread"],
190186
}) + ABSL_DEFAULT_LINKOPTS,
191187
deps = [
188+
":platform",
192189
":randen",
193190
":seed_material",
194-
":traits",
195191
"//absl/base",
196192
"//absl/base:config",
197193
"//absl/base:core_headers",
198-
"//absl/base:endian",
199-
"//absl/base:raw_logging_internal",
200194
"//absl/random:seed_gen_exception",
201195
"//absl/types:span",
202196
],
@@ -299,13 +293,12 @@ cc_library(
299293
copts = ABSL_DEFAULT_COPTS,
300294
linkopts = ABSL_DEFAULT_LINKOPTS,
301295
deps = [
302-
":pool_urbg",
296+
":entropy_pool",
303297
":salted_seed_seq",
304298
":seed_material",
305-
"//absl/base:core_headers",
299+
"//absl/base:config",
306300
"//absl/container:inlined_vector",
307301
"//absl/meta:type_traits",
308-
"//absl/types:span",
309302
],
310303
)
311304

@@ -639,23 +632,22 @@ cc_test(
639632
linkopts = ABSL_DEFAULT_LINKOPTS,
640633
deps = [
641634
":seed_material",
635+
"//absl/types:span",
642636
"@googletest//:gtest",
643637
"@googletest//:gtest_main",
644638
],
645639
)
646640

647641
cc_test(
648-
name = "pool_urbg_test",
642+
name = "entropy_pool_test",
649643
size = "small",
650644
srcs = [
651-
"pool_urbg_test.cc",
645+
"entropy_pool_test.cc",
652646
],
653647
copts = ABSL_TEST_COPTS,
654648
linkopts = ABSL_DEFAULT_LINKOPTS,
655649
deps = [
656-
":pool_urbg",
657-
"//absl/meta:type_traits",
658-
"//absl/types:span",
650+
":entropy_pool",
659651
"@googletest//:gtest",
660652
"@googletest//:gtest_main",
661653
],
@@ -750,7 +742,6 @@ cc_test(
750742
linkopts = ABSL_DEFAULT_LINKOPTS,
751743
deps = [
752744
":wide_multiply",
753-
"//absl/numeric:bits",
754745
"//absl/numeric:int128",
755746
"@googletest//:gtest",
756747
"@googletest//:gtest_main",

0 commit comments

Comments
 (0)