Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1863)

Unified Diff: base/rand_util_posix.cc

Issue 140773006: Improve base::RandBytes() performance by 1.75x-2.10x on POSIX. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Simplify NaCl. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/rand_util_posix.cc
diff --git a/base/rand_util_posix.cc b/base/rand_util_posix.cc
index 082d64923d5b9bf63890565bcf922eb361cc3ab3..7a10ffd89f2d3b67a678abe416d50369c0d6b708 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;
@@ -42,14 +39,16 @@ base::LazyInstance<URandomFd>::Leaky g_urandom_fd = LAZY_INSTANCE_INITIALIZER;
namespace base {
// NOTE: This function must be cryptographically secure. http://crbug.com/140076
wtc 2014/01/22 22:29:09 This comment should be in front of RandUint64(). I
DaleCurtis 2014/01/22 22:54:14 Done.
-uint64 RandUint64() {
- uint64 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) {
wtc 2014/01/22 22:29:09 Define RandBytes after RandUint64, as the Style Gu
DaleCurtis 2014/01/22 22:54:14 Done.
+ const int urandom_fd = g_urandom_fd.Pointer()->fd();
+ const bool success =
+ ReadFromFD(urandom_fd, static_cast<char*>(output), output_length);
CHECK(success);
+}
+uint64 RandUint64() {
+ uint64 number;
+ RandBytes(&number, sizeof(number));
return number;
}

Powered by Google App Engine
This is Rietveld 408576698