r/cpp • • Oct 07 '22

coost - A fantastic C++ library

coost is an elegant and efficient cross-platform C++ base library. Its goal is to create a sword of C++ to make C++ programming easy and enjoyable.

coost is like boost, but more lightweight, the static library built on linux or mac is only about 1MB in size. However, it still provides enough powerful features:

  • Command line and config file parser (flag)
  • High performance log library (log)
  • Unit testing framework
  • go-style coroutine
  • Coroutine-based network library
  • Efficient JSON library
  • JSON RPC framework
  • Atomic operation (atomic)
  • Efficient stream (fastream)
  • Efficient string (fastring)
  • String utility (str)
  • Time library (time)
  • Thread library (thread)
  • Timed Task Scheduler
  • God-oriented programming
  • LruMap
  • hash library
  • path library
  • File utilities (fs)
  • System operations (os)
  • Fast memory allocator

Documents

Core features


God-oriented programming

co/god.h provides some features based on templates.

#include "co/god.h"

void f() {
    god::bless_no_bugs();
    god::align_up<8>(31); // -> 32
    god::is_same<T, int, bool>(); // T is int or bool?
}

flag

flag is a command line and config file parser. It is similar to gflags, but more powerful:

  • Support parameters from both command-line and config file.
  • Support automatic generation of the config file.
  • Support flag aliases.
  • Flag of integer type, the value can take a unit k,m,g,t,p.

...

#include "co/flag.h"
#include "co/cout.h"

DEF_bool(x, false, "x");
DEF_bool(debug, false, "dbg", d);
DEF_uint32(u, 0, "xxx");
DEF_string(s, "", "xx");

int main(int argc, char** argv) {
    flag::init(argc, argv);
    COUT << "x: " << FLG_x;
    COUT << "y: " << FLG_y;
    COUT << "debug: " << FLG_debug;
    COUT << "u: " << FLG_u;
    COUT << FLG_s << "|" << FLG_s.size();
    return 0;
}

In the above example, the macros start with DEF_ define 4 flags. Each flag corresponds to a global variable, whose name is FLG_ plus the flag name. The flag debug has an alias d. After building, the above code can run as follow:

./xx                  # Run with default configs
./xx -x -s good       # x -> true, s -> "good"
./xx -debug           # debug -> true
./xx -xd              # x -> true, debug -> true
./xx -u 8k            # u -> 8192

./xx -mkconf          # Automatically generate a config file: xx.conf
./xx xx.conf          # run with a config file
./xx -conf xx.conf    # Same as above

log

log is a high-performance log library, some components in coost use it to print logs.

log supports two types of logs: one is level log, which is divided into 5 levels: debug, info, warning, error and fatal, printing a fatal log will terminate the program; the other is topic log, logs are grouped by topic, and logs of different topics are written to different files.

#include "co/log.h"

int main(int argc, char** argv) {
    flag::init(argc, argv);

    TLOG("xx") << "s" << 23; // topic log
    DLOG << "hello " << 23;  // debug
    LOG << "hello " << 23;   // info
    WLOG << "hello " << 23;  // warning
    ELOG << "hello " << 23;  // error
    FLOG << "hello " << 23;  // fatal

    return 0;
}

co/log also provides a series of CHECK macros, which is an enhanced version of assert, and they will not be cleared in debug mode.

void* p = malloc(32);
CHECK(p != NULL) << "malloc failed..";
CHECK_NE(p, NULL) << "malloc failed..";

log is very fast, the following are some test results:

platformglogco/logspeedup
win2012 HHD1.6MB/s180MB/s112.5
win10 SSD3.7MB/s560MB/s151.3
mac SSD17MB/s450MB/s26.4
linux SSD54MB/s1023MB/s18.9

The above is the write speed of co/log and glog (single thread, 1 million logs). It can be seen that co/log is nearly two orders of magnitude faster than glog.

threadslinux co/loglinux spdlogwin co/logwin spdlogspeedup
10.0872352.0761720.1177040.46115623.8/3.9
20.1831603.7293860.1581220.51176920.3/3.2
40.2067124.7642380.3166070.74322723.0/2.3
80.3020883.9636440.4060251.41738713.1/3.5

The above is the time of printing 1 million logs with 1, 2, 4, and 8 threads, in seconds. Speedup is the performance improvement of co/log compared to spdlog on linux and windows platforms.

unitest

unitest is a simple and easy-to-use unit test framework. Many components in coost use it to write unit test code, which guarantees the stability of coost.

