New around here? Register your SA Forums Account here!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.
this discussion is not very guid

Adbot
ADBOT LOVES YOU

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Blinkz0rz posted:

i'd use List<T>.Add because we're talking about c#

how do you think List is implemented under-the-hood?

should it have magic special-case powers that other libraries aren't allowed to use?

champagne posting
Apr 5, 2006

YOU ARE A BRAIN
IN A BUNKER


terrible programming: the etl team at my place of work ingested a bunch of stuff, mostly strings where some of them happen to start with '\0'. These landed in the read-only db.

The solution downstream? Make everything check strings to see if they start with a null character.

mystes
May 31, 2006

Is having an array of raw guids without other associated information really that common? Like, if the guid struct didn't already exist and someone was saying it should be implemented as a struct because of theoretical performance benefits despite this creating a major gotcha with respect to the constructor that would seem like some serious premature optimization to me.

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS

mystes posted:

Is having an array of raw guids without other associated information really that common? Like, if the guid struct didn't already exist and someone was saying it should be implemented as a struct because of theoretical performance benefits despite this creating a major gotcha with respect to the constructor that would seem like some serious premature optimization to me.

this is what i've been trying to understand. what weird purpose does preallocating and eventually resizing a container of guids serve such that it's worth the usage edge case of ending up with an array of duplicate "globally unique" identifiers?

i'll chalk it up to "dumb design decision" more than anything else i guess

AWWNAW
Dec 30, 2008

Blinkz0rz posted:

i'd use List<T>.Add because we're talking about c#

which is most commonly backed by arrays

Zlodo
Nov 24, 2006

Jabor posted:

it's a valid struct, in that all the fields are initialized and there aren't any pointers off into la-la land that are ripe for corrupting memory. your code is allowed to access all those default-initialized fields, and decide to do (or not do) things with them as it pleases.

they're not initialized to anything useful tho, so maybe you won't crash but you'll still have a bug, which could have been caught at compile time

quote:

in c++, you literally pinky-promise to the compiler that you will never read from those uninitialized locations, and the compiler is allowed to mutate and optimize your code in all sorts of ways that will break horribly if it turns out that you were lying and do actually read those locations before you wrote something valid into them.

in c++ you can't access uninitialized memory if you use the standard containers and write your types properly

I mean yeah the language is dumb and lets you have uninitialized integers of floats or pointers, but if you use c++ in the same way you'd use c# you are not going to run into these issues

what would be great is if c++ allowed to enforce a ban on unsafe constructs within a namespace or something like that, like forbidding declaring raw pointers and uninitialized members and variables and using the address of operator

I mean if you force yourself not to do those things you get rid of a large proportions of the pitfalls of the language

Zlodo
Nov 24, 2006
(loving phone)

cinci zoo sniper
Mar 14, 2013




Boiled Water posted:

terrible programming: the etl team at my place of work ingested a bunch of stuff, mostly strings where some of them happen to start with '\0'. These landed in the read-only db.

The solution downstream? Make everything check strings to see if they start with a null character.

delet this

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Blinkz0rz posted:

this is what i've been trying to understand. what weird purpose does preallocating and eventually resizing a container of guids serve

are you a self-taught programmer?

i don't mean to be offensive, it's just that things like how you implement higher-level abstractions like lists from raw arrays are a really common thing in formal instruction, but it seems like you've never ever encountered the idea before

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Zlodo posted:

they're not initialized to anything useful tho, so maybe you won't crash but you'll still have a bug, which could have been caught at compile time

oh, you've solved the halting problem too?

or were you going to the rust-style thing where you're not allowed to do unless the compiler can prove it's safe, and if the compiler can't prove it you need to refactor your code to make it easier for the compiler to prove?

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS

Jabor posted:

are you a self-taught programmer?

i don't mean to be offensive, it's just that things like how you implement higher-level abstractions like lists from raw arrays are a really common thing in formal instruction, but it seems like you've never ever encountered the idea before

no i understand it in the abstract and i was being facetious wrt List<T>.Add because the conversation very quickly turned from c#'s weird case to manual memory allocation. i'm talking about the specific case where you're doing this with guids that aren't actually guids

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
so when you resize your underlying array, what is it filled with before you copy over your existing entries?

would you agree that it makes no sense to generate a unique random guid for every slot that is immediately overwritten with the thing you copy over?

Zlodo
Nov 24, 2006

Jabor posted:

oh, you've solved the halting problem too?

I know that trying to predict every bug at compile time is impossible bc it would mean solving the halting problem but "yell at programmer if type used in array don't have a default ctor" is not solving the halting problem

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Zlodo posted:

I know that trying to predict every bug at compile time is impossible bc it would mean solving the halting problem but "yell at programmer if type used in array don't have a default ctor" is not solving the halting problem

so we're back to saying "you're not allowed to put a guid in any form of collection" ?

Zlodo
Nov 24, 2006

Jabor posted:

so when you resize your underlying array, what is it filled with before you copy over your existing entries?

in c++ it is uninitialized until the entries are copied over
which is dangerous but it's the responsibility of the container implementation, which is written by experts (yeah I do know a lot of idiots roll their own and they shouldn't)

quote:

would you agree that it makes no sense to generate a unique random guid for every slot that is immediately overwritten with the thing you copy over?

the problem is that c# is unable to separate memory allocation and object initialization, which is a shame

the fact that c++ permits it arguably results in the language to be dangerous and full of pitfalls and as I said before I wouldn't mind a way to segregate the use of dangerous constructs to low level libraries written by experts

but the c# way doesn't seem to provide a satisfactory solution for uninitialized structs either

filling them with 0 and claiming it's ok bc "at least it won't crash" isn't really that good of a solution imo

mystes
May 31, 2006

Allocating the space as a separate step at least allows you to separate the language into unsafe and safe modes rather than making everyone deal with uninitialized structs.

If you're implementing a list or whatever can't you actually do this in unsafe c#?

Xarn
Jun 26, 2015

Blinkz0rz posted:

i'd use List<T>.Add because we're talking about c#

Do you like, have no idea how that is implemented? Because that is dumbest poo poo I've read today and I clicked on fishmech's post in D&D.

prisoner of waffles
May 8, 2007

Ah! well a-day! what evil looks
Had I from old and young!
Instead of the cross, the fishmech
About my neck was hung.
array initialization chat: the same poo poo again and again, it takes too long, and at best it ends up all zeros

Zlodo
Nov 24, 2006

prisoner of waffles posted:

array initialization chat: the same poo poo again and again, it takes too long, and at best it ends up all zeros

yeah but I missed it yesterday bc I was probated for quoting stymie

champagne posting
Apr 5, 2006

YOU ARE A BRAIN
IN A BUNKER


i'm not sure i'd take array resizing advice from a poster who quoted stymie

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


prisoner of waffles posted:

array initialization chat: the same poo poo again and again, it takes too long, and at best it ends up all zeros

but enough about life and the human experience,.

cinci zoo sniper
Mar 14, 2013




my code to get a last jsonthing from a 1-to-many relationship (where jsons may not exist at all) and do stuff with it:



lead developer on this project: "hey cinci, do u use some gui tool that writes sql scripts for u? ur sqls are real weird, you can just do this like so"

his code:



posting as images because clownflare blocks sqls lol

champagne posting
Apr 5, 2006

YOU ARE A BRAIN
IN A BUNKER


ah yes the code school of "more compact, better than"

cinci zoo sniper
Mar 14, 2013




Boiled Water posted:

ah yes the code school of "more compact, better than"

also the code school of "left join everything because i'm an idiot who does not know how left join works and what is the result of left joining many matching entities to a different entity", or "reusing code is only for poors who cannot afford new code every time"

pseudorandom
Jun 16, 2010



Yam Slacker

Blinkz0rz posted:

but why is that something you'd want to do in the first place? it feels like a weird consideration for something whose existence and use relies entirely on randomness

like, have System.Guid() create a default v4 and have an alternative constructor that accepts null and spits out all zeroes or something or like System.NullGuid() or some poo poo

or take it a step further and don't even have a guid type; just have a bunch of guid helper methods that spit out strings

Just because the concept of a Guid means globally unique, doesn't mean the container must always be globally unique all of the time. It's just holding a value.

Let's say I have a CSV file containing a list of people: "guid, first name, last name, butt". This list may have thousands of lines. If I'm trying to load this CSV, and I don't want my program taking the computational time to generate thousands of new random guids, just so they can be overwritten with the "guid" values being read from the CSV.

Another example is sending the IDs over the network; the client might see the quantity of IDs being received, and thus pre-allocate an array for all of the IDs that it is receiving. Again, I don't want my client application generating thousands of random numbers that are going to be immediately discarded when the existing Guids are read in from the network.

cinci zoo sniper posted:

his code:



posting as images because clownflare blocks sqls lol

:stare:

Just wait until this guy learns how much more efficient queries are if it's written on all one line.

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS

Jabor posted:

so when you resize your underlying array, what is it filled with before you copy over your existing entries?

would you agree that it makes no sense to generate a unique random guid for every slot that is immediately overwritten with the thing you copy over?

i agree but would contend that storing guids in an array and then resizing it is exactly the kind of edge case i'd expect to have performance implications.

at the end of the day asking for a guid and not actually getting a guid is a bad experience

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


i know we're talking hypothetical universe here, but i'd prefer if a constructed guid was immediately valid (nb: nonzero), and check the assumption on the other end that the space must be pre-allocated (rather than allocating repeatedly as it comes in over the wire, with a typical constructor)

Just My Opinion Though and i don't work in super high efficiency code, so idk how realistic such assumptions even are

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS

pseudorandom posted:

Just because the concept of a Guid means globally unique, doesn't mean the container must always be globally unique all of the time. It's just holding a value.

Let's say I have a CSV file containing a list of people: "guid, first name, last name, butt". This list may have thousands of lines. If I'm trying to load this CSV, and I don't want my program taking the computational time to generate thousands of new random guids, just so they can be overwritten with the "guid" values being read from the CSV.

