Java does something similar to reduce object creation when the value of a primitive type such as 'int' is assigned to a variable of its class equivalent 'Integer'; the runtime always creates a set of reusable (and hopefully immutable!) Integer objects for small values.
E.g.
int a = 5; // Ordinary primitive
Integer b = new Integer(5); // New object created explicitly
Integer c = 5; // Prepared object for '5' is recalled/reused
Integer d = 555; // New object created automatically
I didn't know that Python did that, but I'm definitely passing that article on to a friend who's learning it (first programming language!). It's probably common to most runtimes which perform some sort of automatic conversion/creation.
Even more fun, you can even change the toString() output of ints without trouble. The chars used for toString()ing are stored in an array (digits field) which can be updated, so that you can have int 0 output as "7", for example, and then 10 as "17".
I can dig up the code for this if you want to see, but it's also fun to figure out on one's own.
Mm, I do already use Field.setAccessible, Field.get, Field.set to mess with some library classes (mostly for hooking stuff which doesn't provide enough built-in ways to hook it), which should also be able to change the value of a private final field given its name.
I didn't consider methods before, but now that you mention it I really want a way to get/set a method implementation directly. The best would be if it worked with lambdas, though that will never work unless/until a method signature can be described by referring to an existing method rather than needing a separate interface for it (that being a separate feature I already knew I wanted). The dream would be e.g.:
Integer.class.getMethod("toString").set( obj -> "Object representing the number " + obj.intValue() );
True. There is something to be said for making such errors too spectacular to ignore. Subtly, gradually corrupting a database is not exactly an improvement.
I honestly don't know but I am guessing it is because integers are Python objects, so when an integer constant appears in the source code the interpreter has to get a data structure representing this integer. This would allow methods to be called on the object.
It does seem a little crazy to me. Fetching a data structure when the processor can deal with an int using a single instruction and memory location seems just plain bloat.
Saying that, I did all my assembly programming when there was no such thing as an L2 cache.
10 comments
3 u/SithEmpire 10 Sep 2017 11:43
Java does something similar to reduce object creation when the value of a primitive type such as 'int' is assigned to a variable of its class equivalent 'Integer'; the runtime always creates a set of reusable (and hopefully immutable!) Integer objects for small values.
E.g.
I didn't know that Python did that, but I'm definitely passing that article on to a friend who's learning it (first programming language!). It's probably common to most runtimes which perform some sort of automatic conversion/creation.
2 u/Phen 10 Sep 2017 12:58
Nothing is immutable with malicious reflection! Just nudge the prival final
valuefield to sow great confusion. http://www.docjar.com/html/api/java/lang/Integer.java.htmlEven more fun, you can even change the toString() output of
ints without trouble. The chars used for toString()ing are stored in an array (digitsfield) which can be updated, so that you can have int 0 output as "7", for example, and then 10 as "17".I can dig up the code for this if you want to see, but it's also fun to figure out on one's own.
0 u/SithEmpire 10 Sep 2017 14:51
Mm, I do already use
Field.setAccessible,Field.get,Field.setto mess with some library classes (mostly for hooking stuff which doesn't provide enough built-in ways to hook it), which should also be able to change the value of a private final field given its name.I didn't consider methods before, but now that you mention it I really want a way to get/set a method implementation directly. The best would be if it worked with lambdas, though that will never work unless/until a method signature can be described by referring to an existing method rather than needing a separate interface for it (that being a separate feature I already knew I wanted). The dream would be e.g.:
0 u/Atarian 10 Sep 2017 13:40
Forgive my ignorance, but what is the benefit of doing it this way? Is it for speed?
0 u/littul_kitton 10 Sep 2017 16:44
Yes. It greatly reduces the footprint in the cache and TLB.
0 u/Atarian 10 Sep 2017 21:46
Seems like the sort of thing that could cause chaos with a bad memory write. The chances of that happening are very slim though, I guess.
1 u/littul_kitton 10 Sep 2017 21:59
True. There is something to be said for making such errors too spectacular to ignore. Subtly, gradually corrupting a database is not exactly an improvement.
0 u/svipbo [OP] 12 Sep 2017 18:20
I honestly don't know but I am guessing it is because integers are Python objects, so when an integer constant appears in the source code the interpreter has to get a data structure representing this integer. This would allow methods to be called on the object.
1 u/Atarian 12 Sep 2017 18:29
It does seem a little crazy to me. Fetching a data structure when the processor can deal with an int using a single instruction and memory location seems just plain bloat.
Saying that, I did all my assembly programming when there was no such thing as an L2 cache.
0 u/InfoTeddy 10 Dec 2017 07:26
python ints automatically resize so they can't overflow, that's probably one of the reasons why ints are data structures in python