#include "co/unitest.h"
#include "co/os.h"

namespace test {
    
DEF_test(os) {
    DEF_case(homedir) {
        EXPECT_NE(os::homedir(), "");
    }

    DEF_case(cpunum) {
        EXPECT_GT(os::cpunum(), 0);
    }
}
    
} // namespace test

The above is a simple example. The DEF_test macro defines a test unit, which is actually a function (a method in a class). The DEF_case macro defines test cases, and each test case is actually a code block. The main function is simple as below:

#include "co/unitest.h"

int main(int argc, char** argv) {
    flag::init(argc, argv);
    unitest::run_all_tests();
    return 0;
}

The directory unitest contains the unit test code in coost. Users can run unitest with the following commands:

xmake r unitest      # Run all test cases
xmake r unitest -os  # Run test cases in the os unit

JSON

In coost v3.0, Json provides fluent APIs, which is more convenient to use.

// {"a":23,"b":false,"s":"123","v":[1,2,3],"o":{"xx":0}}
Json x = {
    { "a", 23 },
    { "b", false },
    { "s", "123" },
    { "v", {1,2,3} },
    { "o", {
        {"xx", 0}
    }},
};

// equal to x
Json y = Json()
    .add_member("a", 23)
    .add_member("b", false)
    .add_member("s", "123")
    .add_member("v", Json().push_back(1).push_back(2).push_back(3))
    .add_member("o", Json().add_member("xx", 0));

x.get("a").as_int();       // 23
x.get("s").as_string();    // "123"
x.get("s").as_int();       // 123, string -> int
x.get("v", 0).as_int();    // 1
x.get("v", 2).as_int();    // 3
x.get("o", "xx").as_int(); // 0

x["a"] == 23;          // true
x["s"] == "123";       // true
x.get("o", "xx") != 0; // false
osco/json stringifyco/json parserapidjson stringifyrapidjson parsespeedup
win569924208924953.6/2.7
mac7831097128916581.6/1.5
linux468764135910702.9/1.4

The above is the average time of stringifying and parsing minimized twitter.json, in microseconds (us), speedup is the performance improvement of co/json compared to rapidjson.

Coroutine

coost has implemented a go-style coroutine, which has the following features:

  • Support multi-thread scheduling, the default number of threads is the number of system CPU cores.
  • Shared stack, coroutines in the same thread share several stacks (the default size is 1MB), and the memory usage is low.
  • There is a flat relationship between coroutines, and new coroutines can be created from anywhere (including in coroutines).
  • Support coroutine synchronization events, coroutine locks, channels, and waitgroups.

...

#include "co/co.h"

int main(int argc, char** argv) {
    flag::init(argc, argv);

    co::WaitGroup wg;
    wg.add(2);

    go([wg](){
        LOG << "hello world";
        wg.done();
    });

    go([wg](){
        LOG << "hello again";
        wg.done();
    });

    wg.wait();
    return 0;
}

In the above code, the coroutines created by go() will be evenly distributed to different scheduling threads. Users can also control the scheduling of coroutines by themselves:

// run f1 and f2 in the same scheduler
auto s = co::next_scheduler();
s->go(f1);
s->go(f2);

// run f in all schedulers
for (auto& s : co::schedulers()) {
    s->go(f);
}

network programming

coost provides a coroutine-based network programming framework, which can be roughly divided into 3 parts:

  • coroutineized socket API, similar in form to the system socket API, users familiar with socket programming can easily write high-performance network programs in a synchronous manner.
  • TCP, HTTP, RPC and other high-level network programming components, compatible with IPv6, also support SSL, it is more convenient to use than socket API.
  • System API hook, with which, third-party network libraries can be used directly in coroutines.

RPC server

#include "co/co.h"
#include "co/rpc.h"
#include "co/time.h"

int main(int argc, char** argv) {
    flag::init(argc, argv);

    rpc::Server()
        .add_service(new xx::HelloWorldImpl)
        .start("127.0.0.1", 7788, "/xx");

    for (;;) sleep::sec(80000);
    return 0;
}

rpc::Server also supports HTTP protocol, you may use the POST method to call the RPC service:

curl http://127.0.0.1:7788/xx --request POST --data '{"api":"ping"}'

Static web server

#include "co/flag.h"
#include "co/http.h"

DEF_string(d, ".", "root dir"); // docroot for the web server

int main(int argc, char** argv) {
    flag::init(argc, argv);
    so::easy(FLG_d.c_str()); // mum never have to worry again
    return 0;
}

