r_cpp | Unsorted

Telegram-канал r_cpp - C++ - Reddit

-

Stay up-to-date with everything C++! Content directly fetched from the subreddit just for you. Join our group for discussions : @programminginc Powered by : @r_channels

Subscribe to a channel

C++ - Reddit

Benchmarking Eight Serialization Formats in C and C++ (JSON, BSON, CBOR, flexbuffers, msgpack, TOML, XML, YAML)

In C++ development, the choice of serialization format can significantly impact performance . I have recently conducted a comprehensive benchmark to evaluate the performance of eight serialization formats.

Formats and Libraries Used:

JSON: yyjson

BSON: libbson

CBOR: tinycbor

flexbuffers: flatbuffers

msgpack: msgpack-c

TOML: toml++

XML: pugixml

YAML: yaml-cpp

If anyone can recommend better libraries for any of those formats, let me know.

Datasets Used:

canada: Heavy on doubles with few field names.

licenses: Highly nested with many strings.

person: Small, designed to test overhead independent of file size.

Again, suggestions for additional public domain datasets are welcome.

Source Code:

The source code for the benchmarks can be found here: https://github.com/getml/reflect-cpp/tree/main/benchmarks/all

Further Information:

The benchmarks shown here were conducted using gcc 11.4 on Ubuntu 22.4. We have Github Actions pipelines set up that automatically run these benchmarks on different compilers and platforms, please refer to the link above.

I have also tested whether performance can be improved by removing the field names. This makes sense for some binary formats for which readability is not a major concern.

Performance Highlights:

Canada Dataset:

Reading:

Fastest: flexbuffers (\~0.732 ms)

Slowest: YAML (\~365.87 ms)

Writing:

Fastest: msgpack (\~0.53 ms)

Slowest: YAML (\~79.68 ms)

Licenses Dataset:

Reading:

Fastest: msgpack (\~0.062 ms)

Slowest: YAML (\~4.448 ms)

Writing:

Fastest: msgpack (0.013 ms)

Slowest: YAML (\~3.281 ms)

Person Dataset:

Reading:

Fastest: flexbuffers (\~0.000405 ms)

Slowest: YAML (\~0.047839 ms)

Writing:

Fastest: msgpack (\~0.000168 ms)

Slowest: YAML (\~0.02871 ms)

Detailed Observations:

Flexbuffers and msgpack emerged as consistent performers, excelling in both reading and writing operations across all datasets. Msgpack was consistently the fastest format for writing. I should also add that whenever communication with other programming languages is a major concern, you should probably go for msgpack rather than flexbuffers, as the quality and speed of packages in other programming languages is better for msgpack than for flatbuffers/flexbuffers.

YAML consistently showed the slowest performance across all datasets, both in reading and writing. This indicates a substantial overhead, making it less suitable for performance-critical applications.

JSON, when using yyjson (the fastest C library for JSON), offered competitive performance. However, it’s important to note that using other JSON libraries like nlohmann/json could result in significantly slower performance (up to 10x slower).

Conclusion:

Choosing the right serialization format can drastically affect your application's performance. For scenarios requiring fast serialization and deserialization, flexbuffers and msgpack are highly recommended.

Full Results:

I cannot post images on this subreddit, but plots depicting the full results can be found here:

https://github.com/getml/reflect-cpp/tree/main/benchmarks

If you want to see the raw results in CSV form, so you can maybe plot them yourself, here you

Читать полностью…

C++ - Reddit

Multi-threaded C++ Operating Environment for Dynamic Neurons using a Common World View
https://xepl.com

https://redd.it/1drpwks
@r_cpp

Читать полностью…

C++ - Reddit

What's the best way to have aligned storage for an object so you can do placement new (and do explicit d'tor calls later)?

I found this simple thing to be incredibly full of confusion and potential UB bombs. I finally settled on:

// as a class member...
alignas(T) std::byte buffer[sizeof(T)];

// ...
new (buffer) T(arg1, arg2);

// and to "get" a T * ...
T *t = std::launder(reinterpret_cast<T *>(buffer));
// and to end its lifetime...
t->~T();

But this simple thing seemed to be full of confusion. Note that there is this useless class called `std::aligned_storage` which is worthless. And there was disagreement about whether `std::launder` is needed here but my reading of the paper that introduced it and stuff online indicates it is needed.

Such confusion for such a basic thing!

https://redd.it/1dree7m
@r_cpp

Читать полностью…

C++ - Reddit

Should CPlusPlus.com update there site to current C++ standards?



View Poll

https://redd.it/1dr9weh
@r_cpp

Читать полностью…

C++ - Reddit

Latency-Sensitive Application and the Memory Subsystem Part 2: Memory Management Mechanisms - Johnny's Software Lab
https://johnnysswlab.com/latency-sensitive-application-and-the-memory-subsystem-part-2-memory-management-mechanisms

https://redd.it/1dr14mj
@r_cpp

Читать полностью…

C++ - Reddit

Why std::fstream::write() modifies tellg()?

Executing the following code

#include <iostream>
#include <fstream>

int main() {
std::fstream fs{"/dev/shm/test.bin", std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc};
std::cerr << "open: "<< fs.isopen()
<< " good: " << fs.good()
<< " tellg: "<< fs.tellg()
<< " tellp: " << fs.tellp()
<< std::endl;

// writes "12" in binary
fs.write("123", 2);

std::cerr << "open: "<< fs.is
open()
<< " good: " << fs.good()
<< " tellg: "<< fs.tellg()
<< " tellp: " << fs.tellp()
<< std::endl;

return 0;
}

Prints

open: 1 good: 1 tellg: 0 tellp: 0
open: 1 good: 1 tellg: 2 tellp: 2

Why does tellg() change? Shouldn't tellg still be 0 since I have not performed any reads?

https://redd.it/1dqyl7n
@r_cpp

Читать полностью…

C++ - Reddit

CppCast: libunifex and std::execution
https://cppcast.com/libunifex_and_stdexecution/

https://redd.it/1dqnjrs
@r_cpp

Читать полностью…

C++ - Reddit

C++ modules discussion. Ask questions and share your best practices, things to avoid, workarounds etc.

It's been month or two since last 'big' C++ module thread, so I thought I would make one. Post anything modules related really. I'll start with couple MSVC tricks that make working with modules more bearable. Followed by a minor complain about incoming CMake import std support.

# Intellisense auto complete workaround:

Intellisense really struggles to properly parse C++ modules, but recently I learned that it's because Intellisense doesn't properly understand module partitions. If your module is a single interface file that doesn't import module partitions then Intellisense is able to provide full auto complete support.

# MSVC slow Go To Definition workaround:

Related to above workaround, if you Ctrl+Click imported typename or use Go To Definition feature (F12) it can take up to 10 seconds if it even works at all. However once again if your module is single interface file, then MSVC is able to navigate you to proper place in code instantly like it did before modules.

# Workaround for generic std namespace related MSVC compile errors:

A lot of the MSVC C++ module compile errors and even internal compiler errors can be avoided by manually including offending standard headers in files where compile errors occur. Sounds silly, but it works. For instance if MSVC is complaining about double definition of std::array or it's complaining about std::array not being defined, then you can most likely fix the error by manually including <array> header. If compiler is complaining about sv string literal, then include <string_view> etc.

# About incoming CMake import std support:

CMake 3.30 will bring support for import std. I have experimented on cmake 3.30.0-rc1 and import std is working fine on MSVC. However Intellisense auto complete breaks and you won't get auto complete for std namespace. MSVC is only able to provide auto complete for std::align_val_t and std::nullptr_t, which isn't helpful at all. Because of this I will just keep using standard includes until this problem is fixed.

https://redd.it/1dqhlh2
@r_cpp

Читать полностью…

C++ - Reddit

What's with all the invisible comments in this sub?

E.G.

https://imgur.com/a/comment-s-never-show-up-after-submitting-SYi8EfU

taken from here: https://old.reddit.com/r/cpp/comments/1dq82dc/didijustoptimizeloadtimesforgodotreducing/?sort=new

Are the mods just quietly shadow banning everything? wtF is going on here ;D

TA!

https://redd.it/1dq911s
@r_cpp

Читать полностью…

C++ - Reddit

Add calculated unicode character to ostream

Given some integer determined at runtime, say 2654, how can I add the corresponding unicode character \u2654 to ostream?

I’ve messed around with a few things but everything I’ve tried fails with “incomplete universal character name \u” which I assume is happening because the integer has not yet been appended

https://redd.it/1dpvctn
@r_cpp

Читать полностью…

C++ - Reddit

Will you use C++26 rcu library?



View Poll

https://redd.it/1dppjkk
@r_cpp

Читать полностью…

C++ - Reddit

Lifting the Pipes - Beyond Sender/Receiver and Expected Outcome
https://youtu.be/B5uNxPe-MVQ

https://redd.it/1dpnoso
@r_cpp

Читать полностью…

C++ - Reddit

Did I make a bad decision learning C++ as my first language? Looking for advice for my next steps...

Disclaimer: Sorry if my English is improper at times.

