Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3401488
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:01:20+00:00 2026-05-18T05:01:20+00:00

I am programming some ‘openvpn-like’ thing and thought it would be a good candidate

  • 0

I am programming some ‘openvpn-like’ thing and thought it would be a good candidate to improve my Haskell knowledge. However, I ran into quite severe performance problems.

What it does: It opens a TUN device; it binds itself on an UDP port, starts 2 threads (forkIO, however compiled with -threaded because of the fdRead). I have not used the tuntap package and did it myself completely in Haskell.

thread 1: read a packet (fdRead) from a tun device. Send it using UDP socket.
thread 2: read a packet (recv) from an UDP socket; send it to tun device (fdWrite)

Problem 1: In this configuration fdRead returns String and I have used the Network.Socket functions that accept String. I made a configuration on local system (some iptables magic) and I can run 15MB/s through it on localhost, the program run basically on 100% CPU. That’s slow. Is there anything I could do to improve the performance?

Problem 2: I will have to prepend something to the packets I am sending; however the sendMany network function takes only ByteString; reading from Fd returns String. Conversion is pretty slow. Converting to Handle doesn’t seem to work well enough with the TUN device….

Problem 3: I wanted to store some information in Data.Heap (functional heap) (I need to use the ‘takeMin’ and although for 3 items it is overkill, it is easy to do 🙂 ). So I created an MVar and on each received packet I’ve pulled the Heap from the MVar, updated the Heap with new info and put it back inito the MVar Now the thing simply starts to eat A LOT of memory. Probably because the old heaps don’t get garbage collected soon/frequently enough..?

Is there a way to solve these problems or do I have to get back to C…? What I am doing should be mostly zerocopy operation – am I using wrong libraries to achieve it?

==================

What I did:
– when putting to MVar, did:

a `seq` putMVar mvar a

That perfectly helped with the memory leak.

  • changed to ByteString; now I get 42MB/s when using just ‘read/write’ with no further processing. The C version does about 56MB/s so this is acceptable.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:01:20+00:00Added an answer on May 18, 2026 at 5:01 am

    String is slow. Really, really, really slow. It’s a singly-linked list of cons cells containing one unicode character each. Writing one to a socket requires converting each character to bytes, copying those bytes into an array, and handing that array to the system call. What part of this sounds like what you want to be doing? 🙂

    You want to be using ByteString exclusively. The ByteString IO functions actually use zero-copy IO where possible. Especially look at the network-bytestring package on hackage. It contains versions of all the network libraries that are optimized to work efficiently with ByteString.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some programming languages (like haskell ) allow cyclic dependencies between modules. Since the compiler
My brother would like to learn some programming to find out if he'd enjoy
In some programming contests, problems have a memory limit (like 64MB or 256MB). How
I'm learning Haskell and solving some programming problems on spoj.pl. The idea of the
In some programming API's I see a list of methods to call, like getBoolean(String
Could people please rank some programming languages like C,C++,Java,perl etc on benchmark line of
I came across a good question while preparing for some programming interviews. Given a
I have some knowledge in C/C++ but only using the Console. I'd like to
Hi programming some C# in a form with a bunch of group boxes. I
I was programming some stuff in a Xcode project, then I decided to move

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.