Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
92c4fc74
R
roslyn
项目概览
lwm1986
/
roslyn
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
roslyn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
92c4fc74
编写于
8月 03, 2016
作者:
H
Heejae Chang
提交者:
GitHub
8月 03, 2016
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #12890 from heejaechang/objectreader
addressed PR feedbacks on object writer/reader
上级
e19244dd
9a92107d
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
269 addition
and
101 deletion
+269
-101
src/Compilers/Core/CodeAnalysisTest/Collections/BitArrayTests.cs
...pilers/Core/CodeAnalysisTest/Collections/BitArrayTests.cs
+60
-0
src/Compilers/Core/CodeAnalysisTest/ObjectSerializationTests.cs
...mpilers/Core/CodeAnalysisTest/ObjectSerializationTests.cs
+46
-0
src/Compilers/Core/Portable/Collections/BitVector.cs
src/Compilers/Core/Portable/Collections/BitVector.cs
+28
-10
src/Compilers/Core/Portable/Serialization/ObjectReader.cs
src/Compilers/Core/Portable/Serialization/ObjectReader.cs
+71
-48
src/Compilers/Core/Portable/Serialization/ObjectWriter.cs
src/Compilers/Core/Portable/Serialization/ObjectWriter.cs
+64
-43
未找到文件。
src/Compilers/Core/CodeAnalysisTest/Collections/BitArrayTests.cs
浏览文件 @
92c4fc74
...
...
@@ -15,6 +15,7 @@ public class BitArrayTests
private
const
int
seed
=
128129347
;
private
const
int
rounds
=
200
;
private
const
int
maxBits
=
132
;
[
Fact
]
public
void
UpperBitsUnset
()
{
...
...
@@ -149,6 +150,65 @@ public void CheckTrueBits()
}
}
[
Fact
]
public
void
CheckWords
()
{
for
(
int
capacity
=
0
;
capacity
<
maxBits
;
capacity
++)
{
BitVector
b
=
BitVector
.
Create
(
capacity
);
for
(
int
i
=
0
;
i
<
capacity
;
i
++)
{
b
[
i
]
=
false
;
}
var
required
=
BitVector
.
WordsRequired
(
capacity
);
var
count
=
BitVector
.
AllSet
(
capacity
).
Words
().
Count
();
}
}
[
Fact
]
public
void
CheckIsTrue
()
{
var
r1
=
new
Random
(
seed
);
for
(
int
capacity
=
0
;
capacity
<
maxBits
;
capacity
++)
{
BitVector
b
=
BitVector
.
Create
(
capacity
);
for
(
int
i
=
0
;
i
<
capacity
;
i
++)
{
b
[
i
]
=
r1
.
NextBool
();
}
var
index
=
0
;
foreach
(
var
word
in
b
.
Words
())
{
for
(
var
i
=
0
;
i
<
BitVector
.
BitsPerWord
;
i
++)
{
if
(
index
>=
capacity
)
{
break
;
}
Assert
.
Equal
(
b
[
index
],
BitVector
.
IsTrue
(
word
,
index
));
index
++;
}
}
}
}
[
Fact
]
public
void
CheckWordsRequired
()
{
for
(
int
capacity
=
0
;
capacity
<
maxBits
;
capacity
++)
{
var
required
=
BitVector
.
WordsRequired
(
capacity
);
var
count
=
BitVector
.
AllSet
(
capacity
).
Words
().
Count
();
Assert
.
Equal
(
count
,
required
);
}
}
private
void
CheckTrueBitsCore
(
int
capacity
,
Random
r1
,
Random
r2
)
{
BitVector
b
=
BitVector
.
Create
(
capacity
);
...
...
src/Compilers/Core/CodeAnalysisTest/ObjectSerializationTests.cs
浏览文件 @
92c4fc74
...
...
@@ -71,6 +71,52 @@ public void TestRoundTripPrimitiveArray()
reader
.
Dispose
();
}
[
Fact
]
public
void
TestRoundTripBooleanArray
()
{
for
(
var
i
=
0
;
i
<
1000
;
i
++)
{
var
inputBool
=
new
bool
[
i
];
for
(
var
j
=
0
;
j
<
i
;
j
++)
{
inputBool
[
j
]
=
j
%
2
==
0
;
}
var
stream
=
new
MemoryStream
();
var
writer
=
new
ObjectWriter
(
stream
);
writer
.
WriteValue
(
inputBool
);
writer
.
Dispose
();
stream
.
Position
=
0
;
var
reader
=
new
ObjectReader
(
stream
);
Assert
.
True
(
Enumerable
.
SequenceEqual
(
inputBool
,
(
bool
[])
reader
.
ReadValue
()));
reader
.
Dispose
();
}
}
[
Fact
]
public
void
TestRoundTripFalseBooleanArray
()
{
var
inputBool
=
Enumerable
.
Repeat
<
bool
>(
false
,
1000
).
ToArray
();
var
stream
=
new
MemoryStream
();
var
writer
=
new
ObjectWriter
(
stream
);
writer
.
WriteValue
(
inputBool
);
writer
.
Dispose
();
stream
.
Position
=
0
;
var
reader
=
new
ObjectReader
(
stream
);
Assert
.
True
(
Enumerable
.
SequenceEqual
(
inputBool
,
(
bool
[])
reader
.
ReadValue
()));
reader
.
Dispose
();
}
[
Fact
]
public
void
TestRoundTripPrimitives
()
{
...
...
src/Compilers/Core/Portable/Collections/BitVector.cs
浏览文件 @
92c4fc74
...
...
@@ -10,16 +10,17 @@ namespace Microsoft.CodeAnalysis
{
internal
struct
BitVector
:
IEquatable
<
BitVector
>
{
private
const
Word
ZeroWord
=
0
;
private
const
int
Log2BitsPerWord
=
5
;
public
const
int
BitsPerWord
=
1
<<
Log2BitsPerWord
;
// Cannot expose the following two field publicly because this structure is mutable
// and might become not null/empty, unless we restrict access to it.
private
static
readonly
Word
[]
s_emptyArray
=
SpecializedCollections
.
EmptyArray
<
Word
>();
private
static
readonly
BitVector
s_nullValue
=
new
BitVector
(
0
,
null
,
0
);
private
static
readonly
BitVector
s_emptyValue
=
new
BitVector
(
0
,
s_emptyArray
,
0
);
private
const
int
Log2BitsPerWord
=
5
;
internal
const
int
BitsPerWord
=
1
<<
Log2BitsPerWord
;
private
const
Word
ZeroWord
=
0
;
private
Word
_bits0
;
private
Word
[]
_bits
;
private
int
_capacity
;
...
...
@@ -95,14 +96,14 @@ public void EnsureCapacity(int newCapacity)
Check
();
}
internal
IEnumerable
<
Word
>
Words
()
public
IEnumerable
<
Word
>
Words
()
{
if
(
_
bits0
!=
0
)
if
(
_
capacity
>
0
)
{
yield
return
_bits0
;
}
for
(
int
i
=
0
;
i
<
_bits
.
Length
;
i
++)
for
(
int
i
=
0
;
i
<
_bits
?
.
Length
;
i
++)
{
yield
return
_bits
[
i
];
}
...
...
@@ -159,6 +160,11 @@ public static BitVector Create(int capacity)
/// <returns></returns>
public
static
BitVector
AllSet
(
int
capacity
)
{
if
(
capacity
==
0
)
{
return
Empty
;
}
int
requiredWords
=
WordsForCapacity
(
capacity
);
Word
[]
bits
=
(
requiredWords
==
0
)
?
s_emptyArray
:
new
Word
[
requiredWords
];
int
lastWord
=
requiredWords
-
1
;
...
...
@@ -298,10 +304,9 @@ public void UnionWith(BitVector other)
if
(
index
>=
_capacity
)
return
false
;
int
i
=
(
index
>>
Log2BitsPerWord
)
-
1
;
int
b
=
index
&
(
BitsPerWord
-
1
);
Word
mask
=
((
Word
)
1
)
<<
b
;
var
word
=
(
i
<
0
)
?
_bits0
:
_bits
[
i
];
return
(
word
&
mask
)
!=
0
;
return
IsTrue
(
word
,
index
);
}
set
...
...
@@ -333,5 +338,18 @@ public void Clear()
_bits0
=
0
;
if
(
_bits
!=
null
)
Array
.
Clear
(
_bits
,
0
,
_bits
.
Length
);
}
public
static
bool
IsTrue
(
Word
word
,
int
index
)
{
int
b
=
index
&
(
BitsPerWord
-
1
);
Word
mask
=
((
Word
)
1
)
<<
b
;
return
(
word
&
mask
)
!=
0
;
}
public
static
int
WordsRequired
(
int
capacity
)
{
if
(
capacity
<=
0
)
return
0
;
return
WordsForCapacity
(
capacity
)
+
1
;
}
}
}
src/Compilers/Core/Portable/Serialization/ObjectReader.cs
浏览文件 @
92c4fc74
...
...
@@ -371,70 +371,93 @@ private Array ReadPrimitiveTypeArrayElements(Type type, DataKind kind, int lengt
return
_reader
.
ReadChars
(
length
);
}
var
array
=
Array
.
CreateInstance
(
type
,
length
);
// optimizations for string where object reader/writer has its own mechanism to
// reduce duplicated strings
if
(
type
==
typeof
(
string
))
{
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
var
value
=
this
.
ReadString
();
array
.
SetValue
(
value
,
i
);
}
return
ReadPrimitiveTypeArrayElements
(
length
,
ReadString
);
}
return
array
;
if
(
type
==
typeof
(
bool
))
{
return
ReadBooleanArray
(
length
);
}
// otherwise, read elements directly from underlying binary writer
for
(
int
i
=
0
;
i
<
length
;
i
++)
switch
(
kind
)
{
case
DataKind
.
Int8
:
return
ReadPrimitiveTypeArrayElements
(
length
,
_reader
.
ReadSByte
);
case
DataKind
.
Int16
:
return
ReadPrimitiveTypeArrayElements
(
length
,
_reader
.
ReadInt16
);
case
DataKind
.
Int32
:
return
ReadPrimitiveTypeArrayElements
(
length
,
_reader
.
ReadInt32
);
case
DataKind
.
Int64
:
return
ReadPrimitiveTypeArrayElements
(
length
,
_reader
.
ReadInt64
);
case
DataKind
.
UInt16
:
return
ReadPrimitiveTypeArrayElements
(
length
,
_reader
.
ReadUInt16
);
case
DataKind
.
UInt32
:
return
ReadPrimitiveTypeArrayElements
(
length
,
_reader
.
ReadUInt32
);
case
DataKind
.
UInt64
:
return
ReadPrimitiveTypeArrayElements
(
length
,
_reader
.
ReadUInt64
);
case
DataKind
.
Float4
:
return
ReadPrimitiveTypeArrayElements
(
length
,
_reader
.
ReadSingle
);
case
DataKind
.
Float8
:
return
ReadPrimitiveTypeArrayElements
(
length
,
_reader
.
ReadDouble
);
case
DataKind
.
Decimal
:
return
ReadPrimitiveTypeArrayElements
(
length
,
_reader
.
ReadDecimal
);
default
:
throw
ExceptionUtilities
.
UnexpectedValue
(
kind
);
}
}
private
Array
ReadBooleanArray
(
int
length
)
{
if
(
length
==
0
)
{
switch
(
kind
)
// simple check
return
SpecializedCollections
.
EmptyArray
<
bool
>();
}
var
array
=
new
bool
[
length
];
var
wordLength
=
BitVector
.
WordsRequired
(
length
);
var
count
=
0
;
for
(
var
i
=
0
;
i
<
wordLength
;
i
++)
{
var
word
=
_reader
.
ReadUInt32
();
for
(
var
p
=
0
;
p
<
BitVector
.
BitsPerWord
;
p
++)
{
case
DataKind
.
Int8
:
array
.
SetValue
(
_reader
.
ReadSByte
(),
i
);
continue
;
case
DataKind
.
Int16
:
array
.
SetValue
(
_reader
.
ReadInt16
(),
i
);
continue
;
case
DataKind
.
Int32
:
array
.
SetValue
(
_reader
.
ReadInt32
(),
i
);
continue
;
case
DataKind
.
Int64
:
array
.
SetValue
(
_reader
.
ReadInt64
(),
i
);
continue
;
case
DataKind
.
UInt8
:
array
.
SetValue
(
_reader
.
ReadByte
(),
i
);
continue
;
case
DataKind
.
UInt16
:
array
.
SetValue
(
_reader
.
ReadUInt16
(),
i
);
continue
;
case
DataKind
.
UInt32
:
array
.
SetValue
(
_reader
.
ReadUInt32
(),
i
);
continue
;
case
DataKind
.
UInt64
:
array
.
SetValue
(
_reader
.
ReadUInt64
(),
i
);
continue
;
case
DataKind
.
Float4
:
array
.
SetValue
(
_reader
.
ReadSingle
(),
i
);
continue
;
case
DataKind
.
Float8
:
array
.
SetValue
(
_reader
.
ReadDouble
(),
i
);
continue
;
case
DataKind
.
Decimal
:
array
.
SetValue
(
_reader
.
ReadDecimal
(),
i
);
continue
;
case
DataKind
.
BooleanType
:
array
.
SetValue
(
_reader
.
ReadBoolean
(),
i
);
continue
;
default
:
throw
ExceptionUtilities
.
UnexpectedValue
(
kind
);
if
(
count
>=
length
)
{
return
array
;
}
array
[
count
++]
=
BitVector
.
IsTrue
(
word
,
p
);
}
}
return
array
;
}
private
T
[]
ReadPrimitiveTypeArrayElements
<
T
>(
int
length
,
Func
<
T
>
read
)
{
if
(
length
==
0
)
{
// quick check
return
SpecializedCollections
.
EmptyArray
<
T
>();
}
var
array
=
new
T
[
length
];
for
(
var
i
=
0
;
i
<
array
.
Length
;
i
++)
{
array
[
i
]
=
read
();
}
return
array
;
}
private
Type
ReadType
()
{
var
kind
=
(
DataKind
)
_reader
.
ReadByte
();
...
...
src/Compilers/Core/Portable/Serialization/ObjectWriter.cs
浏览文件 @
92c4fc74
...
...
@@ -514,55 +514,76 @@ private void WritePrimitiveTypeArrayElements(Type type, DataKind kind, Array ins
// its own optimization to reduce repeated string
if
(
type
==
typeof
(
string
))
{
foreach
(
var
value
in
instance
)
{
this
.
WriteString
((
string
)
value
);
}
WritePrimitiveTypeArrayElements
((
string
[])
instance
,
WriteString
);
return
;
}
// optimization for bool array
if
(
type
==
typeof
(
bool
))
{
WriteBooleanArray
((
bool
[])
instance
);
return
;
}
// otherwise, write elements directly to underlying binary writer
foreach
(
var
value
in
instance
)
switch
(
kind
)
{
switch
(
kind
)
{
case
DataKind
.
Int8
:
_writer
.
Write
((
sbyte
)
value
);
break
;
case
DataKind
.
Int16
:
_writer
.
Write
((
short
)
value
);
break
;
case
DataKind
.
Int32
:
_writer
.
Write
((
int
)
value
);
break
;
case
DataKind
.
Int64
:
_writer
.
Write
((
long
)
value
);
break
;
case
DataKind
.
UInt16
:
_writer
.
Write
((
ushort
)
value
);
break
;
case
DataKind
.
UInt32
:
_writer
.
Write
((
uint
)
value
);
break
;
case
DataKind
.
UInt64
:
_writer
.
Write
((
ulong
)
value
);
break
;
case
DataKind
.
Float4
:
_writer
.
Write
((
float
)
value
);
break
;
case
DataKind
.
Float8
:
_writer
.
Write
((
double
)
value
);
break
;
case
DataKind
.
Decimal
:
_writer
.
Write
((
decimal
)
value
);
break
;
case
DataKind
.
BooleanType
:
_writer
.
Write
((
bool
)
value
);
break
;
default
:
throw
ExceptionUtilities
.
UnexpectedValue
(
kind
);
}
case
DataKind
.
Int8
:
WritePrimitiveTypeArrayElements
((
sbyte
[])
instance
,
_writer
.
Write
);
return
;
case
DataKind
.
Int16
:
WritePrimitiveTypeArrayElements
((
short
[])
instance
,
_writer
.
Write
);
return
;
case
DataKind
.
Int32
:
WritePrimitiveTypeArrayElements
((
int
[])
instance
,
_writer
.
Write
);
return
;
case
DataKind
.
Int64
:
WritePrimitiveTypeArrayElements
((
long
[])
instance
,
_writer
.
Write
);
return
;
case
DataKind
.
UInt16
:
WritePrimitiveTypeArrayElements
((
ushort
[])
instance
,
_writer
.
Write
);
return
;
case
DataKind
.
UInt32
:
WritePrimitiveTypeArrayElements
((
uint
[])
instance
,
_writer
.
Write
);
return
;
case
DataKind
.
UInt64
:
WritePrimitiveTypeArrayElements
((
ulong
[])
instance
,
_writer
.
Write
);
return
;
case
DataKind
.
Float4
:
WritePrimitiveTypeArrayElements
((
float
[])
instance
,
_writer
.
Write
);
return
;
case
DataKind
.
Float8
:
WritePrimitiveTypeArrayElements
((
double
[])
instance
,
_writer
.
Write
);
return
;
case
DataKind
.
Decimal
:
WritePrimitiveTypeArrayElements
((
decimal
[])
instance
,
_writer
.
Write
);
return
;
default
:
throw
ExceptionUtilities
.
UnexpectedValue
(
kind
);
}
}
private
void
WriteBooleanArray
(
bool
[]
array
)
{
// convert bool array to bit array
var
bits
=
BitVector
.
Create
(
array
.
Length
);
for
(
var
i
=
0
;
i
<
array
.
Length
;
i
++)
{
bits
[
i
]
=
array
[
i
];
}
// send over bit array
foreach
(
var
word
in
bits
.
Words
())
{
_writer
.
Write
(
word
);
}
}
private
void
WritePrimitiveTypeArrayElements
<
T
>(
T
[]
array
,
Action
<
T
>
write
)
{
for
(
var
i
=
0
;
i
<
array
.
Length
;
i
++)
{
write
(
array
[
i
]);
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录