Rust-Based, Memory-Safe PNG Decoders "Vastly Outperform" C-Based PNG Libraries

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • sdack
    Senior Member
    • Mar 2011
    • 1716

    Originally posted by darkonix View Post
    I don't fully disagree with you but you are describing progress. Would you prefer to continue working in machine code? No one is or at least should say Rust or any new technology prevents all bugs. But it is progress to prevent some. It is progress to have some productivity improvements. It is also progress to have a solid build standard infrastructure. Rust will still need to prove itself in the future. Until something better appears.

    I like shiny new things to be honest. I always liked that in my 40+ years experience. It keeps things interesting.
    It is not progress what I was describing, but human nature. Progress is just something we do. People do not necessarily choose the things they should in theory. You do not see people adopting veganism when it is healthier and better for the environment. Alcohol causes countless deaths each year, and still people drink. Do you believe people will drop alcohol, eat vegan, lower sugar intake and do daily exercises, because it is the right thing to do? People choose what they find practical, and converting to Rust is an inconvenience, a bitter pill. It will not see a major adoption.
    Last edited by sdack; 12 December 2024, 10:50 AM.

    Comment

    • ssokolow
      Senior Member
      • Nov 2013
      • 5058

      Originally posted by sdack View Post
      It is not progress what I was describing, but human nature. Progress is just something we do. People do not necessarily choose the things they should in theory. You do not see people adopting veganism when it is healthier and better for the environment. Alcohol causes countless deaths each year, and still people drink. Do you think people will drop alcohol, start to eat vegan, lower their sugar intake and do daily exercises, because it is the right thing to do? People choose what they find practical, and converting to Rust is an inconvenience.
      Depends on the person. I find staying on Python more of an inconvenience than converting to Rust for non-throwaway projects because of the "It works! Don't f**k with it!" factor combined with how dependencies won't stay static.

      Rust also has some very nice dependencies for achieving things that show up commonly in "scripting" tasks, like Serde, Clap, Rayon, ignore, etc. and they're dirt-simple to add to a project.
      Last edited by ssokolow; 12 December 2024, 10:58 AM.

      Comment

      • back2未來
        Phoronix Member
        • Oct 2023
        • 67

        Originally posted by lowflyer View Post

        Sorry back2未來 , But this blurp is not C++. It is plain ole' C code with a bit of adaption to use C++ libraries. You got that with the help from some "cpp expert", you say? He lied to you. A mere change from `gcc` to `g++` does not convert your C code into C++.
        [ very true; he wanted to preserve the coherence towards previous structures&style for readability and maintenance, include proven resources and stay efficient for the task, but he's very flexible and suggests an updated, 'more strict cpp' version:
        Code:
        #include <iostream>
        #include <string>
        #include <functional>
        #include <sstream>
        #include <boost/asio.hpp>
        #include <nlohmann/json.hpp>
        
        using json = nlohmann::json;
        namespace asio = boost::asio;
        using boost::asio::ip::tcp;​
         
        // Function to handle HTTP GET request and fetch JSON data
        void fetch(const std::string& host, const std::string& path, const std::function<void(const json&)>& callback) {
         if (host.empty() || path.empty()) {
         std::cerr << "Error: Empty host or path provided" << std::endl;
         return;
         }
        
         if (!callback) {
         std::cerr << "Error: Invalid callback function provided" << std::endl;
         return;
         }
        
         try {
         asio::io_context io_context;
        
         // Resolve host
         tcp::resolver resolver(io_context);
         auto endpoints = resolver.resolve(host, "http");
        
         // Create socket and connect to host
         tcp::socket socket(io_context);
         asio::connect(socket, endpoints);
        
         // Prepare the HTTP GET request
         std::stringstream request_stream;
         request_stream << "GET " << path << " HTTP/1.1\r\n"​
         << "Host: " << host << "\r\n"
         << "Connection: close\r\n\r\n";
        
        ​// Send the request
         asio::write(socket, asio::buffer(request_stream.str()));
        
         // Receive the response
         std::string response;
         asio::streambuf buffer;
         //asio::read_until(socket, buffer, "\r\n\r\n");
         asio::read_until(socket, buffer, "}" );​
        
         // Process response headers (skip them)
         std::istream response_stream(&buffer);
         std::getline(response_stream, response); // First line is the HTTP status
         std::string header;
         while (std::getline(response_stream, header) && header != "\r") {
         // Skipping the headers
         }​
        
        // Read the body of the response
         std::ostringstream body_stream;
         //asio::read(socket, buffer, asio::transfer_all());
         body_stream << &buffer;
        
        ​// Parse the JSON data from the response body
         std::string body = body_stream.str();
         if (body.empty()) {
         std::cerr << "Error: Received empty response body from " << host << path << std::endl;
         } else {
         try {​
         json jsonData = json::parse(body);
         callback(jsonData);
         } catch (const json::parse_error& e) {
         std::cerr << "Failed to decode JSON from " << host << path << ": " << e.what() << std::endl;
         }​
         }
         } catch (const std::exception& e) {​
        std::cerr << "Exception while fetching data "<< e.what() << std::endl;
         }
        }
         
        int main() {
         // Example usage of fetch
         fetch("worldtimeapi.org", "/api/ip", [](const json& data) {
         if (data.is_null()) {
         std::cerr << "Error: Received null JSON data" << std::endl;
         return;
         }
         std::cout << "Received data: " << data.dump(4) << std::endl;
         });
        
         return 0;
        }​
        (thx) ]​
        Last edited by back2未來; 16 December 2024, 05:28 AM. Reason: code verified

        Comment

        • sdack
          Senior Member
          • Mar 2011
          • 1716

          Originally posted by darkonix View Post
          There is a point there: it is still valid C++, right?
          C++ is a superset of C. It is perfectly fine for code to look like C in C++. One does not have to use every single feature of C++ on each line. If one can solve a problem with a few lines of C code inside a C++ application then it was done correctly. I have seen idiots who try to solve every problem with templates, because they were just having fun. There are often many ways one can solve a problem, but idiotic code is much harder to maintain. Keep it simple. Do not listen to the bullshitters.

          Comment

          • duby229
            Senior Member
            • Nov 2007
            • 7778

            Originally posted by sdack View Post
            C++ is a superset of C. It is perfectly fine for code to look like C in C++. One does not have to use every single feature of C++ on each line. If one can solve a problem with a few lines of C code inside a C++ application then it was done correctly. I have seen idiots who try to solve every problem with templates, because they were just having fun. There are often many ways one can solve a problem, but idiotic code is much harder to maintain. Keep it simple. Do not listen to the bullshitters.
            You do know gcc outperforms g++, right? And by huge margin on a few metrics too. Gcc compiles like 5 times faster than g++, it produces considerably smaller file sizes, it generates faster binaries.

            If your C++ code is really C, then you should just write C code... You'll get better results...

            EDIT: I really don't understand why people even write in C++ at all, it is not fast, or intuitive, or readable... It is convoluted, overly complex and hard to learn...

            EDIT: Even in this thread there are a few guys shaming new graduates because they don't fully understand C++, but even with their claimed 40+ years experience, I absolutely guarantee even they don't come anywhere even close to fully understanding C++. Not even the guys defining it and writing the compiler for it fully understand it or else it would perform better!
            Last edited by duby229; 12 December 2024, 02:12 PM.

            Comment

            • darkonix
              Senior Member
              • Sep 2021
              • 376

              Originally posted by duby229 View Post

              You do know gcc outperforms g++, right? And by huge margin on a few metrics too. Gcc compiles like 5 times faster than g++, it produces considerably smaller file sizes, it generates faster binaries.

              If your C++ code is really C, then you should just write C code... You'll get better results...

              EDIT: I really don't understand why people even write in C++ at all, it is not fast, or intuitive, or readable... It is convoluted, overly complex and hard to learn...

              EDIT: Even in this thread there are a few guys shaming new graduates because they don't fully understand C++, but even with their claimed 40+ years experience, I absolutely guarantee even they don't come anywhere even close to fully understanding C++. Not even the guys defining it and writing the compiler for it fully understand it or else it would perform better!
              But there are things that are super convenient in C++. If I can choose why would I want to suffer the curse of C null terminated "strings" when there is a much simpler String class in C++? There are lots of other features that are useful. The problem is that you can start adding a lot of complexity too. It is a trade off.
              Last edited by darkonix; 12 December 2024, 11:45 PM.

              Comment

              • mobadboy
                Senior Member
                • Jul 2024
                • 160

                Originally posted by sdack View Post
                So much anger and ignorance!
                lol

                Originally posted by sdack View Post
                If the Rust people were not stupid, they would not need to be protected from mistakes in the first place.
                hmmmmmmmmmmm

                Originally posted by sdack View Post

                Also, seat belts protect from the mistakes of other drivers. And only to add this: FUCK.


                you don't say

                re-read the sections i've split up in sequence and think about it

                Comment

                • mobadboy
                  Senior Member
                  • Jul 2024
                  • 160

                  Originally posted by duby229 View Post

                  You do know gcc outperforms g++, right? And by huge margin on a few metrics too. Gcc compiles like 5 times faster than g++, it produces considerably smaller file sizes, it generates faster binaries.
                  compile once run many times

                  it is not like c doesnt come with a cost. i can count, on one hand, the number of rust applications which have crashed on me. c though... there's a few that are good: linux, systemd etc, but that's because the standards are so high. most applications do not have the resourcing to meet the same standards. providing the same standards in c++ though reduces the resource effort because you suddenly get tons of features which you don't have to reimplement yourself. providing the same standards in rust is even much easier than c++ and more accessible to Not Perfect people who might be writing some code at 2am since they have a job and that's just what happens sometimes.

                  Comment

                  • darkonix
                    Senior Member
                    • Sep 2021
                    • 376

                    Originally posted by sdack View Post
                    C++ is a superset of C. It is perfectly fine for code to look like C in C++. One does not have to use every single feature of C++ on each line. If one can solve a problem with a few lines of C code inside a C++ application then it was done correctly. I have seen idiots who try to solve every problem with templates, because they were just having fun. There are often many ways one can solve a problem, but idiotic code is much harder to maintain. Keep it simple. Do not listen to the bullshitters.
                    C++ is not a strict superset of C. Some code compiles in C that does not in C++.

                    Example (from a quick google search):
                    Code:
                    #include <stdlib.h>
                    
                    int main()
                    {
                        // C++ error: invalid conversion from 'void*' to 'int*'
                        int *p = malloc(sizeof(int));
                        free(p);
                    }​

                    Comment

                    • sdack
                      Senior Member
                      • Mar 2011
                      • 1716

                      Originally posted by darkonix View Post

                      C++ is not a strict superset of C. Some code compiles in C that does not in C++.

                      Example (from a quick google search):
                      Code:
                      #include <stdlib.h>
                      
                      int main()
                      {
                      // C++ error: invalid conversion from 'void*' to 'int*'
                      int *p = malloc(sizeof(int));
                      free(p);
                      }​
                      I appreciate the attention to detail, but when you want to dig deeper then you should not just say "C++" and "C" but be specific on the compilers and standards you are referring to. A simple -fpermissive to g++ will compile it for example. Anyhow, I think I got my point across, or else would you not try to distract with a tangent.

                      Comment

                      Working...
                      X