Skip to content

Commit

Permalink
Optimizes IonValue.getType() to avoid vtable/itable lookups when invo…
Browse files Browse the repository at this point in the history
…ked on IonStruct values.
  • Loading branch information
tgregg committed Aug 29, 2024
1 parent ea306eb commit fa12d30
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 250 deletions.
45 changes: 21 additions & 24 deletions src/jmh/java/com/amazon/ion/IonValueGetTypeBenchmark.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion;


import com.amazon.ion.system.IonSystemBuilder;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
Expand All @@ -16,11 +17,7 @@
import org.openjdk.jmh.infra.Blackhole;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
Expand All @@ -31,24 +28,24 @@
@State(Scope.Benchmark)
public class IonValueGetTypeBenchmark {

private static final IonSystem system = IonSystemBuilder.standard().build();
private static final IonSystem SYSTEM = IonSystemBuilder.standard().build();

private final IonBool bool = system.newBool(false);
private final IonInt integer = system.newInt(123);
private final IonNull ionNull = system.newNull();
private final IonFloat ionFloat = system.newFloat(42);
private final IonDecimal ionDecimal = system.newDecimal(1.23);
private final IonString string = system.newString("abc");
private final IonSymbol symbol = system.newSymbol("def");
private final IonBlob blob = system.newBlob(new byte[]{});
private final IonClob clob = system.newClob(new byte[]{});
private final IonStruct struct = system.newEmptyStruct();
private final IonList list = system.newEmptyList();
private final IonSexp sexp = system.newEmptySexp();
private final IonDatagram dg = system.newDatagram();
private final IonBool bool = SYSTEM.newBool(false);
private final IonInt integer = SYSTEM.newInt(123);
private final IonNull ionNull = SYSTEM.newNull();
private final IonFloat ionFloat = SYSTEM.newFloat(42);
private final IonDecimal ionDecimal = SYSTEM.newDecimal(1.23);
private final IonString string = SYSTEM.newString("abc");
private final IonSymbol symbol = SYSTEM.newSymbol("def");
private final IonBlob blob = SYSTEM.newBlob(new byte[]{});
private final IonClob clob = SYSTEM.newClob(new byte[]{});
private final IonStruct struct = SYSTEM.newEmptyStruct();
private final IonList list = SYSTEM.newEmptyList();
private final IonSexp sexp = SYSTEM.newEmptySexp();
private final IonDatagram dg = SYSTEM.newDatagram();

@Benchmark
public int testAddSuffixViaInstanceVariablePlus() {
public int ionValueGetType() {
return integer.getType().ordinal() +
bool.getType().ordinal() +
ionNull.getType().ordinal() +
Expand All @@ -64,12 +61,12 @@ public int testAddSuffixViaInstanceVariablePlus() {
dg.getType().ordinal();
}

private final IonDatagram realWorld;
private final IonDatagram container;
private Blackhole bh;

public IonValueGetTypeBenchmark() {
try {
realWorld = system.getLoader().load(Paths.get("/Users/greggt/Documents/StructuredLogging/kinesis/service_log_legacy.ion").toFile());
container = SYSTEM.getLoader().load(Paths.get("ion-tests/iontestdata/good/message2.ion").toFile());
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -81,8 +78,8 @@ public void setup(Blackhole bh) {
}

@Benchmark
public void getTypeReadWorld() {
getTypeRecursive(realWorld);
public void getTypeContainer() {
getTypeRecursive(container);
}

public void getTypeRecursive(IonContainer container) {
Expand Down
19 changes: 3 additions & 16 deletions src/main/java/com/amazon/ion/impl/lite/IonBlobLite.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.impl.lite;

import com.amazon.ion.IonBlob;
Expand Down Expand Up @@ -67,7 +54,7 @@ int scalarHashCode() {
}

@Override
public IonType getType()
public IonType getTypeSlow()
{
return IonType.BLOB;
}
Expand Down
19 changes: 3 additions & 16 deletions src/main/java/com/amazon/ion/impl/lite/IonBoolLite.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.impl.lite;

import com.amazon.ion.IonBool;
Expand Down Expand Up @@ -68,7 +55,7 @@ public IonBoolLite clone()
}

@Override
public IonType getType()
public IonType getTypeSlow()
{
return IonType.BOOL;
}
Expand Down
19 changes: 3 additions & 16 deletions src/main/java/com/amazon/ion/impl/lite/IonClobLite.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.impl.lite;

import com.amazon.ion.IonClob;
Expand Down Expand Up @@ -70,7 +57,7 @@ int scalarHashCode() {
}

@Override
public IonType getType()
public IonType getTypeSlow()
{
return IonType.CLOB;
}
Expand Down
19 changes: 3 additions & 16 deletions src/main/java/com/amazon/ion/impl/lite/IonDatagramLite.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.impl.lite;

import static com.amazon.ion.SystemSymbols.ION_1_0;
Expand Down Expand Up @@ -386,7 +373,7 @@ public IonSystemLite getSystem()
}

@Override
public IonType getType()
public IonType getTypeSlow()
{
return IonType.DATAGRAM;
}
Expand Down
19 changes: 3 additions & 16 deletions src/main/java/com/amazon/ion/impl/lite/IonDecimalLite.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.impl.lite;

import com.amazon.ion.Decimal;
Expand Down Expand Up @@ -105,7 +92,7 @@ int scalarHashCode()
}

@Override
public IonType getType()
public IonType getTypeSlow()
{
return IonType.DECIMAL;
}
Expand Down
19 changes: 3 additions & 16 deletions src/main/java/com/amazon/ion/impl/lite/IonFloatLite.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.impl.lite;

import com.amazon.ion.Decimal;
Expand Down Expand Up @@ -76,7 +63,7 @@ int scalarHashCode()
}

@Override
public IonType getType()
public IonType getTypeSlow()
{
return IonType.FLOAT;
}
Expand Down
19 changes: 3 additions & 16 deletions src/main/java/com/amazon/ion/impl/lite/IonIntLite.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.impl.lite;

import com.amazon.ion.IntegerSize;
Expand Down Expand Up @@ -107,7 +94,7 @@ int scalarHashCode()
}

@Override
public IonType getType()
public IonType getTypeSlow()
{
return IonType.INT;
}
Expand Down
19 changes: 3 additions & 16 deletions src/main/java/com/amazon/ion/impl/lite/IonListLite.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.impl.lite;

import com.amazon.ion.ContainedValueException;
Expand Down Expand Up @@ -81,7 +68,7 @@ int hashSignature() {
}

@Override
public IonType getType()
public IonType getTypeSlow()
{
return IonType.LIST;
}
Expand Down
19 changes: 3 additions & 16 deletions src/main/java/com/amazon/ion/impl/lite/IonNullLite.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.impl.lite;

import com.amazon.ion.IonNull;
Expand Down Expand Up @@ -58,7 +45,7 @@ public IonNullLite clone()
}

@Override
public IonType getType()
public IonType getTypeSlow()
{
return IonType.NULL;
}
Expand Down
19 changes: 3 additions & 16 deletions src/main/java/com/amazon/ion/impl/lite/IonSexpLite.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.impl.lite;

import com.amazon.ion.ContainedValueException;
Expand Down Expand Up @@ -75,7 +62,7 @@ int hashSignature() {
}

@Override
public IonType getType()
public IonType getTypeSlow()
{
return IonType.SEXP;
}
Expand Down
Loading

0 comments on commit fa12d30

Please sign in to comment.