gsuberland,
@gsuberland@chaos.social avatar

made some code use 7x less memory and 7x faster by allocating up to 32 times more things.

computers are weird.

gsuberland,
@gsuberland@chaos.social avatar

the TL;DR is that .NET objects actually have a fair bit of metadata on the heap, which takes up space if the object's fields are small. if the object is mostly just a wrapper around two other object references, then you end up with 72 bytes allocated just for runtime metadata. in my case, by the time I counted other fields, it's 148 bytes per object. by changing how that object is represented (in this case storing all the elements of a range rather than the range description itself) it's smaller

gsuberland,
@gsuberland@chaos.social avatar

checking whether an element is in a list of ranges took a long time because you needed to execute a lambda for all entries until you found a matching one, so worst case O(n), and the lambda itself required a few calls (and pointer dereferences internally).

the expanded variant allowed the items in the range to packed into unsigned integers, which could be placed into a HashSet instead, which has O(1) complexity for lookups, making it ~7x faster.

jannem,
@jannem@fosstodon.org avatar

@gsuberland I believe Python takes 24 bytes per element in a list (AFAIK they're always references).

Probably overall well motivated from an ergonomics perspective but it kills cache locality. Often worth using numpy arrays even for things that have nothing to do with math.

ignaloidas,
@ignaloidas@not.acu.lt avatar

@jannem @gsuberland everything is always a reference in Python.

One fun optimization that it does is that it pre-creates integers from -5 to 256 so that they don't need to be created while running code.

gsuberland,
@gsuberland@chaos.social avatar

@ignaloidas @jannem redis does the same thing with a large range of numbers and certain string constants for the same reason. reference deduplication.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • DreamBathrooms
  • mdbf
  • ethstaker
  • magazineikmin
  • GTA5RPClips
  • rosin
  • thenastyranch
  • Youngstown
  • osvaldo12
  • slotface
  • khanakhh
  • kavyap
  • InstantRegret
  • Durango
  • provamag3
  • everett
  • cisconetworking
  • Leos
  • normalnudes
  • cubers
  • modclub
  • ngwrru68w68
  • tacticalgear
  • megavids
  • anitta
  • tester
  • JUstTest
  • lostlight
  • All magazines