Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/real-logic/agrona
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpt777 committed Nov 5, 2021
2 parents 63a3082 + 0a57665 commit 3e2a901
Show file tree
Hide file tree
Showing 12 changed files with 1,044 additions and 539 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2014-2021 Real Logic Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.agrona.concurrent;

import org.agrona.ExpandableArrayBuffer;
import org.agrona.ExpandableDirectByteBuffer;
import org.openjdk.jmh.annotations.*;

import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;

/**
* Benchmark for the {@link org.agrona.MutableDirectBuffer#parseIntAscii(int, int)} method.
*/
@Fork(value = 3, jvmArgsPrepend = "-Dagrona.disable.bounds.checks=true")
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 10, time = 1)
@State(Scope.Benchmark)
public class MutableDirectBufferParseIntAsciiBenchmark
{
private static final int CAPACITY = 16;

@Param({ "-2147483648", "-1234567890", "0", "-9182", "27085146", "1999999999", "2147483647" })
private String value;
private int length;

private final UnsafeBuffer unsafeBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(CAPACITY));
private final ExpandableArrayBuffer expandableArrayBuffer = new ExpandableArrayBuffer(CAPACITY);
private final ExpandableDirectByteBuffer expandableDirectByteBuffer = new ExpandableDirectByteBuffer(CAPACITY);

/**
* Setup test data.
*/
@Setup
public void setup()
{
length = value.length();
unsafeBuffer.putStringWithoutLengthAscii(0, value);
expandableArrayBuffer.putStringWithoutLengthAscii(0, value);
expandableDirectByteBuffer.putStringWithoutLengthAscii(0, value);
}

/**
* Benchmark the {@link UnsafeBuffer#parseIntAscii(int, int)} method.
*
* @return parsed value.
*/
@Benchmark
public int unsafeBuffer()
{
return unsafeBuffer.parseIntAscii(0, length);
}

/**
* Benchmark the {@link ExpandableArrayBuffer#parseIntAscii(int, int)} method.
*
* @return parsed value.
*/
@Benchmark
public int expandableArrayBuffer()
{
return expandableArrayBuffer.parseIntAscii(0, length);
}

/**
* Benchmark the {@link ExpandableDirectByteBuffer#parseIntAscii(int, int)} method.
*
* @return parsed value.
*/
@Benchmark
public int expandableDirectByteBuffer()
{
return expandableDirectByteBuffer.parseIntAscii(0, length);
}

/**
* Benchmark the {@link Integer#parseInt(String)} method.
*
* @return parsed value.
*/
@Benchmark
public int integerParseInt()
{
return Integer.parseInt(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright 2014-2021 Real Logic Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.agrona.concurrent;

import org.agrona.ExpandableArrayBuffer;
import org.agrona.ExpandableDirectByteBuffer;
import org.openjdk.jmh.annotations.*;

import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;

/**
* Benchmark for the {@link org.agrona.MutableDirectBuffer#parseLongAscii(int, int)} method.
*/
@Fork(value = 3, jvmArgsPrepend = "-Dagrona.disable.bounds.checks=true")
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 10, time = 1)
@State(Scope.Benchmark)
public class MutableDirectBufferParseLongAsciiBenchmark
{
private static final int CAPACITY = 32;

@Param({
"-9223372036854775808",
"-1913372036854775855",
"0",
"-9182",
"27085146",
"1010101010101010",
"8999999999999999999",
"9223372036854775807" })
private String value;
private int length;

private final UnsafeBuffer unsafeBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(CAPACITY));
private final ExpandableArrayBuffer expandableArrayBuffer = new ExpandableArrayBuffer(CAPACITY);
private final ExpandableDirectByteBuffer expandableDirectByteBuffer = new ExpandableDirectByteBuffer(CAPACITY);

/**
* Setup test data.
*/
@Setup
public void setup()
{
length = value.length();
unsafeBuffer.putStringWithoutLengthAscii(0, value);
expandableArrayBuffer.putStringWithoutLengthAscii(0, value);
expandableDirectByteBuffer.putStringWithoutLengthAscii(0, value);
}

/**
* Benchmark the {@link UnsafeBuffer#parseLongAscii(int, int)} method.
*
* @return parsed value.
*/
@Benchmark
public long unsafeBuffer()
{
return unsafeBuffer.parseLongAscii(0, length);
}

/**
* Benchmark the {@link ExpandableArrayBuffer#parseLongAscii(int, int)} method.
*
* @return parsed value.
*/
@Benchmark
public long expandableArrayBuffer()
{
return expandableArrayBuffer.parseLongAscii(0, length);
}

/**
* Benchmark the {@link ExpandableDirectByteBuffer#parseLongAscii(int, int)} method.
*
* @return parsed value.
*/
@Benchmark
public long expandableDirectByteBuffer()
{
return expandableDirectByteBuffer.parseLongAscii(0, length);
}

/**
* Benchmark the {@link Long#parseLong(String)} method.
*
* @return parsed value.
*/
@Benchmark
public long longParseLong()
{
return Long.parseLong(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class MutableDirectBufferPutIntAsciiBenchmark
{
private static final int CAPACITY = 16;

@Param({ "-2147483648", "0", "-9182", "27085146", "-123456789", "2147483647" })
@Param({ "-2147483648", "-1234567890", "0", "-9182", "27085146", "1999999999", "2147483647" })
private int value;

private final UnsafeBuffer unsafeBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(CAPACITY));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ public class MutableDirectBufferPutLongAsciiBenchmark
{
private static final int CAPACITY = 32;

@Param(
{ "-9223372036854775808",
"0",
"-9182",
"123456",
"97385146",
"10101010101",
"-6180362504315475",
"9223372036854775807"
})
@Param({
"-9223372036854775808",
"-1913372036854775855",
"0",
"-9182",
"27085146",
"1010101010101010",
"8999999999999999999",
"9223372036854775807" })
private long value;

private final UnsafeBuffer unsafeBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(CAPACITY));
Expand Down
29 changes: 27 additions & 2 deletions agrona/src/main/java/org/agrona/AsciiEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,41 @@
*/
public final class AsciiEncoding
{
/**
* Maximum number of digits in a US-ASCII-encoded int.
*/
public static final int INT_MAX_DIGITS = 10;

/**
* Maximum number of digits in a US-ASCII-encoded long.
*/
public static final int LONG_MAX_DIGITS = 19;

/**
* A absolute value of the {@link Integer#MIN_VALUE} as long.
*/
public static final long INTEGER_ABSOLUTE_MIN_VALUE = Math.abs((long)Integer.MIN_VALUE);

/**
* US-ASCII-encoded byte representation of the {@link Integer#MIN_VALUE}.
*/
public static final byte[] MIN_INTEGER_VALUE = String.valueOf(Integer.MIN_VALUE).getBytes(US_ASCII);

/**
* US-ASCII-encoded byte representation of the {@link Integer#MAX_VALUE}.
*/
public static final byte[] MAX_INTEGER_VALUE = String.valueOf(Integer.MAX_VALUE).getBytes(US_ASCII);

/**
* US-ASCII-encoded byte representation of the {@link Long#MIN_VALUE}.
*/
public static final byte[] MIN_LONG_VALUE = String.valueOf(Long.MIN_VALUE).getBytes(US_ASCII);

/**
* US-ASCII-encoded byte representation of the {@link Long#MAX_VALUE}.
*/
public static final byte[] MAX_LONG_VALUE = String.valueOf(Long.MAX_VALUE).getBytes(US_ASCII);

/**
* Byte value of the minus sign ('{@code -}').
*/
Expand Down Expand Up @@ -102,8 +127,8 @@ private AsciiEncoding()
*
* @param value to find the end encoded character offset.
* @return the offset at which the encoded value will end.
* @deprecated Use {@link #digitCount(int)} instead.
* @see #digitCount(int)
* @deprecated Use {@link #digitCount(int)} instead.
*/
@Deprecated
public static int endOffset(final int value)
Expand All @@ -119,8 +144,8 @@ public static int endOffset(final int value)
*
* @param value to find the end encoded character offset.
* @return the offset at which the encoded value will end.
* @deprecated Use {@link #digitCount(long)} instead.
* @see #digitCount(long)
* @deprecated Use {@link #digitCount(long)} instead.
*/
@Deprecated
public static int endOffset(final long value)
Expand Down
Loading

0 comments on commit 3e2a901

Please sign in to comment.