Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 729 730 [731] 732 733 ... 795

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 815207 times)

strainer

  • Bay Watcher
  • Goatherd
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10950 on: May 17, 2018, 07:23:01 am »

Rolling a custom allocated string type may not often be productive, if we view that escaping of extension pointers as a form of compression - its not likely to produce much of a compression ratio unless the fixed space for strings is very small.

Quote
loading each entire entity into a memory page: very slow)
There is a relevant idea of AoS vs SoA data storage. Array of Structs can be convenient but spaces individual attributes out in memory, essentially interleaves all the arrays through memory. Struct of Array maximises locality of individual attributes.  eg, if you have bools requiring lots of access it is optimal to keep them in their own array or right along with some other info needed in the same critical calculations.
Logged
Klok the Kloker !

Eschar

  • Bay Watcher
  • hello
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10951 on: May 17, 2018, 09:57:25 pm »

Pygame question: what is the name of the default font that pygame uses if it can't find the one you pass to pygame.font.Font?
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10952 on: May 17, 2018, 10:40:56 pm »

Rolling a custom allocated string type may not often be productive, if we view that escaping of extension pointers as a form of compression - its not likely to produce much of a compression ratio unless the fixed space for strings is very small.

The reason it works out is that the "small-string" is held on the stack instead of the heap, meaning that you avoid loading the different memory page whenever you access the string. What matters is the proportion of strings that have different sizes. e.g. fbstring used by facebook avoids heap calls for all strings up to 23 characters in length. Note that Microsoft's string implementation in Visual C++ is already 24 bytes long, so it takes up the same space in the stack as fbstring does, except it doesn't have this optimization.

So (1) it's saving space overall but mainly (2) it's avoiding loading an entire memory page (64 bytes of data) on every string operation for strings up to 23 chars long, which can in theory cut memory access by over 50% on those calls. e.g. the fbstring needs an if-statement to decide whether it's a short-string or a long-string, so it should be slower than gcc string, which doesn't have the if-statement. However, fbstring tells you the size of a typical string in 0.9 nanoseconds vs gcc-string in 1.6 nano-seconds, and that's even after the if-statement is taken into account. e.g. they're actually seeing that 50% speedup on memory accesses because most strings tend to be small.

e.g.: what's the most common string? The empty string. How big is this string? One byte big: because you need a null terminator. fbstring or similar home-rolled strings allow you to avoid doing the "malloc" type operation every time the default string constructor is called, because the whole string is created in-situ on the stack.
« Last Edit: May 17, 2018, 10:51:06 pm by Reelya »
Logged

bloop_bleep

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10953 on: May 18, 2018, 12:31:57 am »

I wonder if it is possible to write an implementation of std::set or std::map in such a way that entire large blocks of data are allocated at once (as std::vector does), as opposed to each individual element, and then arrange the elements within the blocks of data such that nodes closer in the tree are closer to each other in memory than nodes far apart in the tree. That way when the CPU loads a page into the cache to locate a parent node, it could hopefully find the child nodes within the same page. This setup reminds me heavily of a heap (data structure, not location in memory), but I'm not sure if this could work with RB trees since they can have "empty spaces" (that is, nodes in the middle of the tree that have  1 or 0 children) unlike heaps.
Logged
Quote from: KittyTac
The closest thing Bay12 has to a flamewar is an argument over philosophy that slowly transitioned to an argument about quantum mechanics.
Quote from: thefriendlyhacker
The trick is to only make predictions semi-seriously.  That way, I don't have a 98% failure rate. I have a 98% sarcasm rate.

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10954 on: May 18, 2018, 12:34:44 am »

Yeah, usually the use case of a map is when you have a completely random lookup range. Hard to pre-allocate that.
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #10955 on: May 18, 2018, 11:53:02 am »

std::unordered_map and std::unordered_set use buckets instead of a rb-trees, and can be reserved, so may be a better fit for that kind of data.

As always I advocate just using what fits semantically first, so if you want a list of items to iterate over use std::vector (not list by default, because Bjarne Stroustrup says so :P).

Likewise, if you want key-value pairs use std::map or std::unordered_map. Not sure which of those should be seen as the default, actually.

Then if profiling reveals performance issues, then you can start to move towards using list instead of vector, or trying different map or set types, or the non-semantic uses of different data structures entirely. I find it to be something of a bad habit to assume you know better than the compiler/standard library nowadays.
« Last Edit: May 18, 2018, 12:16:56 pm by MorleyDev »
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #10956 on: May 18, 2018, 01:31:04 pm »

An argument I think I've seen made before is that std::map should more be thought of as std::ordered_map, so used in the situations where you actually care about being able to iterate over the map with your keys ordered, and to otherwise default to std::unordered_map where the order of iteration is unimportant (which is probably going to be most of the situations). So unordered_map is really closer to the standard use of map, whilst map is the exceptional use-case.

Replace map with set and second verse same as the first.
« Last Edit: May 18, 2018, 01:32:50 pm by MorleyDev »
Logged

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10957 on: May 21, 2018, 04:24:03 am »

when you think you know javascript




Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10958 on: May 21, 2018, 04:32:02 am »

"+" can mean "positive" similar to how a minus sign before a number can mean "negative", so in this case it means 'a' plus 'positive b'. This is a standard thing, and not specific to JavaScript. When you have ++ that's a single operator and '+ +' is two separate operators, which are evaluated separately for their semantic meaning.

e.g. the first '+' is evaluated to mean string concatenation, because the type of addition depends on what's on the LHS of the operator in JavaScript. Then, the second '+' is taken to be the start of what's being concatenated, which is "positive 'b'", meaning that 'b' needs to be converted to numeric, then the positive value evaluated, which is NaN.
« Last Edit: May 21, 2018, 04:42:38 am by Reelya »
Logged

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10959 on: May 21, 2018, 07:53:43 am »

well, the unary plus gets evaluated first for being an unary operator, then the concatenation proceeds left to right, but confusing enough.
Logged

strainer

  • Bay Watcher
  • Goatherd
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10960 on: May 21, 2018, 09:27:49 am »

I like being able hint the interpreter to initialise a number as 'Real' (double float) in JS with the value -0 . A plain 0 will initialise as a 32 bit int. This can make a significant difference to performance of crucial routines.
Logged
Klok the Kloker !

eerr

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10961 on: May 21, 2018, 06:16:49 pm »

https://gyazo.com/1956ff0658fecc47fc0636d654a6c9d2
Does anybody know what this noVNC icon means? I don't think I installed any new software lately.

Edit: Never mind, it's not a virtual network installed it's just a favorites bar thing.
« Last Edit: May 21, 2018, 06:19:17 pm by eerr »
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #10962 on: May 24, 2018, 11:23:37 am »

I like being able hint the interpreter to initialise a number as 'Real' (double float) in JS with the value -0 . A plain 0 will initialise as a 32 bit int. This can make a significant difference to performance of crucial routines.

Fun fact, this and | 0 are used by asm.js to indicate floats and integers.

Though I guess asm.js isn't particularly relevant anymore now webassembly is like....an official 'thing'. So now you write code in BrainFuck and compile and run it in the browser! And some less important languages too :P
« Last Edit: May 24, 2018, 11:25:21 am by MorleyDev »
Logged

strainer

  • Bay Watcher
  • Goatherd
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10963 on: May 24, 2018, 04:10:13 pm »

I had admired asm.js approach, maybe the sneaky interpreter hinting which made it quick will become depreciated by the footprint of the abominable WASM arrrg!
Logged
Klok the Kloker !

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #10964 on: June 01, 2018, 03:26:29 pm »

My workplace is finally getting around to doing our big Python 3 transition. And when I say "my workplace", I mean me.

What happens when you use undocumented features? Your coworker gets to sift through all of your code after you quit, fixing it as he goes.
Logged
Pages: 1 ... 729 730 [731] 732 733 ... 795