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 6735321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:59:43+00:00 2026-05-26T10:59:43+00:00

I’m trying to read multiple files that have been serialized with ProtoBuf.NET using .NET

  • 0

I’m trying to read multiple files that have been serialized with ProtoBuf.NET using .NET Tasks like this:

public static ResultsDump Amalgamate(RuntimeTypeModel model, IEnumerable<string> files)
{      
  var readDumpTasks = 
    files.Select(fn =>
      Task<ResultsDump>.Factory.StartNew(() => {
        try {
          using (var dumpFile = new FileStream(fn, FileMode.Open))
          {
            var miniDump = (ResultsDump)model.Deserialize(dumpFile, null, typeof(ResultsDump));
            if (miniDump == null) {
              throw new Exception(string.Format("Failed to deserialize dump file {0}", fn));
            }
            //readDumps.Add(miniDump);
            return miniDump;
          }
        }
        catch (Exception e) {
          throw new Exception(string.Format("cannot read dump file {0}: {1}", fn, e.Message), e);
        }
      })).ToArray();

  Task.WaitAll(readDumpTasks);

  var allDumps = readDumpTasks.Select(t => t.Result).ToList();

  // Goes on.. irrelevant to the question
}

For some reason, CPU usage doesn’t really go above a single core.
Is there something inherent lock in Protobuf.NET that doesn’t like desrializing multiple file concurrently?

I’ve tried this with multiple RuntimeTypeModel instances as well as one, and it always seems to peak at a very “low” CPU usage level..

Am I even wrong to be blaming ProtoBuf.NET? Is this the .NET memory allocator / TPL?

  • 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-26T10:59:43+00:00Added an answer on May 26, 2026 at 10:59 am

    There is intentionally very limited locking in protobuf-net; it only really locks while checking the types (first run) to see what is needed. Once the model is understood, it is lock-free, and it is designed to be trivially parallel.

    As noted (comments) it is extremely likely that IO is your bottleneck. Indeed, parallelising access to the same physical disk / spindle will usually greatly reduce throughput, as the buffer is more contended and it has to do more seeking rather than contiguous reading.

    This should be easy to test / validate: for a test run, instead of reading from disk, load them all into memory first;

    var ms = new MemoryStream(
        File.ReadAllBytes(path));
    

    With all the files loaded, now do the same code but passing the MemoryStreams in as input. If it still doesn’t scale, it might be a bug. I strogly suspect, however, that you will find it parallelises nicely at that point.


    Here’s a worked example, which for me saturates all my cores with concurrent deserialization:

    using System.Collections.Generic;
    using ProtoBuf;
    using System;
    using System.IO;
    using System.Threading.Tasks;
    
    
    internal class Program
    {
        private static void Main()
        {
            var foo = new Foo { Bars = new List<Bar>() };
            var rand = new Random(1234);
            for (int i = 0; i < 1000; i++)
            {
                var bar = new Bar
                {
                    A = rand.Next(),
                    B = rand.Next(),
                    C = rand.Next(),
                    D = rand.Next(),
                    E = rand.Next(),
                    F = rand.Next(),
                    G = rand.Next(),
                    H = rand.Next()
                };
                foo.Bars.Add(bar);
            }
            var ms = new MemoryStream();
            Serializer.Serialize(ms, foo);
            var bytes = ms.ToArray();
            const int count = 100000;
            Parallel.For(0, count, x =>
            {
                Serializer.Deserialize<Foo>(new MemoryStream(bytes));
            });
        }
    }
    [ProtoContract]
    internal class Foo
    {
        [ProtoMember(1)]
        public List<Bar> Bars { get; set; }
    }
    [ProtoContract]
    internal class Bar
    {
        [ProtoMember(1)]
        public int A { get; set; }
        [ProtoMember(2)]
        public int B { get; set; }
        [ProtoMember(3)]
        public int C { get; set; }
        [ProtoMember(4)]
        public int D { get; set; }
        [ProtoMember(5)]
        public int E { get; set; }
        [ProtoMember(6)]
        public int F { get; set; }
        [ProtoMember(7)]
        public int G { get; set; }
        [ProtoMember(8)]
        public int H { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.