Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
StackExchange.Redis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
StackExchange.Redis
Commits
80be84fc
Commit
80be84fc
authored
Jul 09, 2018
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make StartsWith more efficient
parent
c33f7283
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
27 deletions
+74
-27
RedisValueEquivalency.cs
StackExchange.Redis.Tests/RedisValueEquivalency.cs
+26
-24
RedisValue.cs
StackExchange.Redis/StackExchange/Redis/RedisValue.cs
+48
-3
No files found.
StackExchange.Redis.Tests/RedisValueEquivalency.cs
View file @
80be84fc
using
System.Text
;
using
System.Runtime.CompilerServices
;
using
System.Text
;
using
Xunit
;
namespace
StackExchange.Redis.Tests
...
...
@@ -151,39 +152,40 @@ private static void CheckString(RedisValue value, string expected)
private
static
byte
[]
Bytes
(
string
s
)
=>
s
==
null
?
null
:
Encoding
.
UTF8
.
GetBytes
(
s
);
string
LineNumber
([
CallerLineNumber
]
int
lineNumber
=
0
)
=>
lineNumber
.
ToString
();
[
Fact
]
public
void
RedisValueStartsWith
()
{
// test strings
RedisValue
x
=
"abc"
;
Assert
.
True
(
x
.
StartsWith
(
"a"
));
Assert
.
True
(
x
.
StartsWith
(
"ab"
));
Assert
.
True
(
x
.
StartsWith
(
"abc"
));
Assert
.
False
(
x
.
StartsWith
(
"abd"
));
Assert
.
False
(
x
.
StartsWith
(
"abcd"
));
Assert
.
False
(
x
.
StartsWith
(
123
));
Assert
.
False
(
x
.
StartsWith
(
false
));
Assert
.
True
(
x
.
StartsWith
(
"a"
)
,
LineNumber
()
);
Assert
.
True
(
x
.
StartsWith
(
"ab"
)
,
LineNumber
()
);
Assert
.
True
(
x
.
StartsWith
(
"abc"
)
,
LineNumber
()
);
Assert
.
False
(
x
.
StartsWith
(
"abd"
)
,
LineNumber
()
);
Assert
.
False
(
x
.
StartsWith
(
"abcd"
)
,
LineNumber
()
);
Assert
.
False
(
x
.
StartsWith
(
123
)
,
LineNumber
()
);
Assert
.
False
(
x
.
StartsWith
(
false
)
,
LineNumber
()
);
// test binary
x
=
Encoding
.
ASCII
.
GetBytes
(
"abc"
);
Assert
.
True
(
x
.
StartsWith
(
"a"
));
Assert
.
True
(
x
.
StartsWith
(
"ab"
));
Assert
.
True
(
x
.
StartsWith
(
"abc"
));
Assert
.
False
(
x
.
StartsWith
(
"abd"
));
Assert
.
False
(
x
.
StartsWith
(
"abcd"
));
Assert
.
False
(
x
.
StartsWith
(
123
));
Assert
.
False
(
x
.
StartsWith
(
false
));
Assert
.
True
(
x
.
StartsWith
(
Encoding
.
ASCII
.
GetBytes
(
"a"
)));
Assert
.
True
(
x
.
StartsWith
(
Encoding
.
ASCII
.
GetBytes
(
"ab"
)));
Assert
.
True
(
x
.
StartsWith
(
Encoding
.
ASCII
.
GetBytes
(
"abc"
)));
Assert
.
False
(
x
.
StartsWith
(
Encoding
.
ASCII
.
GetBytes
(
"abd"
)));
Assert
.
False
(
x
.
StartsWith
(
Encoding
.
ASCII
.
GetBytes
(
"abcd"
)));
Assert
.
True
(
x
.
StartsWith
(
"a"
)
,
LineNumber
()
);
Assert
.
True
(
x
.
StartsWith
(
"ab"
)
,
LineNumber
()
);
Assert
.
True
(
x
.
StartsWith
(
"abc"
)
,
LineNumber
()
);
Assert
.
False
(
x
.
StartsWith
(
"abd"
)
,
LineNumber
()
);
Assert
.
False
(
x
.
StartsWith
(
"abcd"
)
,
LineNumber
()
);
Assert
.
False
(
x
.
StartsWith
(
123
)
,
LineNumber
()
);
Assert
.
False
(
x
.
StartsWith
(
false
)
,
LineNumber
()
);
Assert
.
True
(
x
.
StartsWith
(
Encoding
.
ASCII
.
GetBytes
(
"a"
))
,
LineNumber
()
);
Assert
.
True
(
x
.
StartsWith
(
Encoding
.
ASCII
.
GetBytes
(
"ab"
))
,
LineNumber
()
);
Assert
.
True
(
x
.
StartsWith
(
Encoding
.
ASCII
.
GetBytes
(
"abc"
))
,
LineNumber
()
);
Assert
.
False
(
x
.
StartsWith
(
Encoding
.
ASCII
.
GetBytes
(
"abd"
))
,
LineNumber
()
);
Assert
.
False
(
x
.
StartsWith
(
Encoding
.
ASCII
.
GetBytes
(
"abcd"
))
,
LineNumber
()
);
x
=
10
;
// integers are effectively strings in this context
Assert
.
True
(
x
.
StartsWith
(
1
));
Assert
.
True
(
x
.
StartsWith
(
10
));
Assert
.
False
(
x
.
StartsWith
(
100
));
Assert
.
True
(
x
.
StartsWith
(
1
)
,
LineNumber
()
);
Assert
.
True
(
x
.
StartsWith
(
10
)
,
LineNumber
()
);
Assert
.
False
(
x
.
StartsWith
(
100
)
,
LineNumber
()
);
}
}
}
StackExchange.Redis/StackExchange/Redis/RedisValue.cs
View file @
80be84fc
using
System
;
using
System.Buffers
;
using
System.IO
;
using
System.Runtime.InteropServices
;
using
System.Text
;
...
...
@@ -769,6 +770,10 @@ public static RedisValue CreateFrom(MemoryStream stream)
/// </summary>
public
bool
StartsWith
(
RedisValue
value
)
{
if
(
this
.
IsNull
||
value
.
IsNull
)
return
false
;
if
(
value
.
IsNullOrEmpty
)
return
true
;
if
(
this
.
IsNullOrEmpty
)
return
false
;
ReadOnlyMemory
<
byte
>
rawThis
,
rawOther
;
var
thisType
=
this
.
Type
;
if
(
thisType
==
value
.
Type
)
// same? can often optimize
...
...
@@ -785,10 +790,50 @@ public bool StartsWith(RedisValue value)
return
rawThis
.
Span
.
StartsWith
(
rawOther
.
Span
);
}
}
rawThis
=
(
ReadOnlyMemory
<
byte
>)
this
;
rawOther
=
(
ReadOnlyMemory
<
byte
>)
value
;
return
rawThis
.
Span
.
StartsWith
(
rawOther
.
Span
);
byte
[]
arr0
=
null
,
arr1
=
null
;
try
{
rawThis
=
this
.
AsMemory
(
out
arr0
);
rawOther
=
value
.
AsMemory
(
out
arr1
);
return
rawThis
.
Span
.
StartsWith
(
rawOther
.
Span
);
}
finally
{
if
(
arr0
!=
null
)
ArrayPool
<
byte
>.
Shared
.
Return
(
arr0
);
if
(
arr1
!=
null
)
ArrayPool
<
byte
>.
Shared
.
Return
(
arr1
);
}
}
private
ReadOnlyMemory
<
byte
>
AsMemory
(
out
byte
[]
leased
)
{
switch
(
Type
)
{
case
StorageType
.
Raw
:
leased
=
null
;
return
_memory
;
case
StorageType
.
String
:
string
s
=
(
string
)
_objectOrSentinel
;
HaveString
:
if
(
s
.
Length
==
0
)
{
leased
=
null
;
return
default
;
}
leased
=
ArrayPool
<
byte
>.
Shared
.
Rent
(
Encoding
.
UTF8
.
GetByteCount
(
s
));
var
len
=
Encoding
.
UTF8
.
GetBytes
(
s
,
0
,
s
.
Length
,
leased
,
0
);
return
new
ReadOnlyMemory
<
byte
>(
leased
,
0
,
len
);
case
StorageType
.
Double
:
s
=
Format
.
ToString
(
OverlappedValueDouble
);
goto
HaveString
;
case
StorageType
.
Int64
:
leased
=
ArrayPool
<
byte
>.
Shared
.
Rent
(
PhysicalConnection
.
MaxInt64TextLen
+
2
);
// reused code has CRLF terminator
len
=
PhysicalConnection
.
WriteRaw
(
leased
,
_overlappedValue64
)
-
2
;
// drop the CRLF
return
new
ReadOnlyMemory
<
byte
>(
leased
,
0
,
len
);
}
leased
=
null
;
return
default
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment