0

I've been wanting for a while to shorten the (no-using-pollution) "inline" attribute from the absurdity that is:
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
to, well, [InlineHint] or [MyCompilerSvcs.InlineHint] or similar- something both quicker to type and quicker to mentally parse.

Is there any way to actually do that? Right now the only "sane" options I can see are to either add using System.Runtime.CompilerServices; (which is a pain when dealing with code behind of an ASP.NET website), adding more specific using aliases (even worse), or to keep it long-form in an accessible location for copy-paste.

Providing this question from 2009 isn't too outdated, using seems to be the only way to shorten how large the attribute reference is (as nobody suggested a more keyword-like variant for large, multifile projects). This related question was from 2010, and also suggests a using trick.

From 2015, there was this question, but it was in reference to the resulting decorations. Since what I'm interested in is the compiler directives themselves (and a performance-based one at that!) I doubt a runtime IL Emit could do this, and a "code bridge" doesn't quite naturally extend to compiler services in my mind.

Currently targeting C# 4.5, but newer versions are not forbidden.

And before "The compiler does inlining automatically!", it only does so for 32 IL Bytes or less and the inline hint overrides the size restriction. There are also other options which could be useful to have more accessible, such as NoOptimization, NoInline, and Synchronized, all of which I would very much like to not have to type absurdly long attributes to access without using statements.

abluejelly
  • 1,266
  • 9
  • 17
  • You are aware that you already have a list of `using` statements in the code behind? Where is the problem in adding another one? And writing the first attribute and then doing a Ctrl + . and adding the using statement automatically is not exactly what I would call a pain.On top of that I doubt you will see any visible performance improvement in a code behind through inlining. – Sefe Jan 09 '17 at 21:26
  • @Sefe Adding the using statement to the automatic set works for new code-behinds, but not for any already-existing ones. I've got a rather large codebase which I would then have to manually add it to. Fixing it going forward isn't bad, but fixing it across multiple in-place projects is a little annoying (best way I can think of is insert after all `using System;\r\n` on a document-wide regex replace). The problem then comes full-force again when I go to strip unneeded dependencies again, and all pages not currently using it nix it. – abluejelly Jan 09 '17 at 23:54
  • Edit->Replace->Entire Solution, Go To Next Error (Shft + Ctrl + F12), Ctrl + ., Add Using, Repeat from Go To Next Error. 5 minutes even for a large codebase. Writing your post lasted longer. – Sefe Jan 10 '17 at 08:59
  • On how many methods approximately are you planning to apply `AggressiveInlining` ? Do you want to apply it on all methods? How many source files are there approximately? Yes there are several ways to do that, but you may not be taking the best approach to improve performance (if that is you intention). Have you determined the performance bottlenecks in your code? – Hadi Brais Jan 10 '17 at 15:08
  • @HadiBrais Not many, and the number of files it's actually used in is very few (a couple of libraries, really). My issue is more that getting at all of the compiler hints/requests is obtuse as hell without adding the `using` statements. They're not needed often enough to add the `using`s to every page (and would be often cleared if you do a mass dependency-stripping), but that doesn't justify making copy-paste from a notepad++ tab a faster access. When else would you ever need an `[AggressiveInlining]` attribute *except* for when you wanted aggressive inlining? – abluejelly Jan 10 '17 at 20:55

1 Answers1

1

You can write a Roslyn-based tool to do that. This enables to apply an attribute with a name of your choice (some short name such as AggInline) and the tool will emit the actual AggressiveInlining attribute and the required using directives. You can see the ImmutableObjectGraph tool as an example on how to do something like that in Roslyn.

Hadi Brais
  • 22,259
  • 3
  • 54
  • 95
  • Expect this to get accepted within a week- I'm gonna wait till I've figured out (something close to) the actual implementation, but this looks like *exactly* what I was hoping for. Thanks. – abluejelly Jan 11 '17 at 16:50