private MultiValueMap<String, String> parseFormData(Charset charset, String body){

        MultiValueMap<String, String> result = new LinkedMultiValueMap<>();

        try {
            String curKey = null;
            String curVal = null;
            int parsePosition = 0;
            int length = body.length();
            for (int position = 0; position < length; position++) {
                char c = body.charAt(position);
                if (c == '&') {
                    if (curKey == null) {
                        if (parsePosition != position) {
                            curKey = URLDecoder.decode(body.substring(parsePosition, position), charset.name());
                        }
                    } else {
                        curVal = URLDecoder.decode(body.substring(parsePosition, position), charset.name());
                    }
                    if (curKey != null) {
                        result.add(curKey, curVal);
                    }
                    curKey = null;
                    curVal = null;
                    parsePosition = position + 1;
                } else if (c == '=') {
                    if (parsePosition != position) {
                        curKey = URLDecoder.decode(body.substring(parsePosition, position), charset.name());
                    }
                    parsePosition = position + 1;
                }
            }
            if (curKey != null) {
                curVal = URLDecoder.decode(body.substring(parsePosition), charset.name());
                result.add(curKey, curVal);
            } else if (parsePosition != length) {
                curKey = URLDecoder.decode(body.substring(parsePosition), charset.name());
                result.add(curKey, curVal);
            }
        } catch (UnsupportedEncodingException ex) {
            throw new IllegalStateException(ex);
        }
        return result;
    }

Complete parsing at once, reducing memory allocation

Comment From: sbrannen

@sujingbo, can you provide benchmarks that support your proposed changes?

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.