Another example is sending the IDs over the network; the client might see the quantity of IDs being received, and thus pre-allocate an array for all of the IDs that it is receiving. Again, I don't want my client application generating thousands of random numbers that are going to be immediately discarded when the existing Guids are read in from the network.

right, i get this and i appreciate you actually providing an example of why you might want to do this

i'm not sure i buy the first example because i expect you wouldn't want to preallocate space for an a csv that could be potentially unbounded in size and would instead want to stream it line-by-line and either parse an existing guid using the string-based constructor or generate a new actually unique one if a guid didn't exist in the field

your second example makes sense and is exactly the kind of situation i was asking about

i still think that having the empty constructor not be a real globally unique guid is bad ergonomics but that's a different issue

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS
lots of lovely attitudes in this terrible programmers thread btw

the pl threads is down-forum if you want to be a pedantic, derisive shithead rather than answering questions honestly

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Blinkz0rz posted:

i agree but would contend that storing guids in an array and then resizing it is exactly the kind of edge case i'd expect to have performance implications.

at the end of the day asking for a guid and not actually getting a guid is a bad experience

"storing it in an array and then resizing it" is how essentially every higher-level collection worth using works, which is why i've been talking about it. it's all arrays under the hood.

this is kind of why i asked earlier if you were self-taught - it's okay if you actually didn't know this stuff! if you do know that arrays are involved everywhere and are being deliberately obtuse about it that's a whole other story.

animist
Aug 28, 2018
don't sign yuor posts

Xarn
Jun 26, 2015
This

Blinkz0rz posted:

i still think that having the empty constructor not be a real globally unique guid is bad ergonomics but that's a different issue

is completely right, and it is deeply ironic that C++ makes this saner and safer than C# without imposing a performance penalty.


But if you don't know that arrays are over-allocated all the time because that is the only way to have useful List<T>, then you really should consider Belize.

prisoner of waffles
May 8, 2007

Ah! well a-day! what evil looks
Had I from old and young!
Instead of the cross, the fishmech
About my neck was hung.

Blinkz0rz posted:

lots of lovely attitudes in this terrible programmers thread btw

the pl threads is down-forum if you want to be a pedantic, derisive shithead rather than answering questions honestly

cosigned

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
there's definitely an argument that it could have been better to force you to write default(MyStruct) to get the default-initialized instance, instead of allowing what looks like a zero-argument constructor call.

JawnV6
Jul 4, 2004

So hot ...
wait y'all can just like... get a random number? whenever u want? that's wild

Plorkyeran
Mar 21, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Blinkz0rz posted:

right, i get this and i appreciate you actually providing an example of why you might want to do this

i'm not sure i buy the first example because i expect you wouldn't want to preallocate space for an a csv that could be potentially unbounded in size and would instead want to stream it line-by-line and either parse an existing guid using the string-based constructor or generate a new actually unique one if a guid didn't exist in the field

looping over the lines of a csv file, parsing the line, and then adding the resulting object to a List results in List preallocating space for you

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS

Xarn posted:

But if you don't know that arrays are over-allocated all the time because that is the only way to have useful List<T>, then you really should consider Belize.

yes, i'm quite aware of this and again was being facetious when I was referring to List<T>.Add

however i think my confusion with this conversation is the insistence that a guid have to be recalculated on resize. is this how arrays work under the covers?

i thought empty arrays only held a reference to their type and length and resizing an array either allocated more space as empty or created a brand new empty array and copied over existing objects. is this not the case? if it is, why would there need to be a constructed guid object at all?

Plorkyeran posted:

looping over the lines of a csv file, parsing the line, and then adding the resulting object to a List results in List preallocating space for you

yeah but why would that involve the list creating a guid at all? can you see my confusion?

CPColin
Sep 9, 2003

Big ol' smile.
It seems like the best solution would be for the following things to be true simultaneously: 1) structs/value types have a default constructor, 2) when an array of structs is allocated, the compiled code calls the default constructor, and 3) the default constructor cannot otherwise be referenced or called in code.

Which I think is what people are saying, basically; it's dumb for C# to let you call the default constructor, specifically in the case when you're working with GUID's.

Adbot
ADBOT LOVES YOU

Plorkyeran
Mar 21, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Blinkz0rz posted:

yes, i'm quite aware of this and again was being facetious when I was referring to List<T>.Add

however i think my confusion with this conversation is the insistence that a guid have to be recalculated on resize. is this how arrays work under the covers?

i thought empty arrays only held a reference to their type and length and resizing an array either allocated more space as empty or created a brand new empty array and copied over existing objects. is this not the case? if it is, why would there need to be a constructed guid object at all?


yeah but why would that involve the list creating a guid at all? can you see my confusion?

c# doesn't let you have allocated-but-uninitialized memory. preallocating space requires creating valid objects in that space. creating a bigger new array and then copying the existing objects over involves initializing all of the elements in that array before you can copy over the objects from the old array.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply