FasterXML / FasterXML/jackson-core

Allow automatic canonicalization (possibly `String.intern()`) of `JsonToken.VALUE_STRING` values

Open
#726 3 comments 0 reactions 0 assignees View on GitHub
pr-needed
Dominant language
Java
Stars
2.4k
Forks
928
Avg merge
2d 18h
Merged PRs (30d)
24

Description

## Version
2.12.4

## Feature request

#### Existing scenario
I think the `INTERN_FIELD_NAMES` flag is great thing to have, which is turned on by default. This would save greatly on the memory footprint especially when the message size is huge, imagine ~3.2 million position messages, all with same key like 'portfolio', 'book'.
Instead of having ~3.2 million of `portfolio` in parsing each batch of message in heap, with `INTERN_FIELD_NAMES`, it would result in only one `portfolio` on the string pool regardless of the message sizes, being ~3.2 million or even more.

#### Changes proposed
A similar feature flag could be provided, even turned on by default as well, when parsing the values.
So that, back to the ~3.2 million records example, instead of having ~3.2 million portfolio names in the heap, the similar feature flag would result in only around ~200 `portfolio name` (like Jason, Jackson) in the string pool.

## Possible changes
From here, it could take in the feature flag, and apply the intern if the flag is on
https://github.com/FasterXML/jackson-core/blob/2.14/src/main/java/com/fasterxml/jackson/core/util/TextBuffer.java#L797

```
public String setCurrentAndReturn(int len) {
_currentSize = len;
// We can simplify handling here compared to full `contentsAsString()`:
if (_segmentSize > 0) { // longer text; call main method
return contentsAsString();
}
// more common case: single segment
int currLen = _currentSize;
String str = (currLen == 0) ? "" : new String(_currentSegment, 0, currLen);
if (JsonFactory.Feature.`INTERN_FIELD_VALUES`.enabledIn(_flags)) {
str = InternCache.instance.intern(str );
}
_resultString = str;
return str;
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.