-
-
Notifications
You must be signed in to change notification settings - Fork 972
Reduce cached codec metaclass registration overhead #15800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jamesfredley
wants to merge
7
commits into
8.0.x
Choose a base branch
from
fix/15374-codec-metaclass-registration-overhead
base: 8.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6e914cc
Avoid duplicate cached codec metaclass registration
jamesfredley 5311c4c
Add codec metaclass registration benchmark
jamesfredley 4805301
Run codec benchmark as opt-in Spock spec
jamesfredley a81a84b
Track codec registrations by weak factory keys
jamesfredley 413a4f3
Merge remote-tracking branch 'origin/8.0.x' into fix/15374-codec-meta…
jamesfredley c421fba
Merge remote-tracking branch 'origin/8.0.x' into fix/15374-codec-meta…
jamesfredley ac31469
Address codec metaclass registration review feedback
jamesfredley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
grails-encoder/src/test/groovy/org/grails/encoder/CodecMetaClassBenchmark.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 | ||
| * | ||
| * https://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.grails.encoder | ||
|
|
||
| import java.util.concurrent.atomic.AtomicInteger | ||
|
|
||
| import groovy.transform.CompileStatic | ||
| import groovy.transform.TypeCheckingMode | ||
| import groovy.lang.MetaClassRegistryChangeEventListener | ||
| import org.codehaus.groovy.runtime.InvokerHelper | ||
|
|
||
| import grails.util.GrailsMetaClassUtils | ||
|
|
||
| @CompileStatic | ||
| class CodecMetaClassBenchmark { | ||
|
|
||
| private static final int REGISTRATION_ITERATIONS = Integer.getInteger('grails.codec.benchmark.registrationIterations', 10_000) | ||
| private static final int ENCODE_ITERATIONS = Integer.getInteger('grails.codec.benchmark.encodeIterations', 1_000_000) | ||
| private static final int ENCODE_WARMUP_ITERATIONS = Integer.getInteger('grails.codec.benchmark.encodeWarmupIterations', 100_000) | ||
| private static final boolean NEW_FACTORY_EACH_REGISTRATION = Boolean.getBoolean('grails.codec.benchmark.newFactoryEachRegistration') | ||
|
|
||
| static void main(String[] args) { | ||
| GroovySystem.metaClassRegistry.removeMetaClass(CodecBenchmarkTarget) | ||
| CodecMetaClassSupport support = new CodecMetaClassSupport() | ||
| BenchmarkCodecFactory codecFactory = new BenchmarkCodecFactory() | ||
| List<ExpandoMetaClass> targetMetaClasses = [GrailsMetaClassUtils.getExpandoMetaClass(CodecBenchmarkTarget)] | ||
| AtomicInteger changeEvents = new AtomicInteger() | ||
| MetaClassRegistryChangeEventListener listener = { changeEvents.incrementAndGet() } as MetaClassRegistryChangeEventListener | ||
| GroovySystem.metaClassRegistry.addMetaClassRegistryChangeEventListener(listener) | ||
| try { | ||
| long registrationStart = System.nanoTime() | ||
| for (int i = 0; i < REGISTRATION_ITERATIONS; i++) { | ||
| CodecFactory registrationFactory = NEW_FACTORY_EACH_REGISTRATION ? new BenchmarkCodecFactory() : codecFactory | ||
| support.configureCodecMethods(registrationFactory, true, targetMetaClasses) | ||
| } | ||
| long registrationNanos = System.nanoTime() - registrationStart | ||
|
|
||
| CodecBenchmarkTarget target = new CodecBenchmarkTarget('a&b<c>d') | ||
| for (int i = 0; i < ENCODE_WARMUP_ITERATIONS; i++) { | ||
| encode(target) | ||
| } | ||
| long encodeStart = System.nanoTime() | ||
| Object encoded = null | ||
| for (int i = 0; i < ENCODE_ITERATIONS; i++) { | ||
| encoded = encode(target) | ||
| } | ||
| long encodeNanos = System.nanoTime() - encodeStart | ||
|
|
||
| println "registrationIterations=${REGISTRATION_ITERATIONS}" | ||
| println "newFactoryEachRegistration=${NEW_FACTORY_EACH_REGISTRATION}" | ||
| println "registrationNanos=${registrationNanos}" | ||
| println "registrationNanosPerOp=${registrationNanos / REGISTRATION_ITERATIONS}" | ||
| println "metaClassChangeEvents=${changeEvents.get()}" | ||
| println "codecMetaMethodRegistrations=${codecMetaMethodRegistrations()}" | ||
| println "encodeAsBenchmarkMetaMethods=${countEncodeAsBenchmarkMetaMethods(targetMetaClasses)}" | ||
| println "encodeIterations=${ENCODE_ITERATIONS}" | ||
| println "encodeNanos=${encodeNanos}" | ||
| println "encodeNanosPerOp=${encodeNanos / ENCODE_ITERATIONS}" | ||
| println "lastEncoded=${encoded}" | ||
| } finally { | ||
| GroovySystem.metaClassRegistry.removeMetaClassRegistryChangeEventListener(listener) | ||
| GroovySystem.metaClassRegistry.removeMetaClass(CodecBenchmarkTarget) | ||
| } | ||
| } | ||
|
|
||
| @CompileStatic(TypeCheckingMode.SKIP) | ||
| private static Object encode(CodecBenchmarkTarget target) { | ||
| InvokerHelper.invokeMethod(target, 'encodeAsBenchmark', null) | ||
| } | ||
|
|
||
| @CompileStatic(TypeCheckingMode.SKIP) | ||
| private static long codecMetaMethodRegistrations() { | ||
| try { | ||
| CodecMetaClassSupport.getMetaMethodRegistrationCount() | ||
| } catch (MissingMethodException ignored) { | ||
| -1L | ||
| } | ||
| } | ||
|
|
||
| @CompileStatic(TypeCheckingMode.SKIP) | ||
| private static int countEncodeAsBenchmarkMetaMethods(List<ExpandoMetaClass> targetMetaClasses) { | ||
| targetMetaClasses.sum { ExpandoMetaClass emc -> | ||
| emc.metaMethods.count { it.name == 'encodeAsBenchmark' } | ||
| } as int | ||
| } | ||
|
|
||
| private static class CodecBenchmarkTarget { | ||
|
|
||
| private final String value | ||
|
|
||
| CodecBenchmarkTarget(String value) { | ||
| this.value = value | ||
| } | ||
|
|
||
| @Override | ||
| String toString() { | ||
| value | ||
| } | ||
| } | ||
|
|
||
| private static class BenchmarkCodecFactory implements CodecFactory { | ||
|
|
||
| private final BenchmarkEncoder encoder = new BenchmarkEncoder() | ||
|
|
||
| @Override | ||
| Encoder getEncoder() { | ||
| encoder | ||
| } | ||
|
|
||
| @Override | ||
| Decoder getDecoder() { | ||
| null | ||
| } | ||
| } | ||
|
|
||
| private static class BenchmarkEncoder implements Encoder { | ||
|
|
||
| private final CodecIdentifier codecIdentifier = new DefaultCodecIdentifier('Benchmark') | ||
|
|
||
| @Override | ||
| CodecIdentifier getCodecIdentifier() { | ||
| codecIdentifier | ||
| } | ||
|
|
||
| @Override | ||
| Object encode(Object o) { | ||
| o?.toString()?.replace('&', '&')?.replace('<', '<')?.replace('>', '>') | ||
| } | ||
|
|
||
| @Override | ||
| void markEncoded(CharSequence string) { | ||
| } | ||
|
|
||
| @Override | ||
| boolean isSafe() { | ||
| true | ||
| } | ||
|
|
||
| @Override | ||
| boolean isApplyToSafelyEncoded() { | ||
| false | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.