News for hackers. Stuff that matters.
Re:Copying data is wasteful, mutating data is dangerous by xg15 on Thursday January 09, 2020 @12:45PM (#22003501)
This seems like a problem that would be suited for an optimizing compiler.

You could imagine some hypothetical language in which all objects are immutable, as far as the programmer is concerned - however the compiler is allowed to reuse objects behind the scenes if there is provably no way that a side-effect could be introduced.

E.g., in such a language, the first variant would be the only legal form to write the normalizing function:

  low = array1.min()
  high = array1.max()
  array2 = array1 - low
  array3 = array2 / (high - low)
  return array3
However, this could be safely turned into the following during compilation:

  low = array1.min()
  high = array1.max()
  array2 = array1 - low
  array2 /= (high - low)
  return array2
... because array2 is not visible outside the function and therefore could not cause side-effects.
Re:Copying data is wasteful, mutating data is dangerous by jerf on Thursday January 09, 2020 @02:04PM (#22004319)
"You could imagine some hypothetical language in which all objects are immutable, as far as the programmer is concerned - however the compiler is allowed to reuse objects behind the scenes if there is provably no way that a side-effect could be introduced."

There are functional languages that do this. Like many other optimizations, you get less than your intuition might naturally expect, but it does work.

A trivial case is "tail call optimization", which most immediately involves not creating a literal in-memory stack record for recursive invocations but also can easily involve the compiler re-using the memory for the arguments. IIRC Erlang will do that as much as it can in tight recursive calls.

Re:Copying data is wasteful, mutating data is dangerous by toast0 on Thursday January 09, 2020 @03:22PM (#22005235)
> You could imagine some hypothetical language in which all objects are immutable, as far as the programmer is concerned - however the compiler is allowed to reuse objects behind the scenes if there is provably no way that a side-effect could be introduced.

Erlang/BEAM fits your hypothetical to some degree. Erlang the language has immutable variables; however, BEAM, the virtual machine, has mutable registers. Whether or not a given code sample results in copy or mutation depends on what information is available to the compiler, of course.

Re:Copying data is wasteful, mutating data is dangerous by zzzeek on Thursday January 09, 2020 @02:02PM (#22004299)
it is theoretically possible that a library like numpy or pandas could do this directly. That is, if you take each of these transformation operations and store them, but not actually run them until needed, that is, when someone accesses the individual array values, you only need to make one copy and there is no impact on the programmer to have to worry about this.

I was doing something similar for SQLAlchemy recently. SQLAlchemy is all about capturing Python operators and operands into data structures that represent intent which can be invoked later.

edit: here is a demonstration: https://gist.github.com/zzzeek/caa4a7ed94f326fbbc031acecb9d7...

edit edit: it is actually possible with numpy by using the Dask library: https://dask.org/

Re:Copying data is wasteful, mutating data is dangerous by masklinn on Thursday January 09, 2020 @02:05PM (#22004325)
The problem with the library doing this is you end up with the same issue as in Haskell: you accumulate thunks and both memory use and location of CPU hotspots becomes much harder to predict and troubleshoot.
Re:Copying data is wasteful, mutating data is dangerous by zzzeek on Thursday January 09, 2020 @02:23PM (#22004540)
so...turn off the "thunking" feature while you are debugging.
Re:Copying data is wasteful, mutating data is dangerous by JeremyBanks on Thursday January 09, 2020 @02:57PM (#22004961)
That won't help when you're debugging the performance implications of thunking.
Re:Copying data is wasteful, mutating data is dangerous by zzzeek on Thursday January 09, 2020 @03:52PM (#22005577)
compare memory / callcounts / time spent with thunking turned on vs. off would give a good idea of it.

i can say for my side of things thunking is a super huge performance gain as it allows for quick gathering of expression intent and then the computation side on the other side of a cache.

Re:Copying data is wasteful, mutating data is dangerous by vmchale on Thursday January 09, 2020 @04:37PM (#22006104)
You can use Futhark; it compiles to PyOpenCL and then can be called by python fairly fluently.

Accelerate is a nice example of an array library with a JIT backend.

Re:Copying data is wasteful, mutating data is dangerous by oefrha on Thursday January 09, 2020 @12:53PM (#22003586)
Alas, CPython isn’t an optimizing compiler, it faithfully compiles code into bytecode which is then faithfully executed.

I don’t know the state of NumPy on PyPy since I don’t use PyPy.

Re:Copying data is wasteful, mutating data is dangerous by anoncake on Thursday January 09, 2020 @03:20PM (#22005216)
It does have a peephole optimizer.
Re:Copying data is wasteful, mutating data is dangerous by masklinn on Thursday January 09, 2020 @01:05PM (#22003701)
numba.jit might be able to optimise the extra copies away though, I'm reasonably sure working with numpy arrays is a pretty significant use case.
Re:Copying data is wasteful, mutating data is dangerous by ben509 on Friday January 10, 2020 @01:22AM (#22008765)
There's another project, Pythran, that does that.

I'm not sure if it's doing inlining before the laziness analysis, which would be ideal.

[1]: https://pythran.readthedocs.io/en/latest/INTERNAL.html#lazyn...