I've been learning C++ on and off for a long while alongside my school now, but after reaching an intermediate level, I am confused as to what to do next ? I mean sure, I can learn a library like QT and make some applications but couldn't have I also done that with much more ease had I used a language like python or some other high-level language? same goes for web development, there are many other better alternatives for it than C++, I guess the point I am trying to make is, why would anyone learn C++ as their first language? From a solo developer's standpoint, isn't it a pain to do pretty much anything in C++? I mean sure you can write some seriously fast programs, but does it even matter for solo projects? It seems to me like for a self-taught person I made a very bad choice and should have gone with something like python or java.

The whole reason I learnt C++ as my first language is firstly, because I thought C++ is C part 2 and skipped my C Udemy course for C++, but more importantly, when I did start learning it, I enjoyed it, that was the main reason, I just enjoyed the power the language gave me, and it was interesting to learn new concepts and practice new language features I learnt. But having fun isn't everything, It would also be nice to know how to use this knowledge, so I would like to ask you guys, what do you advise me to do ? Do you think I should learn a framework like QT ? or should I learn other languages now while keeping in touch with C++ ? also if C++ is so difficult then where does one get to reap the rewards of putting in extra effort to learn this language ? Perhaps it'll help me in job hunting ? idrk.

I am sorry if I sound really stupid, I am genuinely looking for some advice here that I can follow.

Thanks.



https://redd.it/1dpfyia
@r_cpp

Читать полностью…

C++ - Reddit

intro article on cache coherency and its effect supported with cpp src repo

written an intro (suitable for undergraduates) article on cache coherency in particular how size and access order affects latency

https://www.seanbutler.net/2024/03/27/size-speed-and-caches.html

its supported by a repo with code that demonstrates the issues

https://github.com/seanbutler/cache-speed-tests



https://redd.it/1dp2hr9
@r_cpp

Читать полностью…

C++ - Reddit

mp-units 2.2.0 released! - mp-units
https://mpusz.github.io/mp-units/latest/blog/2024/06/14/mp-units-220-released/

https://redd.it/1dp1bg0
@r_cpp

Читать полностью…

C++ - Reddit

C++26 new features

https://en.cppreference.com/w/cpp/compiler\_support/26

https://redd.it/1drvko8
@r_cpp

Читать полностью…

C++ - Reddit

What are some good open source projects that are active that use C++ and I can contribute?

Hi. I'm wondering if someone with knowledge of the open source community knows of any projects that I can contribute to using C or C++ (C++ preferred). I am looking but I'm not always confident in the projects I am finding and would love it if someone could help me out. Thanks and have a great day!

https://redd.it/1drkkcg
@r_cpp

Читать полностью…

C++ - Reddit

Which ide/editor you use for c++ in Linux

Hey guys
I am trying to switch to Ubuntu
But I can't find a way to compile my cpp code
In windows I used Visual Studio
but there is no visual studio in linux
And I need to import external libs for my projects because I am learning Opengl So VScode doesn't work well
Can you help me

https://redd.it/1drb8ca
@r_cpp

Читать полностью…

C++ - Reddit

Trying to use C++ on MAc for the 1st time.

I have configured VS code for C++ 20 with clang version 18. But still I have to compile the code form the terminal using "clang++ -std=c++20 hardware_info.cpp -o hardware" command an then run the object file.

So, I saw that there are other options for IDE like CLion and XCode, but are they better than VS code provided the problem above.

https://redd.it/1dr8fk0
@r_cpp

Читать полностью…

C++ - Reddit

What are the resources you used to keep updated with C++?



https://redd.it/1dqzs48
@r_cpp

Читать полностью…

C++ - Reddit

A recursive division benchmark among different systems programming languages (C vs. C++ vs. Rust vs. Zig)
https://github.com/jorgeperezlara/Benchmarks

https://redd.it/1dqyemm
@r_cpp

Читать полностью…

C++ - Reddit

How I use unions in C++. An implementation of a full adder circuit.

This class implements a [full adder circuit](https://en.wikipedia.org/wiki/Adder_(electronics)). It uses unions to mask a struct of unsigned bits with a byte (uint8\_t).

class CByte
{
struct _Byte
{
unsigned B1 : 1;
unsigned B2 : 1;
unsigned B3 : 1;
unsigned B4 : 1;
unsigned B5 : 1;
unsigned B6 : 1;
unsigned B7 : 1;
unsigned B8 : 1;
};

struct _Carry
{
unsigned C0 : 1;
unsigned C1 : 1;
unsigned C2 : 1;
unsigned C3 : 1;
unsigned C4 : 1;
unsigned C5 : 1;
unsigned C6 : 1;
unsigned C7 : 1;
};

union BYTE
{
struct _Byte B;
uint8_t U;
};

union CARRY
{
struct _Carry C;
uint8_t U;
};

public:
friend class CNumber;

CByte()
{
m_b.U = 0;
m_c.U = 0;
}

CByte(uint8_t byte)
{
m_b.U = byte;
m_c.U = 0;
};

CByte(const CByte& rhs)
{
*this = rhs;
}

CByte& operator = (const CByte& rhs)
{
if (this != &rhs)
{
m_b = rhs.m_b;
m_c = rhs.m_c;
}
return *this;
}

CByte& operator + (const CByte& rhs)
{
CByte Out;

Out.m_b.B.B1 = m_c.C.C0 ^ (m_b.B.B1 ^ rhs.m_b.B.B1);
m_c.C.C1 = (m_b.B.B1 & rhs.m_b.B.B1) | (rhs.m_b.B.B1 & m_c.C.C0) | (m_b.B.B1 & m_c.C.C0);

Out.m_b.B.B2 = m_c.C.C1 ^ (m_b.B.B2 ^ rhs.m_b.B.B2);
m_c.C.C2 = (m_b.B.B2 & rhs.m_b.B.B2) | (rhs.m_b.B.B2 & m_c.C.C1) | (m_b.B.B2 & m_c.C.C1);

Out.m_b.B.B3 = m_c.C.C2 ^ (m_b.B.B3 ^ rhs.m_b.B.B3);
m_c.C.C3 = (m_b.B.B3 & rhs.m_b.B.B3) | (rhs.m_b.B.B3 & m_c.C.C2) | (m_b.B.B3 & m_c.C.C2);

Out.m_b.B.B4 = m_c.C.C3 ^ (m_b.B.B4 ^ rhs.m_b.B.B4);
m_c.C.C4 = (m_b.B.B4 & rhs.m_b.B.B4) | (rhs.m_b.B.B4 & m_c.C.C3) | (m_b.B.B4 & m_c.C.C3);

Out.m_b.B.B5 = m_c.C.C4 ^ (m_b.B.B5 ^ rhs.m_b.B.B5);
m_c.C.C5 = (m_b.B.B5 & rhs.m_b.B.B5) | (rhs.m_b.B.B5 & m_c.C.C4) | (m_b.B.B5 & m_c.C.C4);

Out.m_b.B.B6 = m_c.C.C5 ^ (m_b.B.B6 ^ rhs.m_b.B.B6);
m_c.C.C6 = (m_b.B.B6 & rhs.m_b.B.B6) | (rhs.m_b.B.B6 & m_c.C.C5) | (m_b.B.B6 & m_c.C.C5);

Out.m_b.B.B7 = m_c.C.C6 ^ (m_b.B.B7 ^ rhs.m_b.B.B7);
m_c.C.C7 = (m_b.B.B7 & rhs.m_b.B.B7) | (rhs.m_b.B.B7 & m_c.C.C6) | (m_b.B.B7 & m_c.C.C6);

Out.m_b.B.B8 = m_c.C.C7 ^ (m_b.B.B8 ^ rhs.m_b.B.B8);
Out.m_c.C.C0 = (m_b.B.B8 & rhs.m_b.B.B8) | (rhs.m_b.B.B8 & m_c.C.C7) | (m_b.B.B8 & m_c.C.C7);

*this = Out;

return *this;
}

protected:
BYTE m_b;
CARRY m_c;
};

https://redd.it/1dqkf7r
@r_cpp

Читать полностью…

C++ - Reddit

Which advisor to choose for a career in HFT?

Hi everyone,

I am just starting my Masters degree and I want to make a career in high frequency trading. I know that HFT is heavily based on both C++ and FPGA. I have two professors to work under, and one of them works in FPGA development and the other in C++ optimization and works with template metaprogramming etc. I would like to know which one would be better to choose and do HFT engineers need to know both FPGA programming (such as HDLs) and C++ well, or do FPGA engineers work on a subset of HFT that is different from the C++ developers and if so, how do their work branch out i.e what are the skills that each job requires to know. Thank you.

https://redd.it/1dqbyry
@r_cpp

Читать полностью…

C++ - Reddit

Did I just optimize load times for Godot, reducing them by ~75%? Yes, but how? I don't know.

Watch instead? https://youtu.be/JPQZVXzjmUk

I have been messing with Godot source this week and looking at their issues. Someone reported a crash when loading an absurdly large (788 MB) file. I tested it and had a pretty long hang before it loaded. Cool, I have written an obj loader before let's see if I can make this any faster.

Perf testing showed a massive time sink in a function called rankEdgeCollapses (or really the two overloaded functions it calls: quadricError.)
quadricError has lots of room for optimization as far as Godot is concerned. There is a branch that can be removed and a for loop that can be unrolled. On it. Once that is done there are several floats that can be removed and just calculated the one time they're referenced.

While I was at it, I just happened to change 1 line of code. The original for loop in randEdgeCollapses looked like this:

for (size_t i = 0; i < collapse_count; ++i) {

Collapse& c = collapses[i];

Fine but I made it like this:

Collapse *c = &collapses[0];

for (size_t i = 0; i < collapse_count; ++i) {

c = &collapses[i];

Once I tested all of these changes in a release build, the results were amazing. We went from an \~19 second load time of a 34MB mesh to 4.8 seconds! This is using Godots --verbose option.

But, something bugged me about that specific reference change to a pointer. (I had originally borked it and made it a reference that gets reset ever iteration. Anyway, enough about dumb things I did.)

How much perf came from that reference change? I didn't know. I can imagine using a pointer as an iterator as we commonly do but a reference to each iteration? I'm unsure.

That change accounted for more than 90% of the gains. Not loop unrolling, inline calculations or removal of unnecessary variables in a loop that will run a half-million times. None of that mattered as much as this reference change to a pointer. Freakin' why? How?

https://redd.it/1dq82dc
@r_cpp

Читать полностью…

C++ - Reddit

c++ problem

i have a bridge code that makes if racer reached a specific index he will go upward from his location to another ( that i will input )

and i have an obstacles that i choose in which index they are so if any of racers reached these indecis either he will go back 3 steps or remain in the same location until the round end

i have a track and i want to make these two operations above to happen on this track how can i do that?

https://redd.it/1dpt8yc
@r_cpp

Читать полностью…

C++ - Reddit

Default member initializer compiler consistency shootout

https://cpp.godbolt.org/z/bMvdzz86c

https://redd.it/1dpnmt9
@r_cpp

Читать полностью…

C++ - Reddit

Want to write my first Compiler

I got a spiked interest in compilers, and I want to make my own for a scripting language and I don't know if I should use C++ or use other languages like Ocaml or Rust, and if am bootstrapping does it even matter what language I use ?

Note: I wrote this question here because I code in C++

https://redd.it/1dpkp2p
@r_cpp

Читать полностью…

C++ - Reddit

Can someone explain me how to add GLFW in a clean new project?

Hi!
I've been learning Cpp for a little while now, and I want to import libraires and such. But I haven't been able to import and use anything so far... I downloaded the binaries for GLFW, copied the include folder into my CLion project's root folder. And then what? I'm supposed to link it, but HOW??? There's no help whatsoever, everyone on every tutorial just acts like it's already done and it's magic or you already OBVIOUSLY know how to do it. So far my CMakeLists.txt looks like that...

cmake_minimum_required(VERSION 3.0)
project(LearnOpenGL)

set(CMAKE_CXX_STANDARD 23)

find_package(GLFW REQUIRED)

add_executable(LearnOpenGL main.cpp)

include_directories(./include)
target_link_libraries("${CMAKE_PROJECT_NAME}" PRIVATE glfw)

Project structure is something like:

Root
|-- include
| |-- GLFW
| | |-- glfw3.h
| | |-- glfwnative.h
|-- CMakeLists.txt
|-- main.cpp

I have no idea what I'm doing, can someone just help me 😭

https://redd.it/1dpdkpi
@r_cpp

Читать полностью…

C++ - Reddit

C++ Interview - How to Prepare?

I had a first round which was to implement a shared pointer in C++. The second round should be like first round as in a practical C++ question, not a leetcode type of question. I have only been prepping leetcode so now im a little nervous, what should i do to prepare?

https://redd.it/1dp445o
@r_cpp

Читать полностью…

C++ - Reddit

How to use -finstrument-functions flag for code profiling

I'm working on a huge codebase containg both c and cpp files. It is a multi-process and multi-threaded project.

Requirement is: I want to dump the function calls along with the function parameters and return values.

Currently after defining the cyg_profile_func_enter/exit, I'm able to dump the hex values, which I'll pass to a script and run addr2line to get the exact function name.

But I'm not able to get the function's parameters and return values.
Also, the debug dump that I'm getting, the standard libraries's functions are also present.

Please suggest how can I achieve this, or if there is a better method that you can think of.

https://redd.it/1dp11gr
@r_cpp

Читать полностью…
Subscribe to a channel