Skip to content
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

Avoid trapping exceptions by using reflection #59

Merged
merged 2 commits into from
May 10, 2017
Merged

Avoid trapping exceptions by using reflection #59

merged 2 commits into from
May 10, 2017

Conversation

jcollins-g
Copy link
Contributor

Currently, dartdoc makes extensive use of the exception trapping and reflection inside mustache4dart. But trapping exceptions is expensive and profiling suggests a great deal of time is spent in here. This should remove the hotspot for clients like dartdoc without altering mustache4dart's behavior.

@valotas
Copy link
Owner

valotas commented May 8, 2017

Thanks for the PR. IT looks like travis does not like the formatting. Never the less, have a look at my comment.

@jcollins-g
Copy link
Contributor Author

I do not see a code comment.

I'll try using a different version of dartfmt and see if it likes it; this was dartfmt-ed with 1.24.0-dev.3.0.

Copy link
Owner

@valotas valotas left a comment

Choose a reason for hiding this comment

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

reflection should not be used unless (useMirrors && USE_MIRRORS) is true.

//Try to make dart2js understand that when we define USE_MIRRORS = false
//we do not want to use any reflector
return (useMirrors && USE_MIRRORS) ? ctxReflector[key] : null;
if (reflect(ctx).type.instanceMembers.containsKey(new Symbol("[]"))) {
Copy link
Owner

Choose a reason for hiding this comment

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

This check should be done if and only if (useMirrors && USE_MIRRORS) otherwise the code will get transpiled to js with a lot more code, unless something changed on the dart side.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Owner

Choose a reason for hiding this comment

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

What do you mean by done? You still use reflect without a check on useMirrors.

Copy link
Owner

Choose a reason for hiding this comment

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

Sorry, was reviewing wrong commit :). It looks good now. I'll try to bake a new version tonight.

@jcollins-g
Copy link
Contributor Author

I believe I've addressed the comment; dartfmt error was due to a change in formatting (unrelated to my change) with dart-style 1.0.5.

if (reflectedString.isAssignableTo(m.parameters[0].type)) {
try {
return ctx[key];
} catch (NoSuchMethodError) {
Copy link
Owner

Choose a reason for hiding this comment

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

I believe, here a null should be returned. I am not sure though, I have to check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously, if (useMirrors && USE_MIRRORS) is true and the attempt to use ctx[key] resulted in an exception, we returned ctxReflector[key]. So a noop here falls through to the base case of returning ctxReflector[key]. The other two if statements just try to get us to return ctxReflector[key] if we can predict in advance that calling ctx[key] will raise an exception.

@jcollins-g
Copy link
Contributor Author

Hi @valotas , just making sure there's nothing else you need from me.

@valotas valotas merged commit b26e509 into valotas:master May 10, 2017
@valotas
Copy link
Owner

valotas commented May 10, 2017

@jcollins-g all good. Just released 1.1.0 with your change

if (reflect(ctx).type.instanceMembers.containsKey(new Symbol("[]"))) {
MethodMirror m = reflect(ctx).type.instanceMembers[new Symbol("[]")];
TypeMirror reflectedString = reflectType(String);
if (reflectedString.isAssignableTo(m.parameters[0].type)) {
Copy link
Owner

Choose a reason for hiding this comment

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

It looks like dart2js has some problems with this one:

00:26 +8 -10: Deep subcontext test [E]                                                             
  UnimplementedError
  dart:_internal                                      dart<.JsTypeMirror.isAssignableTo
  package:mustache4dart/mustache_context.dart 257:9   dart<._ObjectReflector.fieldValue
  package:mustache4dart/mustache_context.dart 241:26  dart<._ObjectReflector.$index
  package:mustache4dart/mustache_context.dart 123:14  dart<._MustacheContext._getActualValue
  package:mustache4dart/mustache_context.dart 100:13  dart<._MustacheContext._getContextForKey
  package:mustache4dart/mustache_context.dart 69:18   dart<._MustacheContext._getInThisOrParent
  package:mustache4dart/mustache_context.dart 65:12   dart<._MustacheContext.field
  test/mustache_context_test.dart 192:12              dart<.main.<fn>

I'm afraid I have to trap some exception after all. Do you mind sharing your usecase? Maybe I can try the exception trapping deeper in order not to hit the performance .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That exception wouldn't be trapped in any case, because UnimplementedError is not a subclass of NoSuchMethodError. So it seems unlikely that reverting this change would correct that test. Does it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, never mind... isAssignableTo isn't implemented. Ouch.

Well, trapping the exception isn't the problem, it is catching it that impacts performance in the vm. As long as catching it doesn't introduce a performance problem in dart2js, it should be safe to do that from my case's perspective.

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

Successfully merging this pull request may close these issues.

2 participants