Skip to content

CultureInfo creation is slower on Linux by 0.1 millisecond compared to Windows.  #67347

@DerFnoeh

Description

@DerFnoeh

Description

When creating a CultureInfo-Object for decimal-Parsing Linux is over 40 times slower compared to windows environments.

The differences can be easily observed by running the same program once inside a windows- and once inside a linux-container.

Example project

Build&Run the dockerfile. It will output the total time in milliseconds.
https://github.com/DerFnoeh/PerformanceDifferencesCultureInfo

Configuration

Laptop running windows and linux container

  • .NET6
  • DockerDesktop on Windows 10 WSL2
  • Image-Base: mcr.microsoft.com/dotnet/aspnet:6.0
  • VS2022
  • 32GB RAM
  • i7 10750H

Performance was also affected on:

  • Native Ubuntu 20.04, AMD Ryzen x64
  • Azure AppService (Linux-Container)
  • Various other laptops with similiar setups

Regression?

Similar results in .NET5

Data

Benchmark Result

300ms on Windows
13000ms on Linux

Trace-Data of slow-execution:

Most CPU Time was spent in the following methods.

Function Name Total CPU [unit, %] Self CPU [unit, %] Module
| - System.Globalization.CultureInfo.get_NumberFormat() 453550 (60,22 %) 4 (0,00 %) System.Private.CoreLib.dll
| - System.Decimal.TryParse(class System.String,value class System.Globalization.NumberStyles,class System.IFormatProvider,value class System.Decimal&) 453378 (60,20 %) 3 (0,00 %) System.Private.CoreLib.dll
| - System.Globalization.NumberFormatInfo..ctor(class System.Globalization.CultureData) 453301 (60,19 %) 2 (0,00 %) System.Private.CoreLib.dll
| - System.Globalization.CultureData.GetNFIValues(class System.Globalization.NumberFormatInfo) 453261 (60,18 %) 19 (0,00 %) System.Private.CoreLib.dll

Analysis

I spent the whole day investigating our larger-code-base to isolate this issue. Unfortunately I am not versed in the deeper details of OS-interfacing and can't provide any further insights.

Workaround

Instead of instantiating CultureInfo every time it is needed, we setup a static readonly variable for it. This removed all performance drawbacks.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions