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

.NET 3.5 via MonoMod.Backports #540

Merged
merged 13 commits into from
May 17, 2024
Merged

.NET 3.5 via MonoMod.Backports #540

merged 13 commits into from
May 17, 2024

Conversation

Washi1337
Copy link
Owner

@Washi1337 Washi1337 commented Mar 17, 2024

  • Adds .NET 3.5 build targets for all main packages.
  • Adds MonoMod.Backports as a conditional package reference for .NET 3.5 build targets.
  • Adds a few extra runtime-abstracting shims to the AsmResolver common package.
  • Adds IInputFile::BaseAddress
  • Removes MemoryMappedDataSource, the new implementation uses UnmanagedDataSource.

Closes #536

@Washi1337 Washi1337 added this to the 6.0.0 milestone Mar 17, 2024
@Washi1337 Washi1337 linked an issue Mar 17, 2024 that may be closed by this pull request
4 tasks
@Washi1337 Washi1337 mentioned this pull request Mar 17, 2024
4 tasks
@nike4613
Copy link

Backports has a few of these shims already, on *Ex suffixed types in the same namespace as the relevant type. (Though I see you're limiting the Backports package ref to net35, so they may not actually be terribly useful...)

@Washi1337
Copy link
Owner Author

Though I see you're limiting the Backports package ref to net35

Yes, I intend to only include MonoMod.Backports when absolutely necessary (i.e., only for the .NET 3.5 target). Using the existing shims for Array and String would therefore not work out.

Comment on lines +27 to +41
/// <summary>
/// Gets a value indicating whether the current runtime runs on the Microsoft Windows operating system.
/// </summary>
public static bool IsRunningOnWindows
{
get;
}

/// <summary>
/// Gets a value indicating whether the current runtime runs on a Unix-based operating system.
/// </summary>
public static bool IsRunningOnUnix
{
get;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's more efficient to not cache the value because then the JIT can optimize branching based on runtime information.

Choose a reason for hiding this comment

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

As long as the static constructor has run when the JIT compiles a method using it, it will see this as a constant (because it gets emitted as a static readonly field, through a very small method body).

Copy link
Owner Author

Choose a reason for hiding this comment

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

I'm pretty sure it will also depend on whether the calls to into RuntimeInformation are considered JIT intrinsics, which I am not sure about.

Comment on lines +10 to +24
/// <summary>
/// Obtains a singleton instance of an empty array of the provided type.
/// </summary>
/// <typeparam name="T">The type to get the empty array for.</typeparam>
/// <returns>The empty array.</returns>
#if !NET35
public static T[] Empty<T>() => Array.Empty<T>();
#else
public static T[] Empty<T>() => ArrayHelper<T>.Empty;

private static class ArrayHelper<T>
{
public static readonly T[] Empty = new T[0];
}
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

Collection expressions might be supported on NET35. If so, I think you could use [] everywhere instead of having a shim.

Choose a reason for hiding this comment

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

I think that when Array.Empty<T>() is not available, [] will always construct a new array. Probably worth double checking though.

Copy link
Owner Author

Choose a reason for hiding this comment

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

This is indeed the case. On .NET 3.5 the following C#

public static int[] ReturnEmpty() => [];

results in the following CIL, creating a new empty array on every call.

.method public static hidebysig int32[] ReturnEmpty() cil managed
{
    .maxstack 8

    IL_0000: ldc.i4.0
    IL_0001: newarr valuetype [mscorlib] System.Int32
    IL_0006: ret
}

@Washi1337 Washi1337 merged commit a640a82 into development May 17, 2024
1 check passed
@Washi1337 Washi1337 deleted the feature/net35-monomod branch May 17, 2024 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add .NET 3.5 Targets
4 participants