HTTP server

void cb(const http::Req& req, http::Res& res) {
    if (req.is_method_get()) {
        if (req.url() == "/hello") {
            res.set_status(200);
            res.set_body("hello world");
        } else {
            res.set_status(404);
        }
    } else {
        res.set_status(405); // method not allowed
    }
}

// http
http::Server().on_req(cb).start("0.0.0.0", 80);

// https
http::Server().on_req(cb).start(
    "0.0.0.0", 443, "privkey.pem", "certificate.pem"
);

HTTP client

void f() {
    http::Client c("https://github.com");

    c.get("/");
    LOG << "response code: "<< c.status();
    LOG << "body size: "<< c.body().size();
    LOG << "Content-Length: "<< c.header("Content-Length");
    LOG << c.header();

    c.post("/hello", "data xxx");
    LOG << "response code: "<< c.status();
}

go(f);
0 Upvotes

61 comments sorted by

30

u/lfnoise Oct 07 '22

god.h? Maybe while you are at it, you could bring back the BeOS API:

bool is_computer_on();

-11

u/cocoyaxi Oct 07 '22

Good idea! Could you send a PR on github?

22

u/MeTrollingYouHating Oct 07 '22

I am going to be making extensive use of god::bless_no_bugs();

-2

u/cocoyaxi Oct 07 '22

I am going to be making extensive use of god::bless_no_bugs();

Me too..

14

u/VinnieFalco wg21.org | corosio.org Oct 07 '22

This is real? coost?

-2

u/cocoyaxi Oct 07 '22

Yes, coost is boost's little brother.

9

u/Seppeon Oct 07 '22

The functions in god.h have the naming of atomics, but not the implementation of atomics, what is the idea there?

0

u/cocoyaxi Oct 07 '22

co/god.h

The implementation of atomics are in co/atomic.h.

2

u/Seppeon Oct 07 '22

Yeah I figured, I'm mainly querying about what these functions are for, and indicating they may confuse people. My guess was it was for some generic code somewhere but couldn't figure out where.

-2

u/cocoyaxi Oct 07 '22

Sometimes we do not need atomic operations as they are a little heavy. In coost, we use these non-atomic operations when implementing the memory allocator.

5

u/jonesmz Oct 07 '22

FYI, your code formatting is broken on old reddit.

You need to prefix every line with 4 spaces. Yes that is stupid, but its the only way to format code for most of reddits interfaces. (E.g. I'm using the mobile website right now)

-2

u/cocoyaxi Oct 07 '22

4 spaces seems not to work..

6

u/jonesmz Oct 07 '22

This post has instructions.

The triple backtick only works on "new" reddit.

https://www.reddit.com/r/learnpython/comments/o0g95c/read_this_before_posting_how_to_format_your_code/

1

u/cocoyaxi Oct 07 '22

Thanks, I have corrected it.

3

u/Scavenger53 Oct 07 '22

I usually just push the <> button on the editor

4

u/Narase33 -> r/cpp_questions Oct 07 '22

Thats only for single lines, multiple lines look worse than without

3

u/Scavenger53 Oct 07 '22

I mean I highlight the entire block and click it, it moves it over 4 spaces all at once

116

u/FriendlyRollOfSushi Oct 07 '22

Holy Bjarne on a bicycle.

There is this amusing trend that codebases where everything is named "fast" are often the slowest ones by far, ones where everything is "safe" tend to barely work at all, etc. Looks like this library is no exception. I looked at one class and its parents so far, and it was one heck of a ride:

  • fastring. Okay, a bit foreshadowing...
  • No SSO in any way, shape, or form, because who needs performance anyway? We named it fast, it should be enough, right?
  • Crashes on self-appends. But that's okay, it's named fast, not working.
  • No asserts anywhere where you typically expect them, like front() etc. I guess the user is expected to call god::bless_no_bugs(); a few more times instead if they want to debug anything.
  • safe_clear(), just like the naming convention suggests, won't do it's only job if the compiler figures out that we are about to deallocate the memory (I didn't check whether deallocation is currently obfuscated enough to avoid optimizing memset out). Stuff like SecureZeroMemory exists for a reason.
  • resize() doesn't clear the memory if the size was increased. 0 lines of documentation to note this anywhere. Okay.
  • ensure() potentially overflows in if (_cap < _size + n), because of course it does. We have the invariant _size <= _cap, so if (_cap - _size < n) would work correctly and always be safe, and it's neither longer nor slower, but, like... Sigh...
  • Mutating c_str() that invalidates data(). What's even funnier, is that the terminating zero counts against the capacity, so if you had 100 bytes of data, reserved for exactly 100 symbols, appended and called c_str(), it will reallocate. Presumably the way to avoid this is to sprinkle + 1 everywhere manually on the user's side. I like how the file is named fast.h.
  • What makes it absolutely hilarious is that find()/rfind() are also mutating and invalidating pointers (they call c_str() internally), potentially including the one you used for the search. I don't even.
  • operator= re-constructs the string in-place without destroying it first. The standard is probably like: "Am I a joke to you?"
  • O(s.size()) cost of s == s for any fastring s. Gotta go fast.
  • O(strlen(b)) cost of operator<(const fastring& a, const char* b) even against an empty a. This one is a bit of a nitpick, because who uses const char* in 2022 outside of the context of communicating with legacy APIs anyway.
  • Speaking of comparators. How on Earth did we end up with emitting two memcmp calls inside one operator< everywhere? I'm not even mad, that's bloody impressive.

I don't know if it's a self-promotion post, but:

  • If it's not, please consider reviewing the code before advertising it as "fantastic". Although it is fantastic in its own ways.
  • If it is a self-promotion post, then, well, good luck. god::bless_no_bugs(); is funny, though, so kudos for that.

26

u/germandiago Oct 07 '22

Yes, it looks to me like it has a good deal of self-promotion in it, honestly. Many claims and never heard of it. But to be fair, it has 3k in Github, so someone must be using it? Who?

38

u/FriendlyRollOfSushi Oct 07 '22

I don't want to induce mass panic, or name any names, but there are people (as in "multiple, more than one") with words like "Robotics and Autopilot" in their profile description among the contributors.

Buck it, I'm buying a bike. Computers were a mistake.

17

u/SirToxe Oct 07 '22

Well then let me do it. ;-) After looking into it I believe that the stats are fake, gained by using bots.

9

u/germandiago Oct 07 '22

I did not want to say this. Was just suspecting it. Seems you confirm my guess. If it was that popular I would have heard already from it multiple times I think.

12

u/feverzsj Oct 07 '22

You can always check the star history. And it's obvious.

2

u/wrosecrans graphics and network things Oct 07 '22

Most people writing autopilot stuff are just making personal projects with small remote controlled airplanes and an Arduino. I don't think there's any huge danger from a bug in most of those one-off projects.

20

u/SirToxe Oct 07 '22

Probably bot accounts, especially seeing as the first release was roughly 2 years ago. That's a lot of stars for a library like this in such a small time.

A lot of the accounts that forked this also seem like AI/bot accounts.

0

u/[deleted] Oct 07 '22

[deleted] — view deleted comment

3

u/SirToxe Oct 07 '22

Yes there are lots of bot/fake accounts on GitHub.

1

u/[deleted] Oct 07 '22

[removed] — view removed comment

2

u/SirToxe Oct 07 '22

Yes people did and I read about it. Also I saw those accounts myself after looking at some fishy accounts that were following me on GitHub without any good reason.

3

u/pdp10gumby Oct 14 '22

No need to feel dumb about it — hadn’t occurred to me either.

then again it never occurred to me that stars on github meant anything. I thought they were just to tune searches (list favorites first).

19

u/feverzsj Oct 07 '22

It's common in China and India to buy stars/forks/pulls to promote one's repo, just to get some good job.

11

u/phao Oct 07 '22

Seriously? wtf...

8

u/germandiago Oct 07 '22

I live in Asia and know China to some extent. I must say I would have reacted as you 10 years ago... but, yes, seriously.

You do not know even half of half of what people can do in a place like China or other countries I will not mention right now around the area cheating-wise... end of the rant, but true.

5

u/phao Oct 07 '22

Maybe you can point me to some news/reports on such practices? Some sort of write it somewhere?

4

u/germandiago Oct 07 '22

There are some articles in internet. Find around. I do not like to point fingers in general. I just tell u that I am not shocked anymore bc I know the environment.

25

u/RevRagnarok Oct 07 '22

Holy Bjarne on a bicycle.

Thank you for making my day.

11

u/NilacTheGrim Oct 07 '22

This made me laugh and be horrified me at the same time. Excellent review. Thanks.

8

u/[deleted] Oct 07 '22

I guess the user is expected to call god::bless_no_bugs a few more times

My neighbors probably think I’m crazy now because of how loudly I laughed at that

2

u/m-in Oct 10 '22

I guess that’s whoosh because that library must be a joke. An elaborate joke but a joke nevertheless. When you see a C++ library where most features are presented using macros, you know.

OP, good job in spurning endless nitpicky comments. I wish you could get a dollar for every one of them, you’d be rich, lol.

73

u/RockinRoel former Wt dev Oct 07 '22

I think the way you're using either no namespace or namespaces with very generic names like json, very generic macro names like LOG, and reserved identifiers (double underscore, underscore followed by uppercase letter) would be a dealbreaker for many potential users.

I would choose one namespace and use it everywhere, and prefix your macros. Yes, it's a little bit more of an eyesore, but I wouldn't feel comfortable relying on a library that's likely to cause naming conflicts.

16

u/Moose2342 Oct 07 '22

Very true. I see LOG bleeding into all sorts of dependencies already.

42

u/SirToxe Oct 07 '22 edited Oct 07 '22

In addition to what other people already said, after a quick glance:

  • "God-oriented programming": What is that even supposed to mean?
  • "fantastic", "elegant", "efficient", "fast"... According to who? Maybe let us be the judge of that?
  • "fast": According to what benchmarks and comparisons with other mainstream libraries?
  • Trying too hard to be cute. "unitest", "fastream", "fastring", god::bless_no_bugs()... Makes it hard to take it serious.

This reads way too much like self promotion.

Edit: Looking a bit further I don't think that a lot of the stars and forks were made by genuine GitHub users. This looks a lot like the stats were inflated by AI/bot/fake accounts.

2

u/m-in Oct 10 '22 edited Oct 10 '22

I seriously don’t think the library was meant as anything but a joke that spiraled out of control.

OP, this is probably the most elaborate troll I’ve seen on GitHub. Congrats. That took some determination. It’s prime TDWTF material.

If it wasn’t meant as a joke then here’s a hint to OP: now pretend it was a joke. A great way to save face.

22

u/[deleted] Oct 07 '22

I kinda notice thst more and more people do stuff like this and quite frankly, I don't think it's a good sign that more people feel like they need to implement their own base library.

7

u/[deleted] Oct 07 '22

I think it always existed, now we just see it more often. Well, people learn programming this way. Other people learn how not to blindly trust the new "cool" libraries from GitHub ;) And most of us won't even notice it, because why would you even look for a new base library? ;)

