Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit50c3afb

Browse files
author
Douglas Crockford
committed
keyPool
1 parent1b5a59f commit50c3afb

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

‎JSONObject.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,21 @@ of this software and associated documentation files (the "Software"), to deal
9191
* </ul>
9292
*
9393
* @author JSON.org
94-
* @version 2012-10-27
94+
* @version 2012-12-01
9595
*/
9696
publicclassJSONObject {
97+
/**
98+
* The maximum number of keys in the key pool.
99+
*/
100+
privatestaticfinalintkeyPoolSize =100;
101+
102+
/**
103+
* Key pooling is like string interning, but without permanently tying up
104+
* memory. To help conserve memory, storage of duplicated key strings in
105+
* JSONObjects will be avoided by using a key pool to manage unique key
106+
* string objects. This is used by JSONObject.put(string, object).
107+
*/
108+
privatestaticHashMapkeyPool =newHashMap(keyPoolSize);
97109

98110
/**
99111
* JSONObject.NULL is equivalent to the value that JavaScript calls null,
@@ -1110,11 +1122,21 @@ public JSONObject put(String key, Map value) throws JSONException {
11101122
* or if the key is null.
11111123
*/
11121124
publicJSONObjectput(Stringkey,Objectvalue)throwsJSONException {
1125+
Stringpooled;
11131126
if (key ==null) {
11141127
thrownewJSONException("Null key.");
11151128
}
11161129
if (value !=null) {
11171130
testValidity(value);
1131+
pooled = (String)keyPool.get(key);
1132+
if (pooled ==null) {
1133+
if (keyPool.size() >=keyPoolSize) {
1134+
keyPool =newHashMap(keyPoolSize);
1135+
}
1136+
keyPool.put(key,key);
1137+
}else {
1138+
key =pooled;
1139+
}
11181140
this.map.put(key,value);
11191141
}else {
11201142
this.remove(key);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp