Skip to content

Commit

Permalink
Optimize codec registry hot path
Browse files Browse the repository at this point in the history
- test UDT and tuple after primitives in codecFor(Object)
- use instanceof in canEncode(Object) where possible
- use Class#isAssignableFrom or `==` in canEncode(Class) where possible
  • Loading branch information
olim7t committed Apr 25, 2017
1 parent a9c4c03 commit c05e014
Show file tree
Hide file tree
Showing 23 changed files with 219 additions and 16 deletions.
Expand Up @@ -49,10 +49,15 @@ default boolean canEncode(GenericType<?> javaType) {
*
* <p>The default implementation wraps the class in a generic type and calls {@link
* #canEncode(GenericType)}, therefore it is invariant as well.
*
* <p>Implementors are encouraged to override this method if there is a more efficient way. In
* particular, if the codec targets a non-generic class, the check can be done with {@code
* Class#isAssignableFrom}. Or even better, with {@code ==} if that class also happens to be
* final.
*/
default boolean canEncode(Class<?> javaType) {
Preconditions.checkNotNull(javaType);
return canEncode(GenericType.of(javaType));
default boolean canEncode(Class<?> javaClass) {
Preconditions.checkNotNull(javaClass);
return canEncode(GenericType.of(javaClass));
}

/**
Expand All @@ -72,10 +77,13 @@ default boolean canEncode(Class<?> javaType) {
* <p>Similarly, codecs that only accept a partial subset of all possible values must override
* this method and manually inspect the object to check if it complies or not with the codec's
* limitations.
*
* <p>Finally, if the codec targets a non-generic Java class, it might be possible to implement
* this method with a simple {@code instanceof} check.
*/
default boolean canEncode(Object object) {
Preconditions.checkNotNull(object);
return getJavaType().__getToken().isSupertypeOf(TypeToken.of(object.getClass()));
default boolean canEncode(Object value) {
Preconditions.checkNotNull(value);
return getJavaType().__getToken().isSupertypeOf(TypeToken.of(value.getClass()));
}

/** Whether this codec is capable of decoding the given CQL type. */
Expand Down
Expand Up @@ -33,6 +33,16 @@ public DataType getCqlType() {
return DataTypes.BIGINT;
}

@Override
public boolean canEncode(Object value) {
return value instanceof Long;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return javaClass == Long.class;
}

@Override
public ByteBuffer encodePrimitive(long value, ProtocolVersion protocolVersion) {
ByteBuffer bytes = ByteBuffer.allocate(8);
Expand Down
Expand Up @@ -34,6 +34,16 @@ public DataType getCqlType() {
return DataTypes.BLOB;
}

@Override
public boolean canEncode(Object value) {
return value instanceof ByteBuffer;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return ByteBuffer.class.isAssignableFrom(javaClass);
}

@Override
public ByteBuffer encode(ByteBuffer value, ProtocolVersion protocolVersion) {
return (value == null) ? null : value.duplicate();
Expand Down
Expand Up @@ -37,6 +37,16 @@ public DataType getCqlType() {
return DataTypes.BOOLEAN;
}

@Override
public boolean canEncode(Object value) {
return value instanceof Boolean;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return javaClass == Boolean.class;
}

@Override
public ByteBuffer encodePrimitive(boolean value, ProtocolVersion protocolVersion) {
return value ? TRUE.duplicate() : FALSE.duplicate();
Expand Down
Expand Up @@ -40,6 +40,16 @@ public DataType getCqlType() {
return DataTypes.DURATION;
}

@Override
public boolean canEncode(Object value) {
return value instanceof CqlDuration;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return javaClass == CqlDuration.class;
}

@Override
public ByteBuffer encode(CqlDuration value, ProtocolVersion protocolVersion) {
if (value == null) {
Expand Down
Expand Up @@ -41,6 +41,16 @@ public DataType getCqlType() {
return cqlType;
}

@Override
public boolean canEncode(Object value) {
return value instanceof ByteBuffer;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return ByteBuffer.class.isAssignableFrom(javaClass);
}

@Override
public ByteBuffer encode(ByteBuffer value, ProtocolVersion protocolVersion) {
return (value == null) ? null : value.duplicate();
Expand Down
Expand Up @@ -43,6 +43,16 @@ public DataType getCqlType() {
return DataTypes.DATE;
}

@Override
public boolean canEncode(Object value) {
return value instanceof LocalDate;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return javaClass == LocalDate.class;
}

@Override
public ByteBuffer encode(LocalDate value, ProtocolVersion protocolVersion) {
if (value == null) {
Expand Down
Expand Up @@ -35,6 +35,16 @@ public DataType getCqlType() {
return DataTypes.DECIMAL;
}

@Override
public boolean canEncode(Object value) {
return value instanceof BigDecimal;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return BigDecimal.class.isAssignableFrom(javaClass);
}

@Override
public ByteBuffer encode(BigDecimal value, ProtocolVersion protocolVersion) {
if (value == null) {
Expand Down
Expand Up @@ -33,6 +33,16 @@ public DataType getCqlType() {
return DataTypes.DOUBLE;
}

@Override
public boolean canEncode(Object value) {
return value instanceof Double;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return javaClass == Double.class;
}

@Override
public ByteBuffer encodePrimitive(double value, ProtocolVersion protocolVersion) {
ByteBuffer bytes = ByteBuffer.allocate(8);
Expand Down
Expand Up @@ -33,6 +33,16 @@ public DataType getCqlType() {
return DataTypes.FLOAT;
}

@Override
public boolean canEncode(Object value) {
return value instanceof Float;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return javaClass == Float.class;
}

@Override
public ByteBuffer encodePrimitive(float value, ProtocolVersion protocolVersion) {
ByteBuffer bytes = ByteBuffer.allocate(4);
Expand Down
Expand Up @@ -37,6 +37,16 @@ public DataType getCqlType() {
return DataTypes.INET;
}

@Override
public boolean canEncode(Object value) {
return value instanceof InetAddress;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return InetAddress.class.isAssignableFrom(javaClass);
}

@Override
public ByteBuffer encode(InetAddress value, ProtocolVersion protocolVersion) {
return (value == null) ? null : ByteBuffer.wrap(value.getAddress());
Expand Down
Expand Up @@ -34,6 +34,16 @@ public DataType getCqlType() {
return DataTypes.INT;
}

@Override
public boolean canEncode(Object value) {
return value instanceof Integer;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return javaClass == Integer.class;
}

@Override
public ByteBuffer encodePrimitive(int value, ProtocolVersion protocolVersion) {
ByteBuffer bytes = ByteBuffer.allocate(4);
Expand Down
Expand Up @@ -33,6 +33,16 @@ public DataType getCqlType() {
return DataTypes.SMALLINT;
}

@Override
public boolean canEncode(Object value) {
return value instanceof Short;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return javaClass == Short.class;
}

@Override
public ByteBuffer encodePrimitive(short value, ProtocolVersion protocolVersion) {
ByteBuffer bytes = ByteBuffer.allocate(2);
Expand Down
Expand Up @@ -44,6 +44,16 @@ public DataType getCqlType() {
return cqlType;
}

@Override
public boolean canEncode(Object value) {
return value instanceof String;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return javaClass == String.class;
}

@Override
public ByteBuffer encode(String value, ProtocolVersion protocolVersion) {
return (value == null) ? null : ByteBuffer.wrap(value.getBytes(charset));
Expand Down
Expand Up @@ -40,6 +40,16 @@ public DataType getCqlType() {
return DataTypes.TIME;
}

@Override
public boolean canEncode(Object value) {
return value instanceof LocalTime;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return javaClass == LocalTime.class;
}

@Override
public ByteBuffer encode(LocalTime value, ProtocolVersion protocolVersion) {
return (value == null)
Expand Down
Expand Up @@ -18,6 +18,7 @@
import com.datastax.oss.driver.api.core.ProtocolVersion;
import com.datastax.oss.driver.api.type.DataType;
import com.datastax.oss.driver.api.type.DataTypes;
import com.datastax.oss.driver.api.type.reflect.GenericType;
import java.nio.ByteBuffer;
import java.util.UUID;

Expand All @@ -29,7 +30,12 @@ public DataType getCqlType() {

@Override
public boolean canEncode(Object value) {
return super.canEncode(value) && ((UUID) value).version() == 1;
return value instanceof UUID && ((UUID) value).version() == 1;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return javaClass == UUID.class;
}

@Override
Expand Down
Expand Up @@ -71,6 +71,16 @@ public DataType getCqlType() {
return DataTypes.TIMESTAMP;
}

@Override
public boolean canEncode(Object value) {
return value instanceof Instant;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return javaClass == Instant.class;
}

@Override
public ByteBuffer encode(Instant value, ProtocolVersion protocolVersion) {
return (value == null)
Expand Down
Expand Up @@ -33,6 +33,16 @@ public DataType getCqlType() {
return DataTypes.TINYINT;
}

@Override
public boolean canEncode(Object value) {
return value instanceof Byte;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return javaClass == Byte.class;
}

@Override
public ByteBuffer encodePrimitive(byte value, ProtocolVersion protocolVersion) {
ByteBuffer bytes = ByteBuffer.allocate(1);
Expand Down
Expand Up @@ -48,6 +48,11 @@ public boolean canEncode(Object value) {
return (value instanceof TupleValue) && ((TupleValue) value).getType().equals(cqlType);
}

@Override
public boolean canEncode(Class<?> javaClass) {
return TupleValue.class.isAssignableFrom(javaClass);
}

@Override
public ByteBuffer encode(TupleValue value, ProtocolVersion protocolVersion) {
if (value == null) {
Expand Down
Expand Up @@ -46,8 +46,12 @@ public DataType getCqlType() {

@Override
public boolean canEncode(Object value) {
return UdtValue.class.isAssignableFrom(value.getClass())
&& ((UdtValue) value).getType().equals(cqlType);
return value instanceof UdtValue && ((UdtValue) value).getType().equals(cqlType);
}

@Override
public boolean canEncode(Class<?> javaClass) {
return UdtValue.class.isAssignableFrom(javaClass);
}

@Override
Expand Down
Expand Up @@ -34,6 +34,16 @@ public DataType getCqlType() {
return DataTypes.UUID;
}

@Override
public boolean canEncode(Object value) {
return value instanceof UUID;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return javaClass == UUID.class;
}

@Override
public ByteBuffer encode(UUID value, ProtocolVersion protocolVersion) {
if (value == null) {
Expand Down
Expand Up @@ -35,6 +35,16 @@ public DataType getCqlType() {
return DataTypes.VARINT;
}

@Override
public boolean canEncode(Object value) {
return value instanceof BigInteger;
}

@Override
public boolean canEncode(Class<?> javaClass) {
return BigInteger.class.isAssignableFrom(javaClass);
}

@Override
public ByteBuffer encode(BigInteger value, ProtocolVersion protocolVersion) {
return (value == null) ? null : ByteBuffer.wrap(value.toByteArray());
Expand Down

0 comments on commit c05e014

Please sign in to comment.