JuliaLang/julia — pull request #3265 by carlobaldassi (2013-06-01 19:37:53+00:00)
Title: BitArray performance enhancements via introduction of UnsafeArray and UnsafeBitArray

(See also #2360)

This is possibly controversial, hence the pull request.
First, see the performance comparisons of affected functions:
https://gist.github.com/carlobaldassi/5691454

Some functions become 10 to 20 times faster; for most the boost is about 1.5x / 2x.

Basically, I introduced two new immutable types, `UnsafeArray` and `UnsafeBitArray`, for internal use, and used them throughout `bitarray.jl`. They actually have the semantics (and inherit from) an `AbstractVector`, but I see them as wrappers around arrays. The code for `UnsafeArray` is a few lines of code in `array.jl`, and could be used there with analogous performance boosts, but I did not touch `Array` functions in this commit.
What I like here is that if you know what you're doing the code mostly stays clean and readable, basically hiding away stuff like `getindex_unchecked(B.chunks, i)` and `unsafe_store!(pointer(A), x, i)` and getting back standard notation as `B[i]` and `A[i] = x`.
(This was always the case, with the exception of the `find` function, for which the pointer notation still gives a better performance, at least in my tests.)
Thoughts?