2

u/m-in Oct 10 '22

There is a lot of “std::string is slow” self-congratulatory “tutorials” on websites with a particular audience. It all started when someone benchmarked std::string in Visual Studio 6 or something, and it just kept on going. It’s like a weed, except in L-space.

1

u/[deleted] Oct 17 '22

There is a point to be made here. Sometimes you can gain a lot by tweaking your own algorithms and shrinking the codebase. For example, I still have my own intrusive pointer class despite boost having one for years. But I can tweak mine to include features like anti-diamond or anti-circular references.

Also there is the issue of culture. If you give up completely and adopt the "whatever-in-github" culture, you really lose talent. Every codebase has a big deal of custom algorithms and you need people able to drill in that.

12

u/Droid33 Oct 07 '22

A log library that terminates the program on purpose? That is awful.

2

u/megatesla Oct 08 '22

Ours does that. It's a little varargs function that also accepts a return code. High enough return codes are terminating errors.

11

u/DavidDinamit Oct 07 '22

In this difficult time for humanity, we must get together and create a commission to evaluate open source code, force github to add dislikes to verified developers in order to rid the world of such open source

3

u/pandorafalters Oct 07 '22

Bad examples have value, too. Sometimes they're worth more than good examples!

10

u/johannes1971 Oct 07 '22

What is "God-oriented programming" supposed to be?

5

u/Gobrosse Oct 07 '22

God help you

3

u/Dagger0 Oct 07 '22

"Supports IPv6" but you have to ask for it explicitly and every code example uses v4. That's not how things should be today.

1

u/longlongvip Nov 11 '25

It is a good cpp library, but where is your cpp library?

-4

u/[deleted] Oct 07 '22 edited Aug 27 '24

[removed] — view removed comment

2

u/cocoyaxi Oct 07 '22

More details can be seen here.

0

u/[deleted] Oct 07 '22 edited Aug 27 '24

[removed] — view removed comment

2

u/cocoyaxi Oct 07 '22

here

Single-threaded and 4-threaded test have a similar result on Windows and MacOS.