Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

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
Mooey Cow
Jan 27, 2018

by Jeffrey of YOSPOS
Pillbug
It really is a FuckedString, btw.

It uses the dereference operator (prefix *) as a conversion to a const TCHAR* (not to be confused with the previously discussed Windows TCHAR, nor the many T-prefixed template classes), but also, it uses the multiply operator (infix *) for treating the string as a path and appending a path separator (a global #define) between the two strings that are... multiplied.

It's horrific by itself, but like... they could have at least used the division operator for that.


Oh yeah, and all the comparisons are done by using **this (to get the TCHAR*) then sending it to stricmp :v:
Always case insensitive, never checks that the string lengths even match, has to null-terminate the strings to compare them.

Adbot
ADBOT LOVES YOU

Nth Doctor
Sep 7, 2010

Darkrai used Dream Eater!
It's super effective!


Mooey Cow posted:

Still haven't figured out what the christ the "F" prefix means. "hosed"?

Based on the context I'd say the F is for Force.

streetlamp
May 7, 2007

Danny likes his party hat
He does not like his banana hat
certainly a solution to finding if a number is prime
(the array goes to 49999)

Horse Clocks
Dec 14, 2004


I know it’s wrong. But I love it. It’s pragmatic.

Interviewer: We need to find out if a number is a prime”
Dev: How big do our numbers go, and do we need a gofast solution?
I: 50k, and sure.
D: ok, here.

It solves the problem. It’d be better if it knew its constraints and hard errored on >50k.

The counterpoint are companies that want everything for twitter scale. “How would you design this?” Uh, web server and Postgres, you told me you hit 1 concurrent user. Revisit it when it’s proven itself to be making money, and beyond the scale of its implementation.

CPColin
Sep 9, 2003

Big ol' smile.
Wait, negative numbers aren't prime, though, are they?

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

streetlamp posted:

certainly a solution to finding if a number is prime
(the array goes to 49999)



No hire. Should have just had return primes.indexOf(Math.abs(num)) !== -1 instead of that if block...

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:
Should have used a bool array of 50k items so you can do an O(1) index operation instead of an O(n) indexOf.

Carbon dioxide
Oct 9, 2012

streetlamp posted:

certainly a solution to finding if a number is prime
(the array goes to 49999)



Doom Mathematic
Sep 2, 2008

streetlamp posted:

certainly a solution to finding if a number is prime
(the array goes to 49999)



Well, we all remember the classic "check if an integer is a perfect number", right?

code:
return x == 6 || x == 28 || x == 496 || x == 8128 || x == 33550336
That's for a 32-bit integer. For 64 bits you only need three more terms.

Qwertycoatl
Dec 31, 2008


Not checking but I'd love if one of those values was wrong. Hours of fun for everyone!

Mad Jaqk
Jun 2, 2013

dwazegek posted:

Should have used a bool array of 50k items so you can do an O(1) index operation instead of an O(n) indexOf.

I know you're joking, but unironically it should have been either a hash table or at least a binary search. Precomputing answers isn't necessarily a bad idea depending on the use case, but there's no reason not to make the search more efficient.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Mad Jaqk posted:

there's no reason not to make the search more efficient.

It's often completely pointless to spend time making something more efficient.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Mad Jaqk posted:

I know you're joking, but unironically it should have been either a hash table or at least a binary search. Precomputing answers isn't necessarily a bad idea depending on the use case, but there's no reason not to make the search more efficient.

Linear searches under 1k items are usually faster than binary or hash table searches.

xtal
Jan 9, 2011

by Fluffdaddy

Lumpy posted:

No hire. Should have just had return primes.indexOf(Math.abs(num)) !== -1 instead of that if block...

Should have had return ~primes.indexOf(Math.abs(num))

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
A bit-vector would be way more efficient in both space and time. Primes are dense at approximately 1 / ln(N), so an array of the primes up to N will have N / ln(N) elements of size log2(N) bits each. There is no N for which N * log2(N) / ln(N) = N * log2(e) is less than N, and that's imagining an optimal bit-packing of the array elements.

rjmccall fucked around with this message at 23:11 on Jun 28, 2018

1337JiveTurkey
Feb 17, 2005

If you use a bit vector, also special case 2 so you can map every N to (N - 1) / 2 and save half the space. You could also take an example from database bitmap indices and compress the vector, letting the compressor figure out that every other bit is 0.

KernelSlanders
May 27, 2013

Rogue operating systems on occasion spread lies and rumors about me.

Hammerite posted:

It's often completely pointless to spend time making something more efficient.

Computers are now vastly cheaper than developers in all but the most exceptional of circumstances.

boo_radley
Dec 30, 2005

Politeness costs nothing

repiv posted:

F was indeed originally for float: https://forums.unrealengine.com/dev...ructs-stand-for

It seems to have morphed into a catch-all prefix for anything that doesn't fit into their other prefixes though.

A common misconception. The 'F' is for "Fother"

xtal
Jan 9, 2011

by Fluffdaddy
bad post

xtal fucked around with this message at 12:52 on Jun 29, 2018

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

CPColin posted:

Wait, negative numbers aren't prime, though, are they?

Primes are defined as integers greater than 1 in context of the fundamental theorem of arithmetic, which says that every number > 1 is either prime or can be stated as a product of prime numbers, and that this representation is unique up to the order of the factors. The reason 1 isn't considered a prime number is because if it were, then there wouldn't be a unique representation of prime factors - e.g. if 1 were prime, then 2 could be factored as 2 * 1, 2 * 1 * 1, 2 * 1 * 1, 2 * 1 * 1 * 1. Similarly, if negative numbers were considered prime, then there wouldn't be a unique representation of the factors (e.g. 4 could be 2 * 2, or -2 * -2.)

CPColin
Sep 9, 2003

Big ol' smile.
Right, so why is that code taking the absolute value of its input?

Nth Doctor
Sep 7, 2010

Darkrai used Dream Eater!
It's super effective!


xtal posted:

Should have had return ~primes.indexOf(Math.abs(num))

TIL about the JavaScript bitwise NOT operator. I'll have to put that up on the shelf right next to writing my own sort implementation.

Taffer
Oct 15, 2010


Hammerite posted:

It's often completely pointless to spend time making something more efficient.

this attitude is why electron exists

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug
Months ago, I implemented a clever hack to get around a stupid problem. It came with a stern warning that they should fix the actual problem.

The clever hack is now considered a "standard" that they copy and paste constantly.

I knew it would happen, but I was hoping I'd be surprised for once.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Taffer posted:

this attitude is why electron exists

Unquestioningly applying the principle that "things should be as efficient as possible" might lead us to devote time to making things efficient whose inefficiency is benign, whereas assuming inefficiency never matters might lead us to create software that is much slower and less responsive than it needs to be. It's almost as though there is a need to apply critical thought and to identify when it's worthwhile spending time making things more efficient, and when it isn't.

Taffer
Oct 15, 2010


Hammerite posted:

It's almost as though there is a need to apply critical thought and to identify when it's worthwhile spending time making things more efficient, and when it isn't.

The issue is that people are often unable to identify which is which, or worse, are forced into a situation where they are fundamentally unable to optimize due to poor optimization choices by those who have come before them. See: the entire javascript ecosystem.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Going too far either way is equally bad.

Ola
Jul 19, 2004

Taffer posted:

The issue is that people are often unable to identify which is which, or worse, are forced into a situation where they are fundamentally unable to optimize due to poor optimization choices by those who have come before them. See: the entire javascript ecosystem.

There's also the well established programming culture of being very loud and rude about others' suboptimal code, which quickly whips juniors into hyper-optimizing every tiny thing.

Coffee Mugshot
Jun 26, 2010

by Lowtax
If you can't write things in an efficient way the first time, the solution is not optimizing your poorly written code, it's solidifying ideas and rewriting from fundamental concepts. Just throw poo poo out more imo, developers get too infatuated with their abstractions.

Anyways thats not how it works at a job, so use pprof and keep eating those low hanging fruits to sustain your salary or w/e.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

As tef says: write code thats easy to delete.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
good job on the optimizations mods... we got this argument down to around 10 posts this time. still doesn't meet our metrics though, let's shoot for 3 posts next time.

CPColin
Sep 9, 2003

Big ol' smile.
Let's write a unit test that fails when the task doesn't complete in time. Better set the limit to 10x what's normal, so we don't get spurious failures.

brap
Aug 23, 2004

Grimey Drawer
efficiency: good or bad? The Something Awful goons will get to the bottom of it

Jeb Bush 2012
Apr 4, 2007

A mathematician, like a painter or poet, is a maker of patterns. If his patterns are more permanent than theirs, it is because they are made with ideas.
it's like knuth said: "all optimization is worse than the most depraved murder. if you optimize you should go to jail forever"

Phobeste
Apr 9, 2006

never, like, count out Touchdown Tom, man

Jeb Bush 2012 posted:

it's like knuth said: "all optimization is worse than the most depraved murder. if you optimize you should go to jail forever"

I prefer his aphorism, “goto is the highest expression of the love of Christ the redeemer and to use other flow control constructs is the work of Satan”

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun
We implemented a caching layer a few years ago. Certain things got cached. Now, let's leave aside that there are actually like four or five ways one set of data is cached; this is because our code has no standards and everyone uses this data differently. That's a pain but not the end of the world. Legacy code!

No, what's bad is that there were pairs of functions written, get and set. There were several of these pairs, but they all worked the same way.

Python code:
def get_shit(object_id):
    # returns None if the cache is empty
    return cache.get(cache_key(object_id))

def set_shit(object_id, stuff):
    cache.set(cache_key(object_id), stuff)
I oversimplify. But where, you might wonder, does stuff come from? Glad you asked!

Python code:
# this is code that needs to hit the cache to do stuff

cached_shit = get_shit(object_id)
if cached_shit is None:
    # do the query to calculate the cached poo poo
    set_shit(object_id, stuff)
This code might exist in ten different places. And the real horror is that some of them calculated the stuff for caching differently. So if the cache was empty for whatever reason, what data was put in the cache could vary based on who called it first.

:psypop:

I quit ripping my hair out and rewrote this but god, it felt good to eliminate that. Fixed a few weird bugs too.

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

CPColin posted:

Right, so why is that code taking the absolute value of its input?

oh yeah that. the joke is that javascript developers code defensively because you never know what could happen in javascript.

Bongo Bill
Jan 17, 2012

Hammerite posted:

Unquestioningly applying the principle that "things should be as efficient as possible" might lead us to devote time to making things efficient whose inefficiency is benign, whereas assuming inefficiency never matters might lead us to create software that is much slower and less responsive than it needs to be. It's almost as though there is a need to apply critical thought and to identify when it's worthwhile spending time making things more efficient, and when it isn't.

Critical thought and also measurement.

feedmegin
Jul 30, 2008

KernelSlanders posted:

Computers are now vastly cheaper than developers in all but the most exceptional of circumstances.

'This software by one developer runs on n hundred thousand computers' is also a thing, though.

Adbot
ADBOT LOVES YOU

Carbon dioxide
Oct 9, 2012

https://twitter.com/troyhunt/status/1013305478175080448

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