Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Inefficient access of LinkedList in Resource$Builder.mergeResources #4294

Closed
drabe8 opened this issue Oct 16, 2019 · 3 comments
Closed

Inefficient access of LinkedList in Resource$Builder.mergeResources #4294

drabe8 opened this issue Oct 16, 2019 · 3 comments
Milestone

Comments

@drabe8
Copy link

drabe8 commented Oct 16, 2019

In org.glassfish.jersey.server.model.Resource$Builder.mergeResources, we see indexed "get"s of the "resources" list, which at least in some cases is a LinkedList. Calling get( n ) on an LinkedList requires traversal of "n" nodes, which is much slower than an indexed get on an ArrayList.

In our application, this is the flow where we see the problem:

        org.glassfish.jersey.server.model.Resource$Builder.mergeResources(Resource.java:573)
        org.glassfish.jersey.server.model.Resource$Builder.buildResourceData(Resource.java:602)
        org.glassfish.jersey.server.model.Resource$Builder.build(Resource.java:655)
        org.glassfish.jersey.server.ResourceBag$Builder.registerModel(ResourceBag.java:121)
        org.glassfish.jersey.server.ResourceBag$Builder.registerResource(ResourceBag.java:89)
        org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:467)
        org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:184)
        org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350)
        org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347)
        org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255)
        org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:347)
        org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392)
        org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)
        org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:415)```

If the LinkedList only contains a few elements, it's probably not a big deal, but we see it with indexes in the 60's. I believe this only occurs at application initialization time, so should not be a big performance hit at runtime... but still might be worth looking at as a possible optimization.
@jansupol
Copy link
Contributor

What version of Jersey do you use? To me, it looks like ArrayList is used already.

@jansupol
Copy link
Contributor

I see, List<Resource.Data> resources is a LinkedList. Yes, should be fixed.

@jansupol
Copy link
Contributor

jansupol commented Nov 1, 2019

Fixed by #4300

@jansupol jansupol added this to the 2.30 milestone Nov 1, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants