Bug 60330 - Process.GetCurrentProcess.MainModule very slow
Summary: Process.GetCurrentProcess.MainModule very slow
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: master
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2017-10-23 17:12 UTC by Zoltan Varga
Modified: 2017-11-17 23:58 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Zoltan Varga 2017-10-23 17:12:14 UTC
using System;
using System.Diagnostics;

namespace slowCurrentProcessMainModulePath
    class MainClass
        public static void Main(string[] args)
            var sw = Stopwatch.StartNew();
            for (int i = 0; i < 100; i++)
                var a = Process.GetCurrentProcess().MainModule.FileName;

There are many problems here:
- MainModule is implemented as returning module[0], so we have to compute information for all modules
- There is a cache for Modules, but Process.GetCurrentProcess () returns a new Process instance every time, so the cache is
  never used.
- ves_icall_System_Diagnostics_Process_GetModules_internal () contains some O(n^2) code, i.e.
mono_w32process_module_get_name () computes the module list again.
Comment 1 Zoltan Varga 2017-10-23 18:42:31 UTC

This will fix this specific issue, but note that the issues in comment 1 are still present.
Comment 2 Zoltan Varga 2017-11-17 23:58:49 UTC
The current fix is good enough.