Replies: 1 comment 13 replies
-
In the case of "rand" it's special, since that is a convenience method for getting random numbers from a common thread local random, it's not an optimized function. For that you should instead use a custom random generator. |
Beta Was this translation helpful? Give feedback.
13 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
A lot of operations are defined on "uninitialized" types. For example
rand
checks whether the underlying structure is initialized, and if not, it seeds it, and sets theinitialized
flag.This seems like unnecessary overhead and complexity to me. Firstly, the check itself. While it'll (probably) be branch-predicted correctly from the third use onwards, and might only add minimal overhead in microbenchmarks, branch predictor saturation exists, and in larger codebases, this will manifest as latency that a user won't track down.
Some types don't have an existing field which indicates whether they are initialized, meaning we have to store extra state, as is the case with the
initialized
boolean.Proposed solution:
Allow the user to specify that the initialization of a type is mandatory, via an attribute on the type declaration.
This would (probably) mean having initializers that return a new value (eg.
new :: uint -> HashMap
), rather than initializing an uninitialized value.Optionally, allow users to specify a default initializer, which would be used instead of the default zero-initialization.
Migrate the stdlib to this pattern, and remove lots of checks for
!initialized
.Beta Was this translation helpful? Give feedback.
All reactions