Skip to content

Suboptimal performance of ControllerLinkBuilder.linkTo(…) #511

Closed

Description

@nwoolls

Hi there. I'm trying out this framework and running into performance issues out of the gate.

For instance, I have a simple method where I add 3 resource links to each result in an array. There are 537 (but it could be more) items. This takes 20 seconds just to add the resource links to the array. Additionally, making subsequent calls to the method seems to have a compounding penalty on performance. Obviously this is unacceptable. I think I must be missing something.

20310 milliseconds to add 3 Spring HATEOAS links to 537 items
53933 milliseconds to add 3 Spring HATEOAS links to 537 items
89763 milliseconds to add 3 Spring HATEOAS links to 537 items

I see references to using the EntityLinks class but this has its own limitations (it seems, requiring one controller-per-model).

Am I hopefully overlooking something simple here? Here is the code in question:

long startTime = System.currentTimeMillis();

for (final MyEntity result : results) {
    final MyLocation location = result.getLocation();
    final String primaryId = location.getStoreId();
    final String secondaryId = location.getSettingId();

    result.add(linkTo(
            methodOn(MyController.class)
            .methodNumber1(primaryId, secondaryId))
            .withSelfRel());

    result.add(linkTo(
            methodOn(MyController.class)
            .methodNumber2(primaryId))
            .withRel(REL_FIRST));

    result.add(linkTo(
            methodOn(MyController.class)
            .methodNumber3(primaryId, secondaryId))
            .withRel(REL_SECOND));
}

long duration = (System.currentTimeMillis() - startTime);

System.err.println(String.format("%d milliseconds to add 3 Spring HATEOAS links to %d items",
        duration, results.size()));

Activity

  1. nwoolls commented on Nov 20, 2016

    @nwoolls
    Author

    To be clear this seems related to the use of ControllerLinkBuilder. If I create links manually (without the benefit of static typing for route building) the problem goes away.

  2. odrotbohm commented on Nov 20, 2016

    @odrotbohm
    Member

    That's interesting and definitely needs to be fixed. Do you have that sample code in a project somewhere that we could profile it?

    I suspect the proxy and the proxy class creation necessary to be the culprits here. We could try to more aggressively cache those, I guess. But still, a project to try those experiments on would be very welcome. :)

  3. odrotbohm commented on Nov 20, 2016

    @odrotbohm
    Member

    I just gave this a spin and cannot reproduce your numbers at all:

    package org.springframework.hateoas.mvc;
    
    import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.junit.Test;
    import org.springframework.hateoas.ResourceSupport;
    import org.springframework.hateoas.TestUtils;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * @author Oliver Gierke
     */
    public class ControllerLinkBuilderPerformanceTest extends TestUtils {
    
        @Test
        public void testname() {
    
            for (int y = 0; y < 5; y++) {
    
                List<MyEntity> results = new ArrayList<MyEntity>();
    
                for (int i = 0; i < 500; i++) {
                    results.add(new MyEntity());
                }
    
                long startTime = System.currentTimeMillis();
    
                String primaryId = "foo";
                String secondaryId = "bar";
                String relFirst = "fooRel";
                String relSecond = "barRel";
    
                for (final MyEntity result : results) {
    
                    result.add(linkTo(methodOn(MyController.class).methodNumber1(primaryId, secondaryId)).withSelfRel());
    
                    result.add(linkTo(methodOn(MyController.class).methodNumber2(primaryId)).withRel(relFirst));
    
                    result.add(linkTo(methodOn(MyController.class).methodNumber3(primaryId, secondaryId)).withRel(relSecond));
                }
    
                long duration = (System.currentTimeMillis() - startTime);
    
                System.err.println(
                        String.format("%d milliseconds to add 3 Spring HATEOAS links to %d items", duration, results.size()));
            }
        }
    
        static class MyEntity extends ResourceSupport {}
    
        static class MyController {
    
            @RequestMapping("/foo")
            Object methodNumber1(String first, String second) {
                return null;
            }
    
            @RequestMapping("/bar")
            Object methodNumber2(String first) {
                return null;
            }
    
            @RequestMapping("/fooBar")
            Object methodNumber3(String first, String second) {
                return null;
            }
        }
    }

    Yields:

    484 milliseconds to add 3 Spring HATEOAS links to 500 items
    144 milliseconds to add 3 Spring HATEOAS links to 500 items
    120 milliseconds to add 3 Spring HATEOAS links to 500 items
    116 milliseconds to add 3 Spring HATEOAS links to 500 items
    98 milliseconds to add 3 Spring HATEOAS links to 500 items
    

    Any chance that your preparing call cause the delay?

  4. nwoolls commented on Nov 20, 2016

    @nwoolls
    Author

    Thanks for the feedback. I will work on creating a sample that reproduces the issue.

    I am certain that it is the code adding the links (specifically the calls to linkTo() and methodOn()) that causes the delay. I changed that code to create Link objects manually instead of using the methods on ControllerLinkBuilder and the problem goes away:

    public static List<MyEntity> addMyEntityLinks(final List<MyEntity> myEntities) {
        for (final MyEntity myEntity : myEntities) {
            addMyEntityLinks(myEntity);
        }
        return myEntities;
    }
    
    public static MyEntity addMyEntityLinks(final MyEntity myEntity) {
        final MyLocation location = myEntity.getLocation();
        final String primaryId = location.getPrimaryId();
        final String secondaryId = location.getSecondaryId();
    
        myEntity.add(
                new Link(String.format(URL_FORMAT_NO1, getApiRootPath(), primaryId, secondaryId), REL_SELF),
                new Link(String.format(URL_FORMAT_NO2, getApiRootPath(), primaryId), REL_FIRST),
                new Link(String.format(URL_FORMAT_NO3, getApiRootPath(), primaryId, secondaryId), REL_SECOND)
        );
        return myEntity;
    }

    However I'd much rather use the strongly typed methods of ControllerLinkBuilder. I'll post back once I have a sample.

  5. nwoolls commented on Nov 20, 2016

    @nwoolls
    Author
  6. added a commit that references this issue on Nov 21, 2016
  7. odrotbohm commented on Nov 21, 2016

    @odrotbohm
    Member

    I've just pushed a couple of improvements that a profiling session unveiled feasible. The core culprit however is an issue in core Spring Framework which causes the synthesized annotations on method parameters to undergo a repeated equals(…) comparison which is quite costly.

    You can see the numbers drop significantly if you copy Spring's TypeDescriptor into your project and comment out the annotation related code in its equals(…) method. I've filed a ticket for that and another one that turned out to be the next hotspot after our improvements made for this ticket here.

    I am resolving this one as fixed as we essentially have to rely on Spring Framework to fix that issue downstream.

  8. self-assigned this
    on Nov 21, 2016
  9. added this to the 0.22 milestone on Nov 21, 2016
  10. changed the title [-]Poor performance simply adding links to results (addl 20-90s)[/-] [+]Suboptimal performance of ControllerLinkBuilder.linkTo(…)[/+] on Nov 21, 2016
  11. added a commit that references this issue on Dec 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions