-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ByteVec: immediate / remote immutable byte vectors + intrinsics. #8964
Closed
Closed
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
b712bea
ByteVec: immediate / remote immutable byte vectors + intrinsics.
StefanKarpinski 49fa2a6
ByteVec: move bounds checking into the bytevec_ref intrinsic.
StefanKarpinski 8e7a309
ByteVec: make bytevec_ref respect `@inbounds` annotations.
StefanKarpinski 4ac1a8e
Str: a couple of `@inbounds` annotations for Str methods.
StefanKarpinski d01a33e
ByteVec: remove index dependency from the here/there branch.
StefanKarpinski 585288b
ByteVec: `long neglen` => `intptr_t neglen` as per @stevengj.
StefanKarpinski 6291b61
ByteVec: decorate remote bytevec data as constant.
StefanKarpinski cefb779
ByteVec: bytevec_ref32 fetching individual bytes, shifts and ors.
StefanKarpinski 51fbcdd
ByteVec: use bytevec_ref32 intrinsic to decode UTF-8 characters.
StefanKarpinski 5ac7e34
ByteVec: bytevec_eq intrinsic (warmup for bytevec_cmp).
StefanKarpinski 84057ec
codegen: type T_intN more precisely as IntegerType pointers.
StefanKarpinski ac9a0e3
bitshifts: don't do checked conversion to Int32.
StefanKarpinski 59cbe1e
ByteVec: fast cmp implementation.
StefanKarpinski 922294d
ByteVec: length(::ByteVec) in Julia instead of as an intrinsic.
StefanKarpinski b670161
ByteVec: replace bytevec_eq with a Julia-native implementation.
StefanKarpinski 3dace93
ByteVec: bytevec_utf8_ref intrinsic for UTF-8 character decoding.
StefanKarpinski ea1ba4f
ByteVec: fast endof(s::Str) using clever bit tricks and getu32.
StefanKarpinski 94b9253
ByteVec: rewrite bytevec_ref32 to only require non-vector code.
StefanKarpinski e6c60b6
ByteVec: make bytevec_ref also use only normal integer instructions.
StefanKarpinski c5d69a7
ByteVec: fix inverted bounds check elimination condition.
StefanKarpinski 780f3fe
ByteVec/Str: more efficient length(::Str), eps for "here" strings.
StefanKarpinski 4edebd9
Str/length: cleaner definition of the bitmask for length computation
StefanKarpinski f6f0267
Str: branch-free UTF-8 decoding is competetive with current decode.
StefanKarpinski 84a65cd
src/codegen.cpp: add CSE pass right before loop unswitching.
StefanKarpinski 6add6b7
ByteVec: use a single `if` for bounds check and error raising.
StefanKarpinski db067d0
ByteVec/Str: return `\ufffd` when decoding a continuation byte.
StefanKarpinski d7e5f7c
ByteVec/Str: handle invalid UTF-8 data correctly without branches.
StefanKarpinski 64119c9
UTF8String: use branch-free decoding. unfortunately, it's slower.
StefanKarpinski 1c8cfcb
ByteVec/Str: special case ASCII iteration in next method for Str.
StefanKarpinski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import Core.ByteVec | ||
export ByteVec, Str | ||
|
||
ByteVec(a::Vector{UInt8}) = ccall(:jl_bytevec, ByteVec, (Ptr{UInt8}, Csize_t), a, length(a)) | ||
ByteVec(s::AbstractString) = ByteVec(bytestring(s).data) | ||
|
||
size(b::ByteVec) = (length(b),) | ||
|
||
function length(b::ByteVec) | ||
here = (b.x >>> 8*(sizeof(b.x)-1)) % Int | ||
there = -(b.x >> 8*sizeof(Int)) % Int | ||
ifelse(b.x < 0, there, here) | ||
end | ||
|
||
getindex(b::ByteVec, i::Real) = | ||
box(UInt8, bytevec_ref(unbox(typeof(b.x), b.x), unbox(Int, Int(i)))) | ||
getu32(b::ByteVec, i::Int) = | ||
box(UInt32, bytevec_ref32(unbox(typeof(b.x), b.x), unbox(Int, i))) | ||
|
||
function ==(a::ByteVec, b::ByteVec) | ||
a_hi = (a.x >> 8*sizeof(Int)) % Int | ||
b_hi = (b.x >> 8*sizeof(Int)) % Int | ||
(a_hi != b_hi) | (a_hi >= 0) | (b_hi >= 0) && return a.x == b.x | ||
pa = reinterpret(Ptr{Uint8}, a.x % UInt) | ||
pb = reinterpret(Ptr{Uint8}, b.x % UInt) | ||
ccall(:memcmp, Cint, (Ptr{Uint8}, Ptr{Uint8}, Csize_t), pa, pb, -a_hi % Uint) == 0 | ||
end | ||
|
||
function cmp(a::ByteVec, b::ByteVec) | ||
a_x, b_x = a.x, b.x | ||
a_here, b_here = a_x >= 0, b_x >= 0 | ||
if !(a_here & b_here) | ||
if b_here | ||
a_x = unsafe_load(reinterpret(Ptr{typeof(a_x)}, a_x % UInt)) | ||
elseif a_here | ||
b_x = unsafe_load(reinterpret(Ptr{typeof(b_x)}, b_x % UInt)) | ||
else | ||
pa = reinterpret(Ptr{Uint8}, a_x % UInt) | ||
pb = reinterpret(Ptr{Uint8}, b_x % UInt) | ||
la = -(a_x >>> 8*sizeof(Int)) % UInt | ||
lb = -(b_x >>> 8*sizeof(Int)) % UInt | ||
c = Int(ccall(:memcmp, Cint, (Ptr{Uint8}, Ptr{Uint8}, Csize_t), pa, pb, min(la,lb))) | ||
return ifelse(c == 0, cmp(la,lb), sign(c)) | ||
end | ||
end | ||
cmp(bswap(a_x), bswap(b_x)) | ||
end | ||
isless(x::ByteVec, y::ByteVec) = cmp(x, y) < 0 | ||
|
||
start(b::ByteVec) = 1 | ||
next(b::ByteVec, i::Int) = (b[i], i+1) | ||
done(b::ByteVec, i::Int) = length(b) < i | ||
|
||
## ByteVec-based string type ## | ||
|
||
immutable Str <: AbstractString | ||
data::ByteVec | ||
end | ||
Str(s::AbstractString) = Str(ByteVec(s)) | ||
|
||
@inline function endof(s::Str) | ||
n = length(s.data) | ||
@inbounds u = getu32(s.data, n-3) | ||
x = (u & 0xc0c0c0c0) $ 0x80808080 | ||
n - leading_zeros(x) >>> 3 | ||
end | ||
|
||
function next(s::Str, k::Int) | ||
a = bswap(getu32(s.data, k)) | ||
0 <= reinterpret(Int32, a) && return Char(a >> 24), k + 1 | ||
l = leading_ones(a) | ||
b = (a << l >> l) $ 0x808080 | ||
r = 32 - 8l | ||
c = b >> r | ||
t = (l != 1) & (l <= 4) & ((b & 0xc0c0c0) >> r == 0) | ||
d = ( (c >> 24) << 18) | | ||
(((c >> 16) & 0xff) << 12) | | ||
(((c >> 8) & 0xff) << 6) | (c & 0xff) | ||
ifelse(t, Char(d), '\ufffd'), k + ifelse(t, l, 1) | ||
end | ||
|
||
const mask = div(typemax(UInt),typemax(UInt8)) | ||
|
||
function length(s::Str) | ||
x = s.data.x | ||
if 0 <= x | ||
lo, hi = x % UInt, (x >>> 8*sizeof(Uint)) % Uint | ||
n = (hi >>> 8*(sizeof(Uint)-1)) % Int | ||
n -= count_ones((mask & (lo >>> 7)) & ~(mask & (lo >>> 6))) | ||
n -= count_ones((mask & (hi >>> 7)) & ~(mask & (hi >>> 6))) | ||
return n | ||
else | ||
p = reinterpret(Ptr{Uint8}, x % UInt) | ||
n = -(x >> 8*sizeof(Int)) % Int | ||
for i = 1:n | ||
b = unsafe_load(p, i) | ||
n -= (b & 0xc0) == 0x80 | ||
end | ||
return n | ||
end | ||
end | ||
|
||
## overload methods for efficiency ## | ||
|
||
sizeof(s::Str) = length(s.data) | ||
|
||
==(s::Str, t::Str) = ==(s.data, t.data) | ||
isless(s::Str, t::Str) = isless(s.data, t.data) | ||
cmp(s::Str, t::Str) = cmp(s.data, t.data) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ export | |
BitArray, | ||
BitMatrix, | ||
BitVector, | ||
ByteVec, | ||
CFILE, | ||
Cmd, | ||
Colon, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
n == 2*sizeof(void*) - 1
, then it looks like the string data cannot be NUL-terminated. Julia currently guarantees NUL-termination (in array.c) so that strings can be passed to external C code expecting NUL-terminated strings.Or is the plan to implement this on top of
ByteVec
s, similar to how it is implemented for UTF-16 and UTF-32 strings (i.e.str.data
is actually a bytevec of length 1 more than the length of the string, and contains an explicit NUL terminator)? This might be cleaner, although it is a subtle breaking change for any code that currently looks at thedata
field of strings. (We could rename all of the string-typedata
fields todata0
in order to make the breakage noisy.)