Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 05:37
    CyrusNajmabadi synchronize #57979
  • 05:35
    CyrusNajmabadi synchronize #57979
  • 05:34
    CyrusNajmabadi synchronize #57979
  • 05:33
    CyrusNajmabadi synchronize #57979
  • 05:24
    CyrusNajmabadi synchronize #57979
  • 05:23
    CyrusNajmabadi synchronize #57979
  • 05:22
    CyrusNajmabadi synchronize #57979
  • 05:08
    CyrusNajmabadi synchronize #57979
  • 05:08
    CyrusNajmabadi synchronize #57979
  • 04:53
    CyrusNajmabadi synchronize #57979
  • 04:49
    CyrusNajmabadi synchronize #57979
  • 04:47
    CyrusNajmabadi synchronize #57979
  • 04:46
    CyrusNajmabadi synchronize #57979
  • 04:42
    CyrusNajmabadi synchronize #57979
  • 04:38
    jmarolf commented #55834
  • 04:37
    jmarolf unlabeled #55834
  • 04:23
    CyrusNajmabadi commented #58119
  • 04:23
    CyrusNajmabadi commented #58119
  • 03:14
    WeihanLi commented #58119
  • 03:01
    CyrusNajmabadi commented #58119
CyrusNajmabadi
@CyrusNajmabadi
CSharpSyntaxGenerator uses the language feature 'Source Generators'
and
Bernd Baumanns
@bernd5
Project.GetSourceGeneratedDocumentsAsync seems to query / build the Compilation
(Project based on MSBuildWorkspace)
This is okay - but it takes quite a long time even if the project has no source generator
Is there some way to determine if a project has source generators?
Oh, new projects seem to have an implicit SourceGenerator
Bernd Baumanns
@bernd5
"System.Text.Json.SourceGeneration.JsonSourceGenerator"
Bernd Baumanns
@bernd5
Can we disable it?
I mean I don't want a JsonSourceGenerator for projects which do nothing with Json...
Where is it defined?
Bernd Baumanns
@bernd5
Is it intended or a bug?
CyrusNajmabadi
@CyrusNajmabadi
it's intentional if the project links in (Somehow) that generator.
it's possible it's built into the sdk.
you might need to create a discussion on the sdk side of things asking if there's a way to disable this.
Bernd Baumanns
@bernd5
@CyrusNajmabadi thanks
Do you know where I can create such a discussion? Which repo?
CyrusNajmabadi
@CyrusNajmabadi
Andrew Hill
@AartBluestoke

https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABAJgEYBYAKGIGYACMhgYQYG8aHunGBLAHYYGAFSgBPABQBKDlx4LBw3AFd8AXgAMAbnkLuGCZ2r7TDAGbQGkpQz5btdgDzlNOuwGoP0vWdOr8D3U+XRMzAF8GMGwMMAALdnDfBWSeYgB2BgDQhSTqGl96OyEGADkIMSlZYz9bAIdU00soa1t7dz4XN0c+Lx8wv381IJDGpkzs3zyacKA=

    public int Try() {
        int sum=0;
        try{
            for (int i=0; i<100; i++)
                sum+=i;
        } catch{}

        return sum;
    }


    public int NoTry() {
        int sum=0;
            for (int i=0; i<100; i++)
                sum+=i;
        return sum;
    }

adding a try{...}catch{} around a trivial for loop can strongly affect its optimisation ; is this expected?

