From fa12d308c6ad20b686b7f18e0fa03d9e3d571d2c Mon Sep 17 00:00:00 2001 From: Tyler Gregg Date: Tue, 13 Aug 2024 14:42:57 -0700 Subject: [PATCH] Optimizes IonValue.getType() to avoid vtable/itable lookups when invoked on IonStruct values. --- .../amazon/ion/IonValueGetTypeBenchmark.java | 45 +++++++++---------- .../com/amazon/ion/impl/lite/IonBlobLite.java | 19 ++------ .../com/amazon/ion/impl/lite/IonBoolLite.java | 19 ++------ .../com/amazon/ion/impl/lite/IonClobLite.java | 19 ++------ .../amazon/ion/impl/lite/IonDatagramLite.java | 19 ++------ .../amazon/ion/impl/lite/IonDecimalLite.java | 19 ++------ .../amazon/ion/impl/lite/IonFloatLite.java | 19 ++------ .../com/amazon/ion/impl/lite/IonIntLite.java | 19 ++------ .../com/amazon/ion/impl/lite/IonListLite.java | 19 ++------ .../com/amazon/ion/impl/lite/IonNullLite.java | 19 ++------ .../com/amazon/ion/impl/lite/IonSexpLite.java | 19 ++------ .../amazon/ion/impl/lite/IonStringLite.java | 19 ++------ .../amazon/ion/impl/lite/IonStructLite.java | 19 ++------ .../amazon/ion/impl/lite/IonSymbolLite.java | 19 ++------ .../ion/impl/lite/IonTimestampLite.java | 2 +- .../amazon/ion/impl/lite/IonValueLite.java | 37 ++++++++------- 16 files changed, 81 insertions(+), 250 deletions(-) diff --git a/src/jmh/java/com/amazon/ion/IonValueGetTypeBenchmark.java b/src/jmh/java/com/amazon/ion/IonValueGetTypeBenchmark.java index 156a5a771a..518c0b9f01 100644 --- a/src/jmh/java/com/amazon/ion/IonValueGetTypeBenchmark.java +++ b/src/jmh/java/com/amazon/ion/IonValueGetTypeBenchmark.java @@ -1,3 +1,5 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 package com.amazon.ion; @@ -5,7 +7,6 @@ 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; @@ -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) @@ -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() + @@ -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); } @@ -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) { diff --git a/src/main/java/com/amazon/ion/impl/lite/IonBlobLite.java b/src/main/java/com/amazon/ion/impl/lite/IonBlobLite.java index baffccb7ef..bcab70da3c 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonBlobLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonBlobLite.java @@ -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; @@ -67,7 +54,7 @@ int scalarHashCode() { } @Override - public IonType getType() + public IonType getTypeSlow() { return IonType.BLOB; } diff --git a/src/main/java/com/amazon/ion/impl/lite/IonBoolLite.java b/src/main/java/com/amazon/ion/impl/lite/IonBoolLite.java index 2ceab4391e..2429a72f79 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonBoolLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonBoolLite.java @@ -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; @@ -68,7 +55,7 @@ public IonBoolLite clone() } @Override - public IonType getType() + public IonType getTypeSlow() { return IonType.BOOL; } diff --git a/src/main/java/com/amazon/ion/impl/lite/IonClobLite.java b/src/main/java/com/amazon/ion/impl/lite/IonClobLite.java index aa55dd7735..702d29f88b 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonClobLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonClobLite.java @@ -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; @@ -70,7 +57,7 @@ int scalarHashCode() { } @Override - public IonType getType() + public IonType getTypeSlow() { return IonType.CLOB; } diff --git a/src/main/java/com/amazon/ion/impl/lite/IonDatagramLite.java b/src/main/java/com/amazon/ion/impl/lite/IonDatagramLite.java index 4f37ec801c..6edf35a668 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonDatagramLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonDatagramLite.java @@ -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; @@ -386,7 +373,7 @@ public IonSystemLite getSystem() } @Override - public IonType getType() + public IonType getTypeSlow() { return IonType.DATAGRAM; } diff --git a/src/main/java/com/amazon/ion/impl/lite/IonDecimalLite.java b/src/main/java/com/amazon/ion/impl/lite/IonDecimalLite.java index 395c016a5a..0358a0f069 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonDecimalLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonDecimalLite.java @@ -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; @@ -105,7 +92,7 @@ int scalarHashCode() } @Override - public IonType getType() + public IonType getTypeSlow() { return IonType.DECIMAL; } diff --git a/src/main/java/com/amazon/ion/impl/lite/IonFloatLite.java b/src/main/java/com/amazon/ion/impl/lite/IonFloatLite.java index 829a0c3a67..61a98b97ff 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonFloatLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonFloatLite.java @@ -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; @@ -76,7 +63,7 @@ int scalarHashCode() } @Override - public IonType getType() + public IonType getTypeSlow() { return IonType.FLOAT; } diff --git a/src/main/java/com/amazon/ion/impl/lite/IonIntLite.java b/src/main/java/com/amazon/ion/impl/lite/IonIntLite.java index 37b290074d..ec3cdc93de 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonIntLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonIntLite.java @@ -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; @@ -107,7 +94,7 @@ int scalarHashCode() } @Override - public IonType getType() + public IonType getTypeSlow() { return IonType.INT; } diff --git a/src/main/java/com/amazon/ion/impl/lite/IonListLite.java b/src/main/java/com/amazon/ion/impl/lite/IonListLite.java index 0a1dc088a7..c14b64474e 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonListLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonListLite.java @@ -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; @@ -81,7 +68,7 @@ int hashSignature() { } @Override - public IonType getType() + public IonType getTypeSlow() { return IonType.LIST; } diff --git a/src/main/java/com/amazon/ion/impl/lite/IonNullLite.java b/src/main/java/com/amazon/ion/impl/lite/IonNullLite.java index bb959b4f78..0d92116696 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonNullLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonNullLite.java @@ -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; @@ -58,7 +45,7 @@ public IonNullLite clone() } @Override - public IonType getType() + public IonType getTypeSlow() { return IonType.NULL; } diff --git a/src/main/java/com/amazon/ion/impl/lite/IonSexpLite.java b/src/main/java/com/amazon/ion/impl/lite/IonSexpLite.java index e9d61233a5..4da7c77d33 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonSexpLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonSexpLite.java @@ -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; @@ -75,7 +62,7 @@ int hashSignature() { } @Override - public IonType getType() + public IonType getTypeSlow() { return IonType.SEXP; } diff --git a/src/main/java/com/amazon/ion/impl/lite/IonStringLite.java b/src/main/java/com/amazon/ion/impl/lite/IonStringLite.java index 99d3d227a1..199eb18726 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonStringLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonStringLite.java @@ -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.IonString; @@ -70,7 +57,7 @@ int scalarHashCode() } @Override - public IonType getType() + public IonType getTypeSlow() { return IonType.STRING; } diff --git a/src/main/java/com/amazon/ion/impl/lite/IonStructLite.java b/src/main/java/com/amazon/ion/impl/lite/IonStructLite.java index 2bd4f1715a..f72b734344 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonStructLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonStructLite.java @@ -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; @@ -345,7 +332,7 @@ else if (_children == null) { @Override - public IonType getType() + public IonType getTypeSlow() { return IonType.STRUCT; } diff --git a/src/main/java/com/amazon/ion/impl/lite/IonSymbolLite.java b/src/main/java/com/amazon/ion/impl/lite/IonSymbolLite.java index 15bbd3e68b..b9d38554e2 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonSymbolLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonSymbolLite.java @@ -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.SymbolTable.UNKNOWN_SYMBOL_ID; @@ -124,7 +111,7 @@ int scalarHashCode() } @Override - public IonType getType() + public IonType getTypeSlow() { return IonType.SYMBOL; } diff --git a/src/main/java/com/amazon/ion/impl/lite/IonTimestampLite.java b/src/main/java/com/amazon/ion/impl/lite/IonTimestampLite.java index 2eb8b4d791..b6c286e462 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonTimestampLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonTimestampLite.java @@ -64,7 +64,7 @@ int scalarHashCode() { } @Override - public IonType getType() + public IonType getTypeSlow() { return IonType.TIMESTAMP; } diff --git a/src/main/java/com/amazon/ion/impl/lite/IonValueLite.java b/src/main/java/com/amazon/ion/impl/lite/IonValueLite.java index e80b54833c..08fd6661fa 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonValueLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonValueLite.java @@ -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.SymbolTable.UNKNOWN_SYMBOL_ID; @@ -842,11 +829,27 @@ public IonSystemLite getSystem() return _context.getSystem(); } - public IonType getType() + public final IonType getType() { - throw new UnsupportedOperationException("this type "+this.getClass().getSimpleName()+" should not be instantiated, there is not IonType associated with it"); + Class clazz = this.getClass(); + // IonStructLite is chosen for the fast path because it is the most commonly used container type as well as + // one of the most common of all types. This is important because internal usages of IonValue.getType() occur + // on container values much more often than on scalars. Do not remove or add special case branches here without + // a thorough study of the performance implications. + if (clazz == IonStructLite.class) { + return IonType.STRUCT; + } + return getTypeSlow(); } + /** + * @see #getType() + * This variant is considered "slow" because it is an abstract method with 14 implementations, meaning that it + * is invoked using a vtable lookup. + * @return the value's type. + */ + abstract IonType getTypeSlow(); + public SymbolToken[] getTypeAnnotationSymbols() { return getTypeAnnotationSymbols(new LazySymbolTableProvider(this));