Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Reduce Revision check calls via interface #623

Closed
wants to merge 1 commit into from

Conversation

benaadams
Copy link
Contributor

@benaadams benaadams commented May 3, 2016

Can cache the first lookup

Revision calls

Revision calls 2

@Tratcher Tratcher self-assigned this May 3, 2016
@Tratcher Tratcher added this to the 1.0.0 milestone May 3, 2016
@rynowak
Copy link
Member

rynowak commented May 4, 2016

😻

@rynowak
Copy link
Member

rynowak commented May 4, 2016

Not to derail too much, but do we have a feel for how much benefit we're really getting from using this pattern instead of just calling Get all the time?

@benaadams
Copy link
Contributor Author

a feel for how much benefit we're really getting from using this pattern instead of just calling Get all the time?

If you only access a feature once per request not much; but if you access it more than once it should be significant e.g. the plaintext benchmark accesses httpContext.Response 4 times

However instead of doing

if (httpContext.Request.Path.StartsWithSegments(_path, StringComparison.Ordinal))
{
    httpContext.Response.StatusCode = 200;
    httpContext.Response.ContentType = "text/plain";
    httpContext.Response.Headers["Content-Length"] = "13";
    return httpContext.Response.Body.WriteAsync(_helloWorldPayload, 0, _helloWorldPayload.Length);
}

It could do

if (httpContext.Request.Path.StartsWithSegments(_path, StringComparison.Ordinal))
{
    var response = httpContext.Response;
    response.StatusCode = 200;
    response.ContentType = "text/plain";
    response.Headers["Content-Length"] = "13";
    return response.Body.WriteAsync(_helloWorldPayload, 0, _helloWorldPayload.Length);
}

To access it only once; but users probably won't do that

@benaadams
Copy link
Contributor Author

@rynowak aspnet/Benchmarks#84 :)

@benaadams
Copy link
Contributor Author

My mistake, it still goes via the feature anyway even if the Reponse is cached; however the first access on StatusCode is very high

feature

With the Get on the frame being 41% of that + casting etc

get

Then the lookups for the other features are very low

@rynowak
Copy link
Member

rynowak commented May 4, 2016

Maybe we could make the cost of the extra lookups even lower? 😀 #617

The hope is that the cost of abstraction is low and that doing optimizations like using the feature directly aren't super beneficial.

@benaadams
Copy link
Contributor Author

I was trying to see if it could be a compile/jit time lookup though it would only work with generics not typeof dev...benaadams:strong-typed-features

{
var cleared = false;
if (Revision != Collection.Revision)
if (Revision == 0)
Copy link
Member

@rynowak rynowak May 5, 2016

Choose a reason for hiding this comment

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

We could also check cached == null here, but this might do enough already. And if we're worried about inlinability being more thorough might be bad (code size)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If cached == null it couldn't update the revision count as it may have changed and other cached features may be invalid; which would mean on second use it would have to clear the cache.

Copy link
Member

Choose a reason for hiding this comment

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

This makes some assumptions about the implementation of Collection.Revision and the initial value. E.g. WebListener's Revision count starts at 0 so this code will keep executing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolved, but 3 nested ifs :(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, but this will go wrong if revision has changed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

k, now is fixed

Copy link
Member

Choose a reason for hiding this comment

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

This code could do with some comments. If we had fxcop it would probably complain about the cyclomatic complexity

Copy link
Member

Choose a reason for hiding this comment

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

You'd be surprised how high that complicity limit is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Better?

@benaadams
Copy link
Contributor Author

OSX is grumpy on restore

@benaadams
Copy link
Contributor Author

Looks to be faster for the same number of requests

Pre
pre-feature-changes

Post
post-feature-changes

@Tratcher
Copy link
Member

:shipit:

@Tratcher
Copy link
Member

Rebased and merged.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants