string-interning

Are strings pooled in Python

北城以北 提交于 2019-11-27 15:07:53
Does Python have a pool of all strings and are they (strings) singletons there? More precise, in the following code one or two strings were created in memory: a = str(num) b = str(num) ? Strings are immutable in Python, so the implementation can decide whether to intern (that's a term often associated with C#, meaning that some strings are stored in a pool) strings or not. In your example, you're dynamically creating strings. CPython does not always look into the pool to detect whether the string is already there - it also doesn't make sense because you first have to reserve memory in order to

Is string interning really useful?

元气小坏坏 提交于 2019-11-27 14:35:41
问题 I was having a conversation about strings and various languages a while back, and the topic of string interning came up. Apparently Java and the .NET framework do this automatically with all strings, as well as several scripting languages. Theoretically, it saves memory because you don't end up with multiple copies of the same string, and it saves time because string equality comparisons are a simple pointer comparison instead of an O(N) run through each character of the string. But the more

How is Java's String#intern() method implemented?

不羁岁月 提交于 2019-11-27 03:37:46
问题 I tried to look at Java's String#intern() method, but it's public native String intern(); In general, how is interning implemented? In String's case? 回答1: For Sun Java, start with JVM_InternString , on line ~3639 of jvm.cpp. Technically, the actual String method is in java/lang/String.c, but it immediately calls JVM_InternString . You can continue to StringTable::intern in symbolTable.cpp . In a more abstract sense, the purpose of interning is to map equivalent strings to a single canonical

When to use intern() on String literals

我是研究僧i 提交于 2019-11-27 03:23:55
I see a lot of legacy code like this: class A { public static final String CONSTANT = "value".intern(); ... } I don't see any reason for the intern(), as in the Javadoc one can read: "All literal strings and string-valued constant expressions are interned." Is there some intent of this, maybe in past revisions of the language? kdgregory This is a technique to ensure that CONSTANT is not actually a constant. When the Java compiler sees a reference to a final static primitive or String, it inserts the actual value of that constant into the class that uses it. If you then change the constant

Why are the results of of str == str.intern() for these strings different?

对着背影说爱祢 提交于 2019-11-27 02:46:09
问题 public static void main(String[] args) { String str1 = new StringBuilder("计算机").append("软件").toString(); System.out.println(str1.intern() == str1); String str2 = new StringBuffer("ja").append("va").toString(); System.out.println(str2.intern() == str2); } Results: true false First one prints true , and the second prints false . Why are the results different? 回答1: The difference in behavior is unrelated to the differences between StringBuilder and StringBuffer . The javadoc of String#intern()

Does Python intern strings?

回眸只為那壹抹淺笑 提交于 2019-11-26 22:58:50
In Java, explicitly declared Strings are interned by the JVM, so that subsequent declarations of the same String results in two pointers to the same String instance, rather than two separate (but identical) Strings. For example: public String baz() { String a = "astring"; return a; } public String bar() { String b = "astring" return b; } public void main() { String a = baz() String b = bar() assert(a == b) // passes } My question is, does CPython (or any other Python runtime) do the same thing for strings? For example, if I have some class: class example(): def __init__(): self._inst =

When does python choose to intern a string [duplicate]

为君一笑 提交于 2019-11-26 21:01:37
This question already has an answer here: About the changing id of an immutable string 5 answers >>> s1 = "spam" >>> s2 = "spam" >>> s1 is s2 True >>> q = 'asdalksdjfla;ksdjf;laksdjfals;kdfjasl;fjasdf' >>> r = 'asdalksdjfla;ksdjf;laksdjfals;kdfjasl;fjasdf' >>> q is r False How many characters should have to s1 is s2 give False ? Where is limit? I.e., I am asking how long a string has to be before python starts making separate copies of it. String interning is implementation specific and shouldn't be relied upon, use equality testing if you want to check two strings are identical. If you want,

String interning in .Net Framework - What are the benefits and when to use interning

我怕爱的太早我们不能终老 提交于 2019-11-26 20:44:48
I want to know the process and internals of string interning specific to .Net framework . Would also like to know the benefits of using interning and the scenarios/situations where we should use string interning to improve the performance. Though I have studied interning from the Jeffery Richter's CLR book but I am still confused and would like to know it in more detail. [Editing] to ask a specific question with a sample code as below: private void MethodA() { string s = "String"; // line 1 - interned literal as explained in the answer //s.intern(); // line 2 - what would happen in line 3 if

Are strings pooled in Python

佐手、 提交于 2019-11-26 18:28:30
问题 Does Python have a pool of all strings and are they (strings) singletons there? More precise, in the following code one or two strings were created in memory: a = str(num) b = str(num) ? 回答1: Strings are immutable in Python, so the implementation can decide whether to intern (that's a term often associated with C#, meaning that some strings are stored in a pool) strings or not. In your example, you're dynamically creating strings. CPython does not always look into the pool to detect whether

Behavior of String literals is confusing

耗尽温柔 提交于 2019-11-26 14:26:31
问题 The behavior of String literals is very confusing in the code below. I can understand line 1, line 2, and line 3 are true , but why is line 4 false ? When I print the hashcode of both they are the same. class Hello { public static void main(String[] args) { String hello = "Hello", lo = "lo"; System.out.print((Other1.hello == hello) + " "); //line 1 System.out.print((Other1.hello == "Hello") + " "); //line 2 System.out.print((hello == ("Hel"+"lo")) + " "); //line 3 System.out.print((hello == (