Skip to content

JAVA-1308: CodecRegistry performance improvements#757

Closed
gnoremac wants to merge 1 commit into
apache:3.xfrom
gnoremac:JAVA-1308
Closed

JAVA-1308: CodecRegistry performance improvements#757
gnoremac wants to merge 1 commit into
apache:3.xfrom
gnoremac:JAVA-1308

Conversation

@gnoremac

Copy link
Copy Markdown
  • Separate the built-in codecs cache so it can be backed by a static Map instead of Guava Cache

@datastax-bot

Copy link
Copy Markdown

Hi @gnoremac, thanks for your contribution!

In order for us to evaluate and accept your PR, we ask that you sign a contribution license agreement. It's all electronic and will take just minutes.

Sincerely,
DataStax Bot.

@datastax-bot

Copy link
Copy Markdown

Thank you @gnoremac for signing the Contribution License Agreement.

Cheers,
DataStax Bot.

@adutra

adutra commented Oct 3, 2016

Copy link
Copy Markdown
Contributor

for an apples-to-apples comparison, here is a small benchmark comparing this branch against 3.x:

CodecRegistryBenchmark.benchmarkCqlType            avgt   20   14.763 ±  0.343   ns/op
CodecRegistryBenchmark.benchmarkCqlTypeJavaType    avgt   20   14.447 ±  0.318   ns/op
CodecRegistryBenchmark.benchmarkCqlTypeJavaType2   avgt   20   15.170 ±  0.335   ns/op
CodecRegistryBenchmark.benchmarkCqlTypeValue       avgt   20   31.785 ±  0.531   ns/op
CodecRegistryBenchmark.benchmarkValue              avgt   20  211.274 ±  5.329   ns/op

CodecRegistryBenchmark.benchmarkCqlType            avgt   20   62.647 ±  0.958   ns/op
CodecRegistryBenchmark.benchmarkCqlTypeJavaType    avgt   20   73.477 ±  0.680   ns/op
CodecRegistryBenchmark.benchmarkCqlTypeJavaType2   avgt   20   66.423 ±  1.204   ns/op
CodecRegistryBenchmark.benchmarkCqlTypeValue       avgt   20   27.152 ±  0.284   ns/op
CodecRegistryBenchmark.benchmarkValue              avgt   20  197.722 ±  4.946   ns/op

Benchmark code here.

So it seems that it does bring a performance boost for common cases (cache hits for built-in codec lookups), while showing a slight degradation for other cases, as expected (probably due to the double lookup).

This benchmark does not cover on-the-fly codec creation, but I assume the results will be slightly worse for this branch compared to 3.x, due to the double lookup before attempting to create the missing codec.

I tend to think that it is a good idea to favor common lookup scenarios in detriment of complex lookups involving on-the-fly codec generation so this improvement seems to go in the good direction. Thanks @gnoremac !

@ben-manes

Copy link
Copy Markdown

Sorry I wasn't able to get performance fix for Guava's cache in during its development, and was unsuccessful thereafter. If you move to JDK8 then the rewrite is much faster. But regardless this seems like a good idea.

@adutra adutra added this to the 3.2.0 milestone Dec 22, 2016
@adutra

adutra commented Feb 8, 2017

Copy link
Copy Markdown
Contributor

@ben-manes would you mind rebasing this on top of current 3.x? Otherwise I can take care of it.

@ben-manes

Copy link
Copy Markdown

I think you meant to ping @gnoremac for the rebase

@adutra

adutra commented Feb 9, 2017

Copy link
Copy Markdown
Contributor

Created JAVA-1396 to explore Caffeine.

- Separate the built-in codecs cache so it can be backed by a static Map instead of Guava Cache
put(TypeCodec.time().getCqlType(), TypeCodec.time());
put(TypeCodec.uuid().getCqlType(), TypeCodec.uuid()); // must be declared before TimeUUIDCodec so it gets chosen when CQL type not available
put(TypeCodec.timeUUID().getCqlType(), TypeCodec.timeUUID());
put(TypeCodec.inet().getCqlType(), TypeCodec.inet());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the recent merge of JAVA-1347, this should have duration as well.

*/
public CodecRegistry() {
this.codecs = new CopyOnWriteArrayList<TypeCodec<?>>(PRIMITIVE_CODECS);
this.codecs = new CopyOnWriteArrayList<TypeCodec<?>>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting this here because I can't comment on unmodified lines: the doc of the codecs field (line 297) is now outdated:

/**
 * The list of registered codecs.
 * This list is initialized with the built-in codecs;
 * User-defined codecs are appended to the list.
 */
private final CopyOnWriteArrayList<TypeCodec<?>> codecs;

TypeCodec<?> codec = cache.get(cacheKey);
TypeCodec<?> codec = PRIMITIVE_CODECS.get(cqlType);
if (codec != null && (javaType == null || codec.accepts(javaType))) {
logger.trace("Returning cached codec {}", codec);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trivial: we could log "Returning primitive codec {}" here, to distinguish from the other case.

@adutra

adutra commented Feb 15, 2017

Copy link
Copy Markdown
Contributor

Closing in favor of #806 .

@adutra adutra closed this Feb 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants