string-interning

When not to use to_sym in Ruby?

对着背影说爱祢 提交于 2021-01-28 03:09:28
问题 I have a large dataset from an analytics provider. It arrives in JSON and I parse it into a hash, but due to the size of the set I'm ballooning to over a gig in memory usage. Almost everything starts as strings (a few values are numerical), and while of course the keys are duplicated many times, many of the values are repeated as well. So I was thinking, why not symbolize all the (non-numerical) values, as well? I've found some discusion of potential problems, but I figure it would be nice to

The return of String.intern() explained

浪尽此生 提交于 2020-01-30 04:07:37
问题 Consider: String s1 = new StringBuilder("Cattie").append(" & Doggie").toString(); System.out.println(s1.intern() == s1); // true why? System.out.println(s1 == "Cattie & Doggie"); // true another why? String s2 = new StringBuilder("ja").append("va").toString(); System.out.println(s2.intern() == s2); // false String s3 = new String("Cattie & Doggie"); System.out.println(s3.intern() == s3); // false System.out.println(s3 == "Cattie & Doggie"); // false I got confused why they are resulting

The return of String.intern() explained

邮差的信 提交于 2020-01-30 04:07:11
问题 Consider: String s1 = new StringBuilder("Cattie").append(" & Doggie").toString(); System.out.println(s1.intern() == s1); // true why? System.out.println(s1 == "Cattie & Doggie"); // true another why? String s2 = new StringBuilder("ja").append("va").toString(); System.out.println(s2.intern() == s2); // false String s3 = new String("Cattie & Doggie"); System.out.println(s3.intern() == s3); // false System.out.println(s3 == "Cattie & Doggie"); // false I got confused why they are resulting

String.intern() vs manual string-to-identifier mapping?

和自甴很熟 提交于 2020-01-14 11:34:10
问题 I recall seeing a couple of string-intensive programs that do a lot of string comparison but relatively few string manipulation, and that have used a separate table to map strings to identifiers for efficient equality and lower memory footprint, e.g.: public class Name { public static Map<String, Name> names = new SomeMap<String, Name>(); public static Name from(String s) { Name n = names.get(s); if (n == null) { n = new Name(s); names.put(s, n); } return n; } private final String str;

String.intern() vs manual string-to-identifier mapping?

时间秒杀一切 提交于 2020-01-14 11:34:03
问题 I recall seeing a couple of string-intensive programs that do a lot of string comparison but relatively few string manipulation, and that have used a separate table to map strings to identifiers for efficient equality and lower memory footprint, e.g.: public class Name { public static Map<String, Name> names = new SomeMap<String, Name>(); public static Name from(String s) { Name n = names.get(s); if (n == null) { n = new Name(s); names.put(s, n); } return n; } private final String str;

String.intern() vs manual string-to-identifier mapping?

落爺英雄遲暮 提交于 2020-01-14 11:33:38
问题 I recall seeing a couple of string-intensive programs that do a lot of string comparison but relatively few string manipulation, and that have used a separate table to map strings to identifiers for efficient equality and lower memory footprint, e.g.: public class Name { public static Map<String, Name> names = new SomeMap<String, Name>(); public static Name from(String s) { Name n = names.get(s); if (n == null) { n = new Name(s); names.put(s, n); } return n; } private final String str;

Java - Automatic String interning within constructors

99封情书 提交于 2020-01-04 08:10:19
问题 Let's say I have a class as follows: class Apple { String apple; Apple(String apple) { this.apple = apple; } } What makes the following code true? public boolean result() { Apple a = new Apple("apple"); Apple b = new Apple("apple"); return a.apple == b.apple; } Does Java automatically intern Strings set within instances of my objects? Is the only time that Java doesn't intern Strings is when they're created using new String("...") ? EDIT : Thanks for the answers, an extension to this question

Java, HashMaps and using Strings as the keys - does the string value get stored twice?

眉间皱痕 提交于 2020-01-03 17:22:07
问题 If I have a HashMap that looks like this: HashMap<String, MyObject> where the String key is a field in MyObject , does this string value get stored twice? So when I add entries: _myMap.put(myObj.getName(), myObj); Am I using double the String size in terms of memory? Or does Java do something clever behind the scenes? Thanks 回答1: Java uses the reference, so it is just a pointer to the string that it stores twice. So you don't have to worry if your string is huge, it will still be the same

How can I do string interning in C or C++?

走远了吗. 提交于 2020-01-01 09:12:27
问题 Is there something like intern() method in C or C++ like there is in Java ? If there isn't, how can I carry out string interning in C or C++? 回答1: boost::flyweight< std::string > seems to be exactly what you're looking for. 回答2: Is there something like intern() method in C like we have in Java ? Not in the standard C library. If there isn't, how to carry out string interning in C? With great difficulty, I fear. The first problem is that "string" is not a well-defined thing in C. Instead you

How can I do string interning in C or C++?

ぃ、小莉子 提交于 2020-01-01 09:11:47
问题 Is there something like intern() method in C or C++ like there is in Java ? If there isn't, how can I carry out string interning in C or C++? 回答1: boost::flyweight< std::string > seems to be exactly what you're looking for. 回答2: Is there something like intern() method in C like we have in Java ? Not in the standard C library. If there isn't, how to carry out string interning in C? With great difficulty, I fear. The first problem is that "string" is not a well-defined thing in C. Instead you