Questions tagged [string-interning]
A string pool allows a runtime to save memory by preserving immutable strings in a pool, so that instances of common strings can be reused throughout the application instead of creating multiple instances of them.
string-interning
165
questions
0
votes
1
answer
85
views
Why has the intern method changed in different versions?
String s1 = new String("1") + new String("1");
s1.intern();
String s2 = "11";
System.out.println(s1 == s2);//jdk6:false;jdk7:true;jdk8:true;jdk17:false
The same code ...
1
vote
2
answers
174
views
How do you use Python's string interning?
I was curious about the performance benefits on dictionary lookups when using Python's sys.intern. As a toy example to measure that, I implemented the following code snippets:
Without interning:
...
-3
votes
2
answers
281
views
How does String Intern work ( C# vs Java )?
I know that the string interning optimization is in both C# and Java.
When I try these two codes using Java:
public static void main(String[] args) {
char p[]={'h','e','e','l','l','l','l'}; // ...
1
vote
3
answers
158
views
Java String intern function and ==
Recently I'm learning Hotspot JVM. When learning the string constant pool and String intern function, I encountered a very weird situation. After browsing a lot of answers, I still can’t explain this ...
2
votes
0
answers
49
views
Python multiple strings point to same heap memory object if value have less than 4 DIFFERENT characters [duplicate]
in addition to the answers provided here
For example :
q = "asdasdasdsadsadsadsadsadsadsadsadsad"
a = "asdasdasdsadsadsadsadsadsadsadsadsad"
>>> a is q
True
even though ...
0
votes
0
answers
40
views
Are Strings Interned When Used As Return Values in Switch Statements?
Say I write a switch statement where the return value is a string. Are the strings that this switch statements return interned (since they may be referenced many times)?
Say we have an enumeration ...
-1
votes
1
answer
38
views
Does python use interning on generator objects? Weird behaviour with (i for i in range(n)) [duplicate]
Why do these two loops not give the same result? (Yes, I know the second version is bad style. But I'd still expect it to give the same output.)
gen1 = range(3)
gen2 = range(3)
print("First ...
3
votes
1
answer
109
views
How to create the str "1" at two different memory locations?
We are able to defeat the small integer intern in this way (a calculation allows us to avoid the caching layer):
>>> n = 674039
>>> one1 = 1
>>> one2 = (n ** 9 + 1) % (n ** ...
1
vote
1
answer
174
views
Javascript: Why does equality of strings take longer for longer strings?
In the following code, 1Mil strings of equal length get created.
Then they are looped through to find a matching string.
The first run has strings that are 3 times longer than the second run.
The ...
-1
votes
3
answers
848
views
Why isn't `str(1) is '1'` `True` in Python? [closed]
I'm not asking about the difference between == and is operators! I am asking about interning or something..!
In Python 3.9.1,
>>> str(1) is '1'
<stdin>:1: SyntaxWarning: "is" ...
0
votes
1
answer
89
views
x[0], y[0], z[0] has the same memory address in cpython, but why a is not?
(env) λ python
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" ...
0
votes
1
answer
247
views
What is the best way to evict unused records from string pool?
I am implementing a cache in Golang. Let's say the cache could be implemented as sync.Map with integer key and value as a struct:
type value struct {
fileName string
functionName string
}
...
1
vote
2
answers
270
views
Simplest way to defeat string interning in Python [duplicate]
I have a case where I would like to defeat string interning.
Let's say the string I have is "foo bar".
I know of a few hacky/not obvious ways to defeat interning a string. All involve ...
1
vote
1
answer
192
views
Why a string is interned that is created via constructor and with a char[]?
I was sure that this would output false, but actually it outputs true:
string foo = new string("foo".ToCharArray());
Console.WriteLine(string.IsInterned(foo) != null); // true
I thought ...
1
vote
4
answers
1k
views
How many Strings are getting created with the new operator [duplicate]
How many Strings are getting created with new operator.
let say I am creating a string with new operator.
String str = new String("Cat")
Will it create 2 strings one in heap and other one ...