diff options
author | dalecurtis@chromium.org <dalecurtis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-23 02:14:28 +0000 |
---|---|---|
committer | dalecurtis@chromium.org <dalecurtis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-23 02:14:28 +0000 |
commit | c910c5a68ca7baf2da4edfd722f55f2b089402e2 (patch) | |
tree | b844a63d20f8c7437e4ace80dfef4da86aa3f94f /base/rand_util_posix.cc | |
parent | 52bd819c62c4e6983f2c34a449beadca49d7cdd6 (diff) | |
download | chromium_src-c910c5a68ca7baf2da4edfd722f55f2b089402e2.zip chromium_src-c910c5a68ca7baf2da4edfd722f55f2b089402e2.tar.gz chromium_src-c910c5a68ca7baf2da4edfd722f55f2b089402e2.tar.bz2 |
Improve base::RandBytes() performance by 1.75x-2.10x on POSIX.
No real changes, just avoids doling out Uint64 sized chunks by
using the underlying method to hand out correctly sized blocks.
Windows is the only platform which doesn't have a byte stream
based generator, so I've moved the generic RandBytes() method
there and added native methods for other platforms which reuse
each platforms internal generator.
Performance measured by the new benchmark test over 5 runs,
each representing 10 generations of 1mb of random data:
Linux x64:
Original: 1199625.4
Modified: 686480.2
Improvement: 1.75x
On OSX (10.9.1):
Original: 1532669.8
Modified: 734808.0
Improvement: 2.10x
BUG=none
TEST=new benchmark unittest.
Review URL: https://codereview.chromium.org/140773006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@246486 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/rand_util_posix.cc')
-rw-r--r-- | base/rand_util_posix.cc | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/base/rand_util_posix.cc b/base/rand_util_posix.cc index 082d649..0a72a20 100644 --- a/base/rand_util_posix.cc +++ b/base/rand_util_posix.cc @@ -20,19 +20,16 @@ namespace { // we can use LazyInstance to handle opening it on the first access. class URandomFd { public: - URandomFd() { - fd_ = open("/dev/urandom", O_RDONLY); + URandomFd() : fd_(open("/dev/urandom", O_RDONLY)) { DCHECK_GE(fd_, 0) << "Cannot open /dev/urandom: " << errno; } - ~URandomFd() { - close(fd_); - } + ~URandomFd() { close(fd_); } int fd() const { return fd_; } private: - int fd_; + const int fd_; }; base::LazyInstance<URandomFd>::Leaky g_urandom_fd = LAZY_INSTANCE_INITIALIZER; @@ -44,13 +41,15 @@ namespace base { // NOTE: This function must be cryptographically secure. http://crbug.com/140076 uint64 RandUint64() { uint64 number; + RandBytes(&number, sizeof(number)); + return number; +} - int urandom_fd = g_urandom_fd.Pointer()->fd(); - bool success = ReadFromFD(urandom_fd, reinterpret_cast<char*>(&number), - sizeof(number)); +void RandBytes(void* output, size_t output_length) { + const int urandom_fd = g_urandom_fd.Pointer()->fd(); + const bool success = + ReadFromFD(urandom_fd, static_cast<char*>(output), output_length); CHECK(success); - - return number; } int GetUrandomFD(void) { |