0

Decoding a json response takes a lot of time on my system - Precision 5520 (11th gen I7, 64 GB) with Linux 20.04 LTS under WSL2 under Windows 10 Pro. Here is the relevant part of the code:

def ms_now():
    return int(time.time_ns() / 1000000)

class Timer():
    def __init__(self):
        self.start = ms_now()

    def stop(self):
        return ms_now() - self.start

timer = Timer()
response = client.invoke(
    FunctionName='my-aws-lambda-function',
    Payload=json.dumps({'body': texts}),
)
print("response", timer.stop(), flush=True)
payload_json = response['Payload']
print("payload_json", timer.stop(), flush=True)
result = json.load(payload_json)
print("result", timer.stop(), flush=True)

This code invokes an AWS Lambda function. The response consists of 38,400 floats. Here is an output of one run (the times are in milliseconds):

response 5619
payload_json 5620
result 76314

It takes more than a minute to deserialize that response which is not that large by any means. I ran this same code in the terminal of PythonAnywhere and the deserialization took 100 milliseconds.

I suspect that WSL is the culprit. How do I diagnose and fix this problem?

EDIT

  1. The same script works fine in the Windows host:
response 1934
payload_json 1934
result 3239 
  1. Since people seem to assume that it's my connection that's slow in WSL, note that when I install packages with large dependencies, such as HuggingFace's sentence-transformers, using pip, they are downloaded quite fast.
12
  • 1
    This question is not about computer hardware or software, within the scope defined in the help center. As for the performance gap, probably hardware, or inefficiencies connected to WSL. I would run the same python script natively on Windows to see if there was a difference
    – Ramhound
    Commented Sep 8, 2023 at 11:20
  • As you're using load(), not loads(), the next question is: what kind of object is payload_json? Can you separately measure data = payload_json.read() and json.loads(data)? I'm betting it's the former that will be slow, not the latter. Commented Sep 8, 2023 at 11:21
  • @u1686_grawity You are right. I tried to produce a simple example with json.loads and it was fast. So I guess it's about reading the i/o buffer through WSL2. How do I fix it? Commented Sep 9, 2023 at 18:15
  • The next question is, what kind of buffer? Is it actually streaming the response over the network from AWS? What does repr(data) say about it? Commented Sep 11, 2023 at 8:21
  • I am not sure what you mean about "actually streaming". The code uses boto3 for making request to AWS and receives the response, which was obviously transferred over the network. What is data in my code? Commented Sep 13, 2023 at 10:18

1 Answer 1

0

This problem is discussed in the bug-report Very slow network speed on WSL2 #4901, where no solution was found but many workarounds were listed. Below are some workarounds in reverse order of suggestion. One of them might perhaps pertain to your problem.

User denskh said :

Specifics of my case is slow upload speed on Win11/WSL2 ~3-4Mbps vs. download of ~500Mpbs. Enabling Netword Direct Memory Access on vEthernet (WSL) virtual adapter fixed this issue and brought upload speed to ~500Mpbs. To check current value run:

Get-NetAdapterAdvancedProperty -name "vEthernet (WSL)" -includehidden -DisplayName "Network Direct (RDMA)"

To enable:

Set-NetAdapterAdvancedProperty -name "vEthernet (WSL)" -includehidden -DisplayName "Network Direct (RDMA)" -DisplayValue "Enabled"

User Jean1995 remarked for WiFi :

My current workaround is to disable and re-enable "Allow other network users to connect through this computer's Internet connection" in the "Properties/Sharing" tab of my WLAN connection.

User sruester said :

Fixed it for me (Intel I219-V adapter with Intel drivers installed):

Properties of WSL ethernet adapter
   Configure ...
       Advanced
          "Large Send Offload Version 2 (IPv4)" -> Disabled
          "Large Send Offload Version 2 (IPv6)" -> Disabled

User eugepemi said :

Finally I tried to disable IPv6 from the vEthernet of WSL and in W11 doesn't appears listed in the control panels, reading GitHub I found out that is because is a hidden device, from PowerShell I managed to disable it and right now WSL is has regular network speeds, the command:

Disable-NetAdapterBinding -Name "vEthernet (WSL)" -ComponentID ms_tcpip6 -IncludeHidden

Some more workarounds can be found in the bug-report itself.

5
  • I don't think my connection is slow in WSL. I install large packages using pip and they get downloaded pretty quickly... Commented Sep 17, 2023 at 17:37
  • According to your tests, it's getting the data that's slow, not decoding the JSON. So what is different about the JavaScript from pip? Might it be because it's done by the browser rather than by pip, and then perhaps you could try another browser.
    – harrymc
    Commented Sep 17, 2023 at 17:46
  • Second question : Does this code work fast in the host? If it's equally slow, then WSL is not a factor.
    – harrymc
    Commented Sep 17, 2023 at 18:24
  • It's Python in the terminal, not JavaScript in the browser. Yes, it works fine in the host. Commented Sep 18, 2023 at 9:08
  • It seems that my answer doesn't relate to your problem, but I would like to keep it here and not delete it, as it might help future readers that have a slow WSL network problem.
    – harrymc
    Commented Sep 18, 2023 at 9:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .