Git Product home page Git Product logo

Comments (6)

oldratlee avatar oldratlee commented on May 16, 2024

另外一个思路做法是
通过编译过程(如编译插件)来完(一些)成手动做的内联动作。

from fastjson2.

wenshao avatar wenshao commented on May 16, 2024

合并重复代码还是谨慎一些。你想合并哪些?可以提一个上来看看

from fastjson2.

kraity avatar kraity commented on May 16, 2024

合并重复代码还是谨慎一些。你想合并哪些?可以提一个上来看看

我的意思不是合并重复代码, 而是对重复的代码在它所在方法上优化改动, 比如
JSONReaderASCII.java#L718

            if (b == ',') {
                this.offset = offset + 1;

                // inline next
                ch = (char) bytes[this.offset++];
                while (ch <= ' ' && ((1L << ch) & SPACE) != 0) {
                    if (this.offset >= end) {
                        ch = EOI;
                    } else {
                        ch = (char) bytes[this.offset++];
                    }
                }
            } else {
                this.offset = offset + 1;
                this.ch = (char) b;
            }

this.offset = offset + 1; 完全可以提前, 不用重复.

JSONReaderASCII.java#L603

_for:
            for (int i = 0; ; ++i) {
                byte c = bytes[offset];
                if (c == '\\') {
                    valueEscape = true;
                    c = bytes[++offset];
                    switch (c) {
                        case 'u': {
                            offset += 4;
                            break;
                        }
                        case 'x': {
                            offset += 2;
                            break;
                        }
                        default:
                            // skip
                            break;
                    }
                    offset++;
                    continue;
                }

                if (c == quote) {
                    valueLength = i;
                    break _for;
                }
                offset++;
            }

可以把 break _for; 优化为 break; 从而 _for label 可以去掉, 这里的 _for label 在很多地方都内联导致重复.

JSONWriterUTF8.java#L309

} else if (ch >= '\uD800' && ch < ('\uDFFF' + 1)) { //  //Character.isSurrogate(c)
                final int uc;
                if (ch >= '\uD800' && ch < ('\uDBFF' + 1)) { // Character.isHighSurrogate(c)
                    if (chars.length - i < 2) {
                        uc = -1;
                    } else {
                        char d = chars[i + 1];
                        // d >= '\uDC00' && d < ('\uDFFF' + 1)
                        if (d >= '\uDC00' && d < ('\uDFFF' + 1)) { // Character.isLowSurrogate(d)
                            uc = ((ch << 10) + d) + (0x010000 - ('\uD800' << 10) - '\uDC00'); // Character.toCodePoint(c, d)
                        } else {
//                            throw new JSONException("encodeUTF8 error", new MalformedInputException(1));
                            bytes[off++] = (byte) '?';
                            continue;
                        }
                    }
                } else {
                    //
                    if (ch >= '\uDC00' && ch < ('\uDFFF' + 1)) { // Character.isLowSurrogate(c)
                        bytes[off++] = (byte) '?';
                        continue;
//                        throw new JSONException("encodeUTF8 error", new MalformedInputException(1));
                    } else {
                        uc = ch;
                    }
                }
                if (uc < 0) {
                    bytes[off++] = (byte) '?';
                } else {
                    bytes[off++] = (byte) (0xf0 | ((uc >> 18)));
                    bytes[off++] = (byte) (0x80 | ((uc >> 12) & 0x3f));
                    bytes[off++] = (byte) (0x80 | ((uc >> 6) & 0x3f));
                    bytes[off++] = (byte) (0x80 | (uc & 0x3f));
                    i++; // 2 chars
                }

这里的 ch >= '\uD800' 重复判断, uc = -1; 这里可以直接 continue;, 这里编码部分很多地方都有内联一样代码, 都是是可以优化.

我的疑惑是: 是否为了内联代码一致, 而不根据代码所在的环境下优化变动.

from fastjson2.

wenshao avatar wenshao commented on May 16, 2024

应该要根据所在环境进行优化,造成现状是因为手工内联的时候,没有做完根据上下文优化的事情,看到就提PR改进吧。

from fastjson2.

kraity avatar kraity commented on May 16, 2024

另外一个思路做法是 通过编译过程(如编译插件)来完(一些)成手动做的内联动作。

我也赞同您的观点, 通过编译时内联, 省去手工也还可以保障代码一致

from fastjson2.

kraity avatar kraity commented on May 16, 2024

应该要根据所在环境进行优化,造成现状是因为手工内联的时候,没有做完根据上下文优化的事情,看到就提PR改进吧。

好的谢谢温少, 我明白了您的观点。

from fastjson2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.