-
Notifications
You must be signed in to change notification settings - Fork 50
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
Fix up javaslang FlavourImpl, modify some derivators to generate slightly more idiomatic code #29
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix up javaslang FlavourImpl, modify some derivators to generate slig…
…htly more idiomatic code
- Loading branch information
commit 2eb44fa9cf071cfdea8c621847094ede02e94280
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,9 +62,9 @@ public static DeriveResult<DerivedCodeSpec> derive(AlgebraicDataType adt, Derive | |
TypeSpec.Builder typeSpecBuilder = TypeSpec.classBuilder(className) | ||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL) | ||
.addTypeVariables(typeVariableNames) | ||
.addField(FieldSpec.builder(TypeName.get(Object.class), "lock", Modifier.PRIVATE, Modifier.FINAL).initializer("new Object()").build()) | ||
.addField(FieldSpec.builder(lazyArgTypeName, "expression", Modifier.PRIVATE).build()) | ||
.addField(FieldSpec.builder(typeName, "evaluation", Modifier.PRIVATE, Modifier.VOLATILE).build()) | ||
.addField(FieldSpec.builder(TypeName.BOOLEAN, "initialized", Modifier.PRIVATE, Modifier.VOLATILE).build()) | ||
.addField(FieldSpec.builder(lazyArgTypeName, "expression", Modifier.PRIVATE, Modifier.FINAL).build()) | ||
.addField(FieldSpec.builder(typeName, "evaluation", Modifier.PRIVATE).build()) | ||
.addMethod(MethodSpec.constructorBuilder() | ||
.addParameter(ParameterSpec.builder(lazyArgTypeName, lazyArgName).build()) | ||
.addStatement("this.expression = $N", lazyArgName).build()) | ||
|
@@ -73,20 +73,20 @@ public static DeriveResult<DerivedCodeSpec> derive(AlgebraicDataType adt, Derive | |
.addModifiers(Modifier.PRIVATE) | ||
.returns(typeName) | ||
.addCode(CodeBlock.builder() | ||
.addStatement("$T _evaluation = this.evaluation", typeName) | ||
.beginControlFlow("if (_evaluation == null)") | ||
.beginControlFlow("synchronized (this.lock)") | ||
.addStatement("_evaluation = this.evaluation") | ||
.beginControlFlow("if (_evaluation == null)") | ||
.addStatement("this.evaluation = _evaluation = expression.$L()", Utils.getAbstractMethods(lazyTypeElement.getEnclosedElements()).get(0).getSimpleName()) | ||
.addStatement("this.expression = null") | ||
.endControlFlow().endControlFlow().endControlFlow() | ||
.beginControlFlow("if (!initialized)") | ||
.beginControlFlow("synchronized (this)") | ||
.beginControlFlow("if (!initialized)") | ||
.addStatement("$T _evaluation = expression.$L()", typeName, Utils.getAbstractMethods(lazyTypeElement.getEnclosedElements()).get(0).getSimpleName()) | ||
.addStatement("evaluation = _evaluation") | ||
.addStatement("initialized = true") | ||
.addStatement("return _evaluation") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the expression was set to null also to free-up memory. |
||
.endControlFlow().endControlFlow().endControlFlow() | ||
.addStatement("return evaluation") | ||
.build()) | ||
.build()) | ||
.addMethod( | ||
Utils.overrideMethodBuilder(adt.matchMethod().element()) | ||
.addStatement("return this.eval().$L($L)", adt.matchMethod().element().getSimpleName(), Utils.asArgumentsStringOld(adt.matchMethod().element().getParameters())) | ||
.addStatement("return eval().$L($L)", adt.matchMethod().element().getSimpleName(), Utils.asArgumentsStringOld(adt.matchMethod().element().getParameters())) | ||
.build()); | ||
|
||
if (adt.typeConstructor().declaredType().asElement().getKind() == ElementKind.INTERFACE) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not synchronizing on
this
to avoid potential synchronization issues as the lazy value could potentially be used as a monitor. But then this would probably be bad practice. As a reference, I think Guava Suppliers.memoize does synchronize onthis
. I need to benchmark if avoiding the creation of the lock Object result in significative performance improvement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I was basing this on Suppliers.memoize, but I did not run any benchmarks — they seem to be trying to avoid making evaluation a volatile but I have no idea if that makes a difference in practice. I will see if I can come up with a benchmark to compare the different implementations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suppliers.memoize also implements Serializable (without serializing the cached value) which limit the possible implementation much more than if we don't implement Serializable. I think using an initialized field is therefore not necessary if we don't implement Serializable. Benchmarking synchronization on
this
is worth it, though.