#1148 - Further performance improvements in link creation.

The implementation details of WebHandler have been significantly refactored to rather work with structures that allow better cacheability by clearly separating abstractions over the statically available information from the per-invocation aspects. This results in a new HandlerMethodParameter(s) abstraction within WebHandler. BoundMethodParameter has been removed entirely. HandlerMethodParameters are create once then cached for every controller method being linked to.

DummyInvocationUtils now creates a ThreadLocal cache of the proxies created for calls to methodOn(…) as they essentially only act as basis for subsequent calls to the methods on the proxy created which in turn are expected to be handed into a linkTo(…) call which obtains the invocation right away. This avoids overhead in cases methodOn(…) is called multiple times for the same controller from a single controller.

The lookup of the LastInvocationAware was previously routed through the proxy, handled by InvocationRecordingMethodInterceptor. This resulted in a second, reflective call for every link creation. DummyInvocationUtils now provides a dedicated lookup method as it knows about the structure of the proxy it created and thus can unfold the recorded invocation more effectively.

The LinkBuilder type hierarchy now works with UriComponents and only creates a UriComponentsBuilder if it needs to modify the backing link in the first place. This avoids superfluous back and forth between UriComponents and UriComponentsBuilders that involved quite a bit of String parsing and creation.

EncodingUtils now starts from a StandardCharsets.UTF_8 to avoid repeated Charset creation.

The changes result in a ~3x performance compared to 1.0.2.RELEASE:

1.0.2.RELEASE

Benchmark                                         Mode  Cnt         Score        Error  Units
ControllerLinkBuilderBenchmark.noLinkCreation    thrpt   10  39004583,189 ± 751668,181  ops/s
ControllerLinkBuilderBenchmark.pureLinkCreation  thrpt   10     43443,133 ±    783,120  ops/s
ControllerLinkBuilderBenchmark.withLinkCreation  thrpt   10     60201,629 ±   1292,179  ops/s

1.1 / 1.0.3 SNAPSHOT

Benchmark                                         Mode  Cnt         Score        Error  Units
ControllerLinkBuilderBenchmark.noLinkCreation    thrpt   10  39618560,950 ± 612794,310  ops/s
ControllerLinkBuilderBenchmark.pureLinkCreation  thrpt   10    121700,634 ±   1510,415  ops/s
ControllerLinkBuilderBenchmark.withLinkCreation  thrpt   10    121982,085 ±   3344,206  ops/s

noLinkCreation - creates a single RepresentationModel instance but adds no links
pureLinkCreation - creates a single link pointing to a controller method
withLinkCreation - creates a single RepresentationModel instance adding a single link
This commit is contained in:
Oliver Drotbohm
2019-12-06 14:24:02 +01:00
parent 9f05bea8ab
commit 452189edd8
14 changed files with 407 additions and 375 deletions

View File

@@ -23,6 +23,7 @@ import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.hateoas.Affordance;
import org.springframework.hateoas.TestUtils;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
/**
@@ -36,14 +37,16 @@ class LinkBuilderSupportUnitTest extends TestUtils {
@Test
void callingSlashWithEmptyStringIsNoOp() {
SampleLinkBuilder builder = new SampleLinkBuilder(UriComponentsBuilder.newInstance(), Collections.emptyList());
SampleLinkBuilder builder = new SampleLinkBuilder(UriComponentsBuilder.newInstance().build(),
Collections.emptyList());
assertThat(builder.slash("")).isEqualTo(builder);
}
@Test
void appendsFragmentCorrectly() {
SampleLinkBuilder builder = new SampleLinkBuilder(UriComponentsBuilder.newInstance(), Collections.emptyList());
SampleLinkBuilder builder = new SampleLinkBuilder(UriComponentsBuilder.newInstance().build(),
Collections.emptyList());
builder = builder.slash("foo#bar");
assertThat(builder.toString()).endsWith("foo#bar");
builder = builder.slash("bar");
@@ -62,7 +65,8 @@ class LinkBuilderSupportUnitTest extends TestUtils {
@Test
void appendsPathContainingColonsCorrectly() {
SampleLinkBuilder builder = new SampleLinkBuilder(UriComponentsBuilder.newInstance(), Collections.emptyList());
SampleLinkBuilder builder = new SampleLinkBuilder(UriComponentsBuilder.newInstance().build(),
Collections.emptyList());
builder = builder.slash("47:11");
@@ -71,8 +75,8 @@ class LinkBuilderSupportUnitTest extends TestUtils {
static class SampleLinkBuilder extends LinkBuilderSupport<SampleLinkBuilder> {
public SampleLinkBuilder(UriComponentsBuilder builder, List<Affordance> afforances) {
super(builder, afforances);
public SampleLinkBuilder(UriComponents components, List<Affordance> afforances) {
super(components, afforances);
}
@Override
@@ -85,8 +89,8 @@ class LinkBuilderSupportUnitTest extends TestUtils {
* @see org.springframework.hateoas.core.LinkBuilderSupport#createNewInstance(org.springframework.web.util.UriComponentsBuilder, java.util.List)
*/
@Override
protected SampleLinkBuilder createNewInstance(UriComponentsBuilder builder, List<Affordance> affordances) {
return new SampleLinkBuilder(builder, affordances);
protected SampleLinkBuilder createNewInstance(UriComponents components, List<Affordance> affordances) {
return new SampleLinkBuilder(components, affordances);
}
}
}