Skip to content

Commit e8417b9

Browse files
committed
tmp commit
1 parent 6a15a66 commit e8417b9

9 files changed

Lines changed: 174 additions & 69 deletions

File tree

msgpack-core/pom.xml

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
55

6-
<parent>
7-
<groupId>org.msgpack</groupId>
8-
<artifactId>msgpack-root</artifactId>
9-
<version>0.7.0-SNAPSHOT</version>
10-
</parent>
6+
<parent>
7+
<groupId>org.msgpack</groupId>
8+
<artifactId>msgpack-root</artifactId>
9+
<version>0.7.0-SNAPSHOT</version>
10+
</parent>
1111

12-
<artifactId>msgpack-core</artifactId>
13-
<name>msgpack-core</name>
12+
<artifactId>msgpack-core</artifactId>
13+
<name>msgpack-core</name>
1414

15-
<dependencies>
16-
<dependency>
17-
<groupId>junit</groupId>
18-
<artifactId>junit</artifactId>
19-
<scope>test</scope>
20-
</dependency>
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<scope>test</scope>
20+
</dependency>
2121

22-
<dependency>
23-
<groupId>org.mockito</groupId>
24-
<artifactId>mockito-core</artifactId>
25-
<scope>test</scope>
26-
</dependency>
27-
</dependencies>
22+
<dependency>
23+
<groupId>org.mockito</groupId>
24+
<artifactId>mockito-core</artifactId>
25+
<scope>test</scope>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>com.google.caliper</groupId>
30+
<artifactId>caliper</artifactId>
31+
</dependency>
32+
</dependencies>
2833
</project>

