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

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • TheMightyBuzzard
    Senior Member
    • Sep 2021
    • 416

    Originally posted by ssokolow View Post

    I'm saying that Rust isn't special in that regard.
    Which is also what I was saying, if you recall.

    Every language has a progression of increasingly more knowledge-requiring constructs, including ones most people will never touch which get used in places like the C++ STL, Django ORM, etc. etc. etc.
    I wish that were the case. Kids loooooooove using the Shiny New Thing They Just Learned even when it is of absolutely zero benefit. Like using futures in a library whose logic absolutely necessitates blocking at every step of its single thread, just because they wanted to play with futures. I've actually seen that done. I stopped using and contributing to that library on the grounds that its author was an idiot.

    Comment

    • duby229
      Senior Member
      • Nov 2007
      • 7782

      Originally posted by DudeBro View Post

      There was an online programming competition that countet prime numbers up to a million and people where asked to provide the fastest code they can.

      nobody in the competition who wrote assembler managed to create faster code that what the rust compiler created.

      So if you are not an absolute master at assembly you likely will not be faster
      This is my experience... I've tried optimizing with assembly in the past, but it actually hurt.

      If you aren't a master at microbenchmarking then it is really hard to write fast assembly.

      Comment

      • iq-0
        Junior Member
        • Jun 2010
        • 14

        Originally posted by Raka555 View Post
        Lets talk again in a few years when someone has to maintain this ...

        image.png
        Code:
        use strict;
        use warnings;
        use AnyEvent::HTTP;
        use JSON;
        
        sub fetch {
                my ($url, $cb) = @_;
                http_get($url,
                        headers => { accept => 'application/json' },
                        sub {
                                my ($data, $hdr) = @_;
                                die "Failed to fetch $url" if $hdr->{Status} !~ /^2/;
                                return $cb->(decode_json $data);
                        }
                );
        }​
        edit: 🍺
        Last edited by iq-0; 10 December 2024, 12:07 PM.

        Comment

        • duby229
          Senior Member
          • Nov 2007
          • 7782

          Originally posted by Raka555 View Post

          Can you see why people prefer Python?
          image.png
          Except Python is seriously slow.... Everything that requires performance will need to be written in something else and then you can use Python as a script interpreter. It's the only thing it's good for.

          Comment

          • Mordrag
            Junior Member
            • Nov 2017
            • 28

            Originally posted by Raka555 View Post
            Lets talk again in a few years when someone has to maintain this ...

            image.png
            Skill Issue

            Comment

            • sdack
              Senior Member
              • Mar 2011
              • 1730

              Originally posted by Mordrag View Post
              Skill Issue
              Rust was created because people are having skill issues.

              Comment

              • back2未來
                Phoronix Member
                • Oct 2023
                • 80

                Originally posted by iq-0 View Post

                Code:
                use strict;
                use warnings;
                use AnyEvent::HTTP;
                use JSON;
                
                sub fetch {
                my ($url, $cb) = @_;
                http_get($url,
                headers => { accept => 'application/json' },
                sub {
                my ($data, $hdr) = @_;
                die "Failed to fetch $url" if $hdr->{Status} !~ /^2/;
                return $cb->(decode_json $data);
                }
                );
                }​
                edit: 🍺
                [ How about a refined version comparison (with some external help)?

                Perl:
                Code:
                use strict;
                use warnings;
                use AnyEvent::HTTP;
                use JSON qw(decode_json);
                
                sub fetch {
                my ($url, $cb) = @_;​
                
                http_get $url, headers => { Accept => 'application/json' }, sub {
                my ($body, $headers) = @_;​
                
                if ($headers->{Status} !~ /^2/) {
                warn "Failed to fetch $url: $headers->{Status} $headers->{Reason}";
                return;
                }​
                
                my $decoded_data;
                eval {
                $decoded_data = decode_json($body);
                1;
                } or do {
                warn "Failed to decode JSON from $url: $@";
                return;
                };​
                
                $cb->($decoded_data);
                };
                }​
                Rust:
                Code:
                use reqwest;
                use serde_json::Value;
                use anyhow::{Result};
                use tokio;
                
                async fn fetch(url: &str) -> Result<Value> {
                 let response = reqwest::get(url).await?;
                
                 println!("response: {:?}\n", response);
                
                 // Check if the response status is not successful
                 if !response.status().is_success() {
                 eprintln!("Failed to fetch {}: {}", url, response.status());
                
                 // Return a custom error message using anyhow
                 return Err(anyhow::anyhow!("Request failed with status: {}", response.status()));
                 }
                
                 // Parse the response body as JSON
                 let json: Value = response.json().await?;
                 Ok(json)
                }​
                
                #[tokio::main]
                async fn main() {​
                 let url = "http://worldtimeapi.org/api/ip";
                 println!("url: {}\n", url);
                 match fetch(url).await {
                  Ok(data) => println!("Received data {:?}", data),
                  Err(e) => eprintln!("Error: {}", e),
                 }
                }
                (thx) ]
                Last edited by back2未來; 14 December 2024, 02:55 PM.

                Comment

                • smitty3268
                  Senior Member
                  • Oct 2008
                  • 6963

                  Originally posted by TheMightyBuzzard View Post

                  Brother, I've been coding Rust since it was still shiny and new and I still had to give that more than a glance to understand all aspects of it. And if you think it's not going to be a new grad tasked with coming along later and maintaining something the original writers are no longer available for just because it wasn't a new grad who wrote it, you are living in fantasy land.
                  Seriously, which parts are confusing?

                  I'm not entirely sure where you are going with this. I do agree some of the language features Rust implements are complex and a new grad won't understand 100% of exactly how they get implemented. But I could say the same about a new grad not being able to directly explain how throwing exceptions in c++ works on the cpu, they just know how to use it in their code.

                  I hope you aren't seriously arguing that stuff like adding a ? to add error handling is confusing to a Rust programmer, or that new grads don't understand async/await.

                  Comment

                  • back2未來
                    Phoronix Member
                    • Oct 2023
                    • 80

                    [ just for completeness, same functionality with Cpp:
                    Code:
                    #include <iostream>
                    #include <curl/curl.h>
                    #include <nlohmann/json.hpp>
                    
                    using json = nlohmann::json;
                    
                    // Callback function to handle data received from libcurl
                    size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* out) {
                    size_t totalSize = size * nmemb;
                    out->append((char*)contents, totalSize);
                    return totalSize;
                    }
                    
                    void fetch(const std::string& url, const std::function<void(const json&)>& callback) {
                    CURL* curl;
                    CURLcode res;
                    std::string readBuffer;
                    
                    curl_global_init(CURL_GLOBAL_DEFAULT);
                    curl = curl_easy_init();
                    if (curl) {
                    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
                    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
                    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
                    curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
                    
                    res = curl_easy_perform(curl);
                    if (res != CURLE_OK) {
                    std::cerr << "Failed to fetch " << url << ": " << curl_easy_strerror(res) << std::endl;
                    } else {
                    try {
                    json jsonData = json::parse(readBuffer);
                    callback(jsonData);
                    } catch (const json::parse_error& e) {
                    std::cerr << "Failed to decode JSON from " << url << ": " << e.what() << std::endl;
                    }
                    }
                    curl_easy_cleanup(curl);
                    }
                    curl_global_cleanup();
                    }
                    
                    int main() {
                    fetch("http://worldtimeapi.org/api/ip", [](const json& data) {
                    std::cout << "Received data: " << data.dump(4) << std::endl;
                    });
                    
                    return 0;
                    }​
                    (thx) ]
                    Last edited by back2未來; 13 December 2024, 09:32 AM. Reason: code review

                    Comment

                    • YBoy360
                      Junior Member
                      • Jun 2023
                      • 13

                      The problem is how to use this lib in real world, if it cannot be linked. C can be linked everywhere.

                      Comment

                      Working...
                      X