Giter Club home page Giter Club logo

Comments (8)

Shane32 avatar Shane32 commented on July 30, 2024 1

Agreed. Unfortunately this would duplicate almost all of the code in both ASTVisitor.cs and SDLPrinter.cs. FYI if you’re writing to a string builder or MemoryStream, I believe GetAwaiter().GetResult() is entirely safe to use because it will run synchronously regardless. But if you’re writing to a file stream or network stream, then there may be multithreading implications (although unlikely for most use cases).

from parser.

Shane32 avatar Shane32 commented on July 30, 2024 1

And it would necessitate duplicating all of the relevant tests for the AST walker and SDL printer. It's just a whole lot of code duplication to properly add synchronous support.

Perhaps a temporary solution might be to add a Print method that accepts a StringBuilder (and/or returns a string). Perhaps also add an overload that accepts a MemoryStream and Encoding to write to a memory stream. These methods could simply use GetAwaiter().GetResult() under the hood but would be known to execute synchronously and hence be free from ill effects.

from parser.

Shane32 avatar Shane32 commented on July 30, 2024

See:

from parser.

nightroman avatar nightroman commented on July 30, 2024

Would replacing ValueTask with Task be a solution, too? I am not that familiar with ValueTask. Quick googling tells it is used in order to improve performance and should be used only when it really improves performance. Because ValueTask introduces some trade-offs (as we see here, for example). Also, in some cases (they say) Task may actually perform better.

from parser.

nightroman avatar nightroman commented on July 30, 2024

Perhaps a temporary solution might be to add a Print method that accepts a StringBuilder (and/or returns a string). Perhaps also add an overload that accepts a MemoryStream and Encoding to write to a memory stream. These methods could simply use GetAwaiter().GetResult() under the hood but would be known to execute synchronously and hence be free from ill effects.

It looks like a good solution, straightforward for the end user, with all tedious caveats resolved internally.

from parser.

Shane32 avatar Shane32 commented on July 30, 2024

ValueTask<T> reduces allocations when the method runs synchronously. Typically this does not occur when using asynchronous methods such as writing to a file or network stream, but even then might if reading/writing cached data. However in this case where the underlying operation is often (or typically) writing to a string builder, there are notable performance benefits to ValueTask<T>.

Note that performance benefits of ValueTask over Task are very rare, as typically the static instance of Task.CompletedTask is returned from synchronously-completed asynchronous methods that return Task. However in this library, ValueTask was used throughout for consistency.

While there are some limitations with ValueTask, there are no benefits or consequences to ValueTask over Task as it relates to executing asynchronous code synchronously.

You may be interested in these relevant articles on the use of ValueTask and allocations:

Notably, while .NET Framework might require up to 4 allocations the first time await is encountered, and 2 for each additional await in the same method, .NET Core reduces this to 1 (per method I believe), so the benefits of using ValueTask on .NET Core is less pronounced than on .NET Framework (but still exist).

That means, whereas in .NET Framework we have at least four allocations for the first suspension and often at least two allocations for each subsequent suspension, in .NET Core we have one allocation for the first suspension (worst case two, if custom awaiters are used), and that’s it.

Just FYI

from parser.

nightroman avatar nightroman commented on July 30, 2024

@Shane32 Thank you for taking an action on this. Looking forward to the next version with Print(s).

from parser.

sungam3r avatar sungam3r commented on July 30, 2024

Published in https://github.com/graphql-dotnet/parser/releases/tag/8.2.0

from parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.