msgpack-core/src/main/java/org/msgpack/core/MessagePack.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
* Created by leo on 2014/02/24.
55
*/
66
public class MessagePack {
7-
static byte[] pack(Object obj) {
7+
8+
9+
public static byte[] pack(Object obj) {
810
// TODO
911
return null;
1012
}
1113

12-
static <E> E unpack(byte[] msg) {
14+
public static <E> E unpack(byte[] msg) {
1315
// TODO
1416
return null;
1517
}
1618

17-
18-
19-
2019
}

msgpack-core/src/main/java/org/msgpack/core/buffer/Buffer.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,21 @@ public class Buffer {
2424

2525
static {
2626
try {
27-
// http://github.com/airlift/slice
28-
29-
// fetch theUnsafe object
27+
// Fetch theUnsafe object for Orackle JDK and OpenJDK
3028
Field field = Unsafe.class.getDeclaredField("theUnsafe");
3129
field.setAccessible(true);
3230
unsafe = (Unsafe) field.get(null);
3331
if (unsafe == null) {
34-
throw new RuntimeException("Unsafe access not available");
32+
throw new RuntimeException("Unsafe is unavailable");
3533
}
34+
// TODO Finding Unsafe instance for Android JVM
3635

37-
// make sure the VM thinks bytes are only one byte wide
36+
// Make sure the VM thinks bytes are only one byte wide
3837
if (sun.misc.Unsafe.ARRAY_BYTE_INDEX_SCALE != 1) {
3938
throw new IllegalStateException("Byte array index scale must be 1, but is " + ARRAY_BYTE_INDEX_SCALE);
4039
}
4140

42-
// fetch a method handle for the hidden constructor for DirectByteBuffer
41+
// Fetch a method handle for the hidden constructor for DirectByteBuffer
4342
Class<?> directByteBufferClass = ClassLoader.getSystemClassLoader().loadClass("java.nio.DirectByteBuffer");
4443
Constructor<?> constructor = directByteBufferClass.getDeclaredConstructor(long.class, int.class, Object.class);
4544
constructor.setAccessible(true);
@@ -76,9 +75,35 @@ public class Buffer {
7675

7776
private AtomicInteger referenceCounter;
7877

79-
static {
80-
// something depending on architecture to get unsafe
81-
//@Suppress...
78+
79+
static Buffer newOffHeapBuffer(int length) {
80+
long address = unsafe.allocateMemory(length);
81+
return new Buffer(address, length);
82+
}
83+
84+
public static Buffer newDirectBuffer(int length) {
85+
return new Buffer(ByteBuffer.allocateDirect(length));
86+
}
87+
88+
public static Buffer newBuffer(int length) {
89+
return new Buffer(ByteBuffer.allocate(length));
90+
}
91+
92+
public static void releaseBuffer(Buffer buffer) {
93+
if(buffer.base instanceof byte[]) {
94+
// We have nothing to do. Wait until the garbage-collector collects this array object
95+
}
96+
else {
97+
unsafe.freeMemory(buffer.address);
98+
}
99+
}
100+
101+
102+
Buffer(long address, int length) {
103+
this.base = null;
104+
this.address = address;
105+
this.size = length;
106+
this.reference = null;
82107
}
83108

84109
public Buffer(ByteBuffer bb) {
@@ -107,6 +132,8 @@ public Buffer(byte[] arr) {
107132
this.reference = null;
108133
}
109134

135+
public int size() { return size; }
136+
110137

111138
public byte getByte(int index) {
112139
return unsafe.getByte(base, address + index);

msgpack-core/src/test/java/org/msgpack/core/Person.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.msgpack.core;
22

3-
import org.msgpack.annotation.Message;
43

54
public class Person {
65
public int id;

msgpack-core/src/test/scala/org/msgpack/core/MessagePackerTest.scala

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,3 @@ object MessagePackTest {
99

1010
}
1111

12-
class MessagePackerTest extends MessagePackSpec {
13-
14-
import MessagePackTest._
15-
16-
"MessagePacker" should {
17-
18-
"serialize primitives" in {
19-
20-
forAll{ (i:Int) =>
21-
val msg = MessagePack.pack(i)
22-
val v = MessagePack.unpack[Int](msg)
23-
v shouldBe i
24-
}
25-
26-
27-
}
28-
29-
"be used with 0.6.x" in {
30-
val p = new Person(1, "leo")
31-
val v6 = new org.msgpack.MessagePack()
32-
v6.setClassLoader(classOf[Message].getClassLoader())
33-
34-
v6.register(classOf[Person])
35-
val packed = v6.write(p)
36-
37-
38-
39-
}
40-
41-
}
42-
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.msgpack.core.buffer
2+
3+
import org.msgpack.core.MessagePackSpec
4+
import java.nio.ByteBuffer
5+
import xerial.core.log.LogLevel
6+
import scala.util.Random
7+
8+
object Test {
9+
val b = ByteBuffer.allocate(10)
10+
b.getInt(0)
11+
12+
val m = Buffer.newBuffer(10)
13+
m.getInt(0)
14+
}
15+
16+
/**
17+
* Created on 2014/05/01.
18+
*/
19+
class BufferTest extends MessagePackSpec {
20+
21+
"Buffer" should {
22+
"getInt" in {
23+
24+
val N = 100000000
25+
val M = 64 * 1024 * 1024
26+
27+
val ubb = Buffer.newBuffer(M)
28+
val udb = Buffer.newDirectBuffer(M)
29+
val bb = ByteBuffer.allocate(M)
30+
val db = ByteBuffer.allocateDirect(M)
31+
32+
def bench(f: Int => Unit) {
33+
var i = 0
34+
while(i < N) {
35+
f((i * 4) % M)
36+
i += 1
37+
}
38+
}
39+
40+
val r = new Random(0)
41+
val rs = new Array[Int](N)
42+
(0 until N).map(i => rs(i) = r.nextInt(N))
43+
def randomBench(f: Int => Unit) {
44+
var i = 0
45+
while(i < N) {
46+
f((rs(i) * 4) % M)
47+
i += 1
48+
}
49+
}
50+
51+
val rep = 3
52+
info(f"Reading buffers (of size:${M}%,d) ${N}%,d x $rep times")
53+
time("sequential getInt", repeat = rep, logLevel = LogLevel.INFO) {
54+
block("unsafe array") {
55+
var i = 0
56+
while(i < N) {
57+
ubb.getInt((i * 4) % M)
58+
i += 1
59+
}
60+
61+
}
62+
63+
block("unsafe direct") {
64+
bench(udb getInt _)
65+
}
66+
67+
block("allocate") {
68+
bench(bb getInt _)
69+
}
70+
71+
block("allocateDirect") {
72+
bench(db getInt _)
73+
}
74+
}
75+
76+
time("random getInt", repeat = rep, logLevel = LogLevel.INFO) {
77+
block("unsafe array") {
78+
randomBench(ubb getInt _)
79+
}
80+
81+
block("unsafe direct") {
82+
randomBench(udb getInt _)
83+
}
84+
85+
block("allocate") {
86+
randomBench(bb getInt _)
87+
}
88+
89+
block("allocateDirect") {
90+
randomBench(db getInt _)
91+
}
92+
}
93+
94+
}
95+
96+
97+
}
98+
99+
}

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
<artifactId>mockito-core</artifactId>
6262
<version>1.9.5</version>
6363
</dependency>
64+
65+
<dependency>
66+
<groupId>com.google.caliper</groupId>
67+
<artifactId>caliper</artifactId>
68+
<version>1.0-beta-1</version>
69+
</dependency>
6470
</dependencies>
6571
</dependencyManagement>
6672

project/Build.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ object Build extends Build {
9393
"org.scalatest" % "scalatest_2.10" % "2.1.0-RC2" % "test",
9494
"org.scalacheck" % "scalacheck_2.10" % "1.11.3" % "test",
9595
"org.xerial" % "xerial-core" % "3.2.3" % "test",
96-
"org.msgpack" % "msgpack" % "0.6.9" % "test"
96+
"org.msgpack" % "msgpack" % "0.6.9" % "test",
97+
"com.novocode" % "junit-interface" % "0.10" % "test"
9798
)
9899
}
99100

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
sbt.version=0.13.2-M1
1+
sbt.version=0.13.2-M3
22

0 commit comments

Comments
 (0)