Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce object allocation with MessageSerializer. (#293)
* Return byte array from MessageSerializer. * Pool Kryo Input and Output objects. * Fix caching. * Use base class. * Simplify deserialize. * Add test.
- Loading branch information
1 parent
e3a2a37
commit 55a4a5d
Showing
12 changed files
with
287 additions
and
88 deletions.
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
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
73 changes: 73 additions & 0 deletions
73
actors/runtime/src/main/java/cloud/orbit/actors/runtime/KryoIOPool.java
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,73 @@ | ||
/* | ||
Copyright (C) 2018 Electronic Arts Inc. All rights reserved. | ||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions | ||
are met: | ||
1. Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the distribution. | ||
3. Neither the name of Electronic Arts, Inc. ("EA") nor the names of | ||
its contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY ELECTRONIC ARTS AND ITS CONTRIBUTORS "AS IS" AND ANY | ||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL ELECTRONIC ARTS OR ITS CONTRIBUTORS BE LIABLE FOR ANY | ||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package cloud.orbit.actors.runtime; | ||
|
||
import java.lang.ref.SoftReference; | ||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import java.util.function.Function; | ||
|
||
abstract class KryoIOPool<T> | ||
{ | ||
|
||
private final Queue<SoftReference<T>> queue = new ConcurrentLinkedQueue<>(); | ||
|
||
private T borrow(final int bufferSize) | ||
{ | ||
T element; | ||
SoftReference<T> reference; | ||
while ((reference = queue.poll()) != null) | ||
{ | ||
if ((element = reference.get()) != null) | ||
{ | ||
return element; | ||
} | ||
} | ||
return create(bufferSize); | ||
} | ||
|
||
protected abstract T create(final int bufferSize); | ||
|
||
protected abstract boolean recycle(final T element); | ||
|
||
<R> R run(final Function<T, R> function, final int bufferSize) | ||
{ | ||
final T element = borrow(bufferSize); | ||
try | ||
{ | ||
return function.apply(element); | ||
} | ||
finally | ||
{ | ||
if (recycle(element)) | ||
{ | ||
queue.offer(new SoftReference<>(element)); | ||
} | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
actors/runtime/src/main/java/cloud/orbit/actors/runtime/KryoOutputPool.java
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,54 @@ | ||
/* | ||
Copyright (C) 2018 Electronic Arts Inc. All rights reserved. | ||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions | ||
are met: | ||
1. Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the distribution. | ||
3. Neither the name of Electronic Arts, Inc. ("EA") nor the names of | ||
its contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY ELECTRONIC ARTS AND ITS CONTRIBUTORS "AS IS" AND ANY | ||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL ELECTRONIC ARTS OR ITS CONTRIBUTORS BE LIABLE FOR ANY | ||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package cloud.orbit.actors.runtime; | ||
|
||
import com.esotericsoftware.kryo.io.Output; | ||
|
||
class KryoOutputPool extends KryoIOPool<Output> | ||
{ | ||
|
||
private static final int MAX_BUFFER_SIZE = 768 * 1024; | ||
static final int MAX_POOLED_BUFFER_SIZE = 512 * 1024; | ||
|
||
@Override | ||
protected Output create(int bufferSize) | ||
{ | ||
return new Output(bufferSize, MAX_BUFFER_SIZE); | ||
} | ||
|
||
@Override | ||
protected boolean recycle(Output output) | ||
{ | ||
if (output.getBuffer().length < MAX_POOLED_BUFFER_SIZE) | ||
{ | ||
output.clear(); | ||
return true; | ||
} | ||
return false; // discard | ||
} | ||
} |
Oops, something went wrong.