Antony Male
@canton7
The actual loop hasn't changed that much -- it's got a couple more movs
Andrew Hill
@AartBluestoke
those movs make it about 3x as slow when you benchmark.net it (but i don't know of an online code fragment benchmark)
Bernd Baumanns
@bernd5
Yeah, the data is now stored on the stack instead of register usage
This would be more a runtime question - roslyn compiles only to IL...
Andrew Hill
@AartBluestoke
should i cross-post to dotnet/coreclr then?
Antony Male
@canton7
Yeah, but as the loop grows to something realistic, those movs will become less significant
arturohernandez10
@arturohernandez10
Does anyone know if of any work on limited scope partial application on C#? Something as simple as tagging an array known at compile time for expansion during compilation as well.
CyrusNajmabadi
@CyrusNajmabadi
sorry, not sure what you're asking about
Something as simple as tagging an array known at compile time for expansion during compilation as well.
can you expand (no pun intended) on what you're asking about?
arturohernandez10
@arturohernandez10

@CyrusNajmabadi
Sure...

        public static void Original()
    {
        var attributes = new [] { new { Name = "Country", Include = false }};
        var dict = new Dictionary<string,string>(){{"Country", "USA"}};
        var list = new List<string>();

        foreach (var attribute in attributes)
            if (attribute.Include)
                list.Add(dict[attribute.Name]);                
    }

    public static void Expanded()
    {
        var dict = new Dictionary<string,string>(){{"Country", "USA"}};
        var list = new List<string>();

        list.Add(dict["USA"]);                
    }
}

It's a somewhat contrived example. In general, you have a variable like attributes in this case, whose entire contents are known at compile time. Expanding only within a limited scope, may be a tractable problem. I clearly used foreach rather than whereas part of limiting the scope. Many coding patterns could be expressed in a similar way, while keeping a good amount of type and syntax checking. And of course this would be useful in code generation.

CyrusNajmabadi
@CyrusNajmabadi
sure. you could use source-generators to solve the above.
look for whatever pattern you want, and generate the final form as-app
arturohernandez10
@arturohernandez10
@CyrusNajmabadi Do you know of any previous, useful or similar work. I'd rather not reinvent the wheel.
CyrusNajmabadi
@CyrusNajmabadi
not really...
there are likely lots of source generators you could look at
but your use case is pretty specific
but would be very feasible with the source generator (and other) roslyn apis.
arturohernandez10
@arturohernandez10
It's the partial evaluation that I could use, even if it's just a reference to a research paper. I do have an idea on how to do this. But I always do a little bit of research. Is this the best place to ask?
CyrusNajmabadi
@CyrusNajmabadi
no, i know of nothing in roslynt hat would help with that.
this is a fine place to ask :)
Yair Halberstadt
@YairHalberstadt
Here's a simple hacky way to do it if the result is a constant. The user marks a static method as CompileTimeEvaluated using an attribute. The SG takes only that method and copies it into a new compilation which has the same metadata references as the previous compilation. It emits that compilation as an assembly and runs that method. It then puts the result in a property somewhere.
That would work reasonably well if the result is any serializable type, but deserialization is likely to be more expensive than just running the method at runtime anyway.
arturohernandez10
@arturohernandez10
@YairHalberstadt Thanks a lot!! I am really going after extracting patterns from codebases. I would like to have a Pattern project, similar to Test project. Just like you can do a Partial evaluation you could take an existing piece of code. Give it a generic interface and using generic types you could define the pattern with a lower abstraction cost. ;)
Yair Halberstadt
@YairHalberstadt
I'm not sure i understood to be honest
arturohernandez10
@arturohernandez10

@YairHalberstadt
Yes, here is an example:

    public static TOut Pattern<TIn,TOut>(Attributes attributes) => (TIn input) =>
    {
        var result = new TOut();

        foreach (var attribute in attributes)
            if (attribute.IsEnum())
                attribute.Assign(result, input);

        return result;
    }

    public static Person Example(Student person)
    {
        var result = new Person();

        result.Degree = __.GivenField(person.Degree.ToString(), a.IsEnum());

        return result;
    }

In sample we can use an example object (not to write generic). __.GivenField Is just a type preserving function, which also has room for an expression tree. The expression tree is used to determine the kind of code generation to be used. And would match the pattern above. The idea is that the pattern implementation code, will always be harder to read. While the example code should be much easier. I think this could be a good thing to try.

I would like to generate the patterns from example decorated code. And that could be a second step. I like the idea of writing a quick first implementation even if the code is ugly.
Авдеев Вячеслав
@lsoft
so, with vs2022 preview4 incremental generators are going in the wild))) but no docs and examples exists :( (except for single not completed md file)...