Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.projectnessie.cel.common.containers.Container;
import org.projectnessie.cel.common.types.ref.FieldType;
import org.projectnessie.cel.common.types.ref.TypeRegistry;
import org.projectnessie.cel.interpreter.AttributeFactory.Attribute;
import org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute;
Expand All @@ -56,6 +57,24 @@
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class AttributesBench {

@State(Scope.Benchmark)
public static class GeneratedScalarFieldGetterState {

final NestedMessage message;
final FieldType fieldType;

public GeneratedScalarFieldGetterState() {
message = NestedMessage.newBuilder().setBb(123).build();
TypeRegistry reg = newRegistry(message);
fieldType = reg.findFieldType("cel.expr.conformance.proto3.TestAllTypes.NestedMessage", "bb");
}
}

@Benchmark
public Object generatedScalarFieldGetter(GeneratedScalarFieldGetterState state) {
return state.fieldType.getFrom.getFrom(state.message);
}

@Benchmark
public void attributesConditionalAttr_TrueBranch() {
TypeRegistry reg = newRegistry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static org.projectnessie.cel.common.types.BoolT.True;
import static org.projectnessie.cel.common.types.Err.newTypeConversionError;
import static org.projectnessie.cel.common.types.Err.noMoreElements;
import static org.projectnessie.cel.common.types.IntT.intOf;
import static org.projectnessie.cel.common.types.TypeT.TypeType;
import static org.projectnessie.cel.common.types.Types.boolOf;
Expand Down Expand Up @@ -45,7 +44,6 @@
import org.projectnessie.cel.common.ULong;
import org.projectnessie.cel.common.types.IteratorT;
import org.projectnessie.cel.common.types.MapT;
import org.projectnessie.cel.common.types.ref.BaseVal;
import org.projectnessie.cel.common.types.ref.TypeAdapter;
import org.projectnessie.cel.common.types.ref.Val;

Expand Down Expand Up @@ -561,7 +559,7 @@ public Val convertToType(org.projectnessie.cel.common.types.ref.Type typeValue)

@Override
public IteratorT iterator() {
return new EntryKeyIterator();
return IteratorT.javaIterator(adapter, toJavaMap().keySet().iterator());
}

@Override
Expand All @@ -586,7 +584,7 @@ public Val get(Val index) {

@Override
public Val size() {
return intOf(entries.size());
return intOf(toJavaMap().size());
}

@Override
Expand All @@ -603,7 +601,8 @@ public Val find(Val key) {
}

private Val scan(Val key) {
for (Object entry : entries) {
for (int i = entries.size() - 1; i >= 0; i--) {
Object entry = entries.get(i);
Val candidate = adapter.nativeToValue(mapEntryKey(entry));
if (candidate.equal(key) == True) {
return adapter.nativeToValue(mapEntryValue(entry));
Expand All @@ -624,7 +623,7 @@ private Map<Val, Val> indexedEntries() {
for (Object entry : entries) {
Val key = adapter.nativeToValue(mapEntryKey(entry));
Val value = adapter.nativeToValue(mapEntryValue(entry));
index.putIfAbsent(key, value);
index.put(key, value);
}
indexedEntries = index;
}
Expand Down Expand Up @@ -681,48 +680,6 @@ private Object rawMapEntryValue(Object entry, int fieldNumber) {
throw new IllegalArgumentException(
String.format("Unexpected %s (%s) in list of map fields", entry.getClass(), entry));
}

private final class EntryKeyIterator extends BaseVal implements IteratorT {
private int index;

@Override
public Val hasNext() {
return boolOf(index < entries.size());
}

@Override
public Val next() {
if (index < entries.size()) {
return adapter.nativeToValue(mapEntryKey(entries.get(index++)));
}
return noMoreElements();
}

@Override
public <T> T convertToNative(Class<T> typeDesc) {
throw new UnsupportedOperationException();
}

@Override
public Val convertToType(org.projectnessie.cel.common.types.ref.Type typeValue) {
throw new UnsupportedOperationException();
}

@Override
public Val equal(Val other) {
throw new UnsupportedOperationException();
}

@Override
public org.projectnessie.cel.common.types.ref.Type type() {
throw new UnsupportedOperationException();
}

@Override
public Object value() {
throw new UnsupportedOperationException();
}
}
}

private static Object normalizeUnsignedValue(FieldDescriptor desc, Object value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (C) 2021 The Authors of CEL-Java
*
* 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
*
* http://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.projectnessie.cel.common.types.pb;

import com.google.protobuf.ByteString;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.Message;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import org.projectnessie.cel.common.types.ref.FieldGetter;

final class GeneratedFieldAccessor {

private static final MethodType OBJECT_GETTER = MethodType.methodType(Object.class, Object.class);

private GeneratedFieldAccessor() {}

static FieldGetter create(PbTypeDescription type, FieldDescription field) {
FieldDescriptor descriptor = field.descriptor();
Class<?> returnType = generatedReturnType(descriptor);
Message zero = type.zero();
Class<?> messageClass = type.reflectType();
if (returnType == null
|| descriptor.isExtension()
|| descriptor.isRepeated()
|| zero instanceof DynamicMessage
|| !messageClass.isInstance(zero)) {
return null;
}

String suffix = accessorSuffix(descriptor.getName());
for (String methodName : new String[] {"get" + suffix, "get" + suffix + '_'}) {
try {
MethodHandle getter =
MethodHandles.publicLookup()
.findVirtual(messageClass, methodName, MethodType.methodType(returnType));
if (!matchesField(getter, zero, descriptor)) {
continue;
}
MethodHandle objectGetter = getter.asType(OBJECT_GETTER);
return target -> invoke(objectGetter, target);
} catch (IllegalAccessException | NoSuchMethodException | RuntimeException e) {
// Generated-name collisions and inaccessible generated classes use descriptor access.
}
}
return null;
}

private static Class<?> generatedReturnType(FieldDescriptor descriptor) {
return switch (descriptor.getType()) {
case BOOL -> boolean.class;
case STRING -> String.class;
case BYTES -> ByteString.class;
case INT32, SINT32, SFIXED32 -> int.class;
case INT64, SINT64, SFIXED64 -> long.class;
case FLOAT -> float.class;
case DOUBLE -> double.class;
default -> null;
};
}

private static boolean matchesField(
MethodHandle getter, Message zero, FieldDescriptor descriptor) {
Object expected = validationValue(descriptor);
Message probe = zero.newBuilderForType().setField(descriptor, expected).buildPartial();
try {
return expected.equals(getter.invoke(probe));
} catch (Throwable e) {
return false;
}
}

private static Object validationValue(FieldDescriptor descriptor) {
return switch (descriptor.getType()) {
case BOOL -> true;
case STRING -> "cel_generated_field_accessor";
case BYTES -> ByteString.copyFromUtf8("cel_generated_field_accessor");
case INT32, SINT32, SFIXED32 -> 0x51a7;
case INT64, SINT64, SFIXED64 -> 0x51a7_19b3_42c5L;
case FLOAT -> 123.25f;
case DOUBLE -> 123.25d;
default ->
throw new IllegalArgumentException("Unsupported field type " + descriptor.getType());
};
}

private static String accessorSuffix(String fieldName) {
StringBuilder result = new StringBuilder(fieldName.length());
boolean capitalizeNext = true;
for (int i = 0; i < fieldName.length(); i++) {
char c = fieldName.charAt(i);
if (c >= 'a' && c <= 'z') {
result.append(capitalizeNext ? Character.toUpperCase(c) : c);
capitalizeNext = false;
} else if ((c >= 'A' && c <= 'Z')) {
result.append(c);
capitalizeNext = false;
} else if (c >= '0' && c <= '9') {
result.append(c);
capitalizeNext = true;
} else {
capitalizeNext = true;
}
}
return result.toString();
}

private static Object invoke(MethodHandle getter, Object target) {
try {
return (Object) getter.invokeExact(target);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
}
Loading
Loading