Optimizing passing data between classes for processing in simulation, C#

I’m working on a simulation that will be running a certain operation upwards of hundreds of millions of times per frame, so performance in this loop is critical. I’m nearly at my performance target but I would like advice on if this is the best way, or there’s something I’m overlooking.

The general set up is a large pool of objects that interact with each other, each consists of an equation set from another class as a Func<T,T>, and takes an arbitrary number of inputs in the form of binary bits passed around as an int. It’s a large looping network that iterates every frame, very similar to how computers function, or a binary neural net? The logic in each object can be expected to be somewhat random, but most will only have 1-2 bits of input/output, however some will use the full 32. Some will also require a memory as well, fitting within an int. The logic process itself is already well optimized taking up only ~10ms per billion ops for smaller ones, 95% of my slowdown seems to come from packing/unpacking the signals to perform the operations on it.

I’ve tried many ways of doing this, here’s my current best:

public class Block
{
    public Func<Signal, Signal> logic;
    public int memory;

    // Inputs and Output connections...
    // Get/Sets...

    public Signal Process (int input)
    {
        return logic(new Signal(input, memory));
    }
}

public struct Signal
{
    public int data;
    public int memory;
    
    // Get/Set methods...
    // Constructor...
}

Each Block receives several bit flags from one/multiple inputs, makes a single int out of them, converts that int along with its memory in to a signal struct, and then processes it using its logic Func, and outputs a new Signal struct which gets split in to its outputs. Hundreds of millions of these every frame. The input/output to signal conversion isn’t shown here, but it’s the same process as what happens within the Process() method. I’ve already tested it and it takes the same time on both ends.

Some notes: Removing the memory int from the Signal struct, even if no blocks ever call for it, makes this run 4x as fast, but I’m not sure why the improvement is so drastic. Since most blocks don’t need memory, I tried to instead use interfaces to make multiple Signal types. Signals not needing memory could then run faster, but this made it run around 100x slower, even if only the memory-free Signals were used. Shown below.

public interface Signal
{
    public abstract int GetVal();

    // Rest of the Get/Sets...
}

public struct Signal32 : Signal
{
    private int data;

    public Signal32(int _data)
    {
        data = _data;
    }

    // Get/Sets...
}

I’ve read that using inheritance for structs it’s bad, so that route probably wont work. My limit there is that the Func<T,T> needs access to the memory, but it only can read whatever the input gives it. It can’t access the memory int in the Block since it’s method is created in a different class, so that route wont work. If you know of a way to bypass this, that would be great.

Any feedback would be appreciated here. My current code works okay enough, but I’d definitely prefer it to be faster.

New contributor

Staik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật