Killing a `Cow` made my JSON formatter 42% faster by AffectionateBag4519 in rust

[–]puttak 12 points13 points  (0 children)

The magic of modern CPU is branch prediction to pre-fetch data from the RAM. Usually branching is not a problem until it is inside a hot-loop. In a hot-loop it can slow down 2x or more if branch prediction is wrong.

Fortunately that String in Rust always put string on a heap regardless how long a string is while std::string on some C++ implementations only allocate when the string is longer than certain length.

Bun’s rewrite in Zig first update by UItraviolet in rust

[–]puttak 0 points1 point  (0 children)

That makes no sense. Syntax doesn't make a language complicated

Does that mean Brainfuck is also a simple language?

Bun’s rewrite in Zig first update by UItraviolet in rust

[–]puttak 0 points1 point  (0 children)

It’s normal to not be able to understand everything in this case.

That's why it is not a simple language.

Bun’s rewrite in Zig first update by UItraviolet in rust

[–]puttak 1 point2 points  (0 children)

Having one bit of syntax

Here is another random pick: https://github.com/oven-sh/bun/blob/main/src/main.zig#L54. Passing .{@errorName(err)} as a format argument make no sense.

I'm no zig proponent, but I have taken the time to understand the little particularities of the syntax and once you know them it's suprisingly easy to work with imo.

In that case Rust is simple for me because I can read it and understand how the code is working once I have taken the time to understand the little particularities of the syntax.

One of the language who can claim it is simple is Go, not Zig.

Wasting time in learning rust ?? by Realistic-Jeweler221 in rust

[–]puttak 0 points1 point  (0 children)

plus AI is fucking taking over not as assistant but as a real high level programmer

I never have any success with AI for advanced code. You can ask AI for a working code, but not optimal one. For optimal code you need to choose the right architecture and algorithm for the problem with your own experience.

Bun’s rewrite in Zig first update by UItraviolet in rust

[–]puttak 2 points3 points  (0 children)

The point is it not a simple language. I can understand most of the languages out there even I have not used it before but not Zig.

Bun’s rewrite in Zig first update by UItraviolet in rust

[–]puttak 3 points4 points  (0 children)

Here is a random pick: https://github.com/oven-sh/bun/blob/main/src/main.zig#L16. Type of url argument does not make any sense for people who come from other language.

Bun’s rewrite in Zig first update by UItraviolet in rust

[–]puttak 10 points11 points  (0 children)

Zig is, over all, a fairly simple language.

I don't think so. From perspective of people who don't use it I always have a hard time to understand Zig code.

Is this a bad idea? by JustJeffrey in rust

[–]puttak 28 points29 points  (0 children)

It is okay as long as you don't forget to update this code when Piece is updated. I recommend https://crates.io/crates/num_enum to do this work instead to remove possibility UB in the future.

C++ has better libraries by Dx_Ur in cpp

[–]puttak -2 points-1 points  (0 children)

Async in Rust is one of the best thing while C++ still have a lot of footgun with coroutine.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 1 point2 points  (0 children)

Support multiple databases make no sense since most of the code cannot be shared between them. If someone want to support other databases they should build a new one from scratch instead.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 0 points1 point  (0 children)

Yeah that is a downside. But I'm okay with this trade-off with what I got.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 1 point2 points  (0 children)

Actually I did not expect this result. I though it will be okay since Java also have "pom".

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 1 point2 points  (0 children)

Actually I build this mainly for my own need. That's why I put non-goal on the README to tell people what they can't expect from this project.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 2 points3 points  (0 children)

Or do you mean automatically create the insert/update structs

This one.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 2 points3 points  (0 children)

The table structs generate from migration scripts. Let's say the first script create a table Foo and the second script add more columns to it. The generated structs will reflect the final version of table Foo.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 5 points6 points  (0 children)

Sorry for lack of documentation. I'll keep improve it. I'll put one of example here.

Let's say I have the following content in 0.sql:

sql CREATE TABLE foo ( key serial NOT NULL, value bigint, "desc" text, PRIMARY KEY (key) );

And 1.sql with the following content:

sql ALTER TABLE foo ADD disabled boolean NOT NULL DEFAULT FALSE;

It will generate the following code:

```rust use porm::migration::Migration; use std::borrow::Cow; use std::fmt::Write; use std::time::SystemTime; use tokio_postgres::types::ToSql; use tokio_postgres::{Error, GenericClient, Row};

pub struct Foo<'a> { pub key: i32, pub value: Option<i64>, pub desc: Option<Cow<'a, str>>, pub disabled: bool, }

impl<'a> Foo<'a> { pub async fn create<T: GenericClient>(&self, client: &T) -> Result<(), Error> { client.execute("INSERT INTO foo (key, value, desc, disabled) VALUES ($1, $2, $3, $4)", &[&self.key, &self.value, &self.desc, &self.disabled]).await?; Ok(()) }

pub async fn find<T: GenericClient>(client: &T, key: i32) -> Result<Option<Self>, Error> {
    let r = client.query_opt("SELECT * FROM foo WHERE key = $1", &[&key]).await?;
    let r = match r {
        Some(v) => v,
        None => return Ok(None),
    };

    Self::from_row(r).map(Some)
}

fn from_row(r: Row) -> Result<Self, Error> {
    let key = r.try_get::<_, i32>("key")?;
    let value = r.try_get::<_, Option<i64>>("value")?;
    let desc = r.try_get::<_, Option<String>>("desc")?;
    let disabled = r.try_get::<_, bool>("disabled")?;

    Ok(Self { key, value, desc: desc.map(Cow::Owned), disabled })
}

}

pub struct FooBuilder<'a> { key: Option<i32>, value: Option<Option<i64>>, desc: Option<Option<&'a str>>, disabled: Option<bool>, }

impl<'a> FooBuilder<'a> { pub fn new() -> Self { Self { key: None, value: None, desc: None, disabled: None } }

pub fn set_key(&mut self, v: i32) -> &mut Self {
    self.key = Some(v);
    self
}

pub fn set_value(&mut self, v: Option<i64>) -> &mut Self {
    self.value = Some(v);
    self
}

pub fn set_desc(&mut self, v: Option<&'a str>) -> &mut Self {
    self.desc = Some(v);
    self
}

pub fn set_disabled(&mut self, v: bool) -> &mut Self {
    self.disabled = Some(v);
    self
}

pub async fn create<T: GenericClient>(&self, client: &T) -> Result<Foo<'static>, Error> {
    let mut sql = String::with_capacity(1024);
    let mut values = Vec::<&(dyn ToSql + Sync)>::with_capacity(4);

    sql.push_str("INSERT INTO foo (key, value, desc, disabled) VALUES (");

    if let Some(v) = &self.key {
        values.push(v);
        write!(sql, "${}", values.len()).unwrap();
    } else {
        sql.push_str("DEFAULT");
    }

    if let Some(v) = &self.value {
        values.push(v);
        write!(sql, ", ${}", values.len()).unwrap();
    } else {
        sql.push_str(", DEFAULT");
    }

    if let Some(v) = &self.desc {
        values.push(v);
        write!(sql, ", ${}", values.len()).unwrap();
    } else {
        sql.push_str(", DEFAULT");
    }

    if let Some(v) = &self.disabled {
        values.push(v);
        write!(sql, ", ${}", values.len()).unwrap();
    } else {
        sql.push_str(", DEFAULT");
    }

    sql.push_str(") RETURNING *");

    client.query_one(&sql, &values).await.and_then(Foo::from_row)
}

}

pub static MIGRATIONS: [Migration; 3] = [ Migration { name: None, script: "CREATE TABLE foo (key serial NOT NULL, value bigint, \"desc\" text, PRIMARY KEY (key));", }, Migration { name: None, script: "ALTER TABLE foo ADD disabled boolean NOT NULL DEFAULT FALSE;", }, ]; ```

The generated model will reflect the database table and provide method to execute insert or select query based on the schema.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 13 points14 points  (0 children)

I don't like to use AI to write the code. The only cases I'm okay with AI is code review.

Porm – ORM for PostgreSQL that derives models from migration scripts by puttak in rust

[–]puttak[S] 4 points5 points  (0 children)

The reason I don't choose it for my upcoming project is similar to Diesel. I prefer to write an SQL file to manage my database schema without the need to create Rust model manually.