Skip to content

performance enhancements#47

Open
jdalton wants to merge 1 commit into
nirgit:masterfrom
jdalton:jdalton/optimize
Open

performance enhancements#47
jdalton wants to merge 1 commit into
nirgit:masterfrom
jdalton:jdalton/optimize

Conversation

@jdalton

@jdalton jdalton commented Apr 25, 2024

Copy link
Copy Markdown
Collaborator

I've optimized the lib:

Before for 4,000 iterations:

avg exec time of 4000 iterations (in ms): 1.7665
...
Time: 7.21 s

After for 4,000 iterations:

avg exec time of 4000 iterations (in ms): 0.438
...
Time: 1.954 s, estimated 2 s

@jdalton jdalton force-pushed the jdalton/optimize branch 2 times, most recently from 30a727c to 5170b03 Compare April 25, 2024 12:24
@nirgit

nirgit commented Apr 26, 2024

Copy link
Copy Markdown
Owner

thanks for the PR @jdalton , i'll be sure to go over it soon

@jdalton jdalton force-pushed the jdalton/optimize branch 5 times, most recently from 67ed86f to acf1b4d Compare April 28, 2024 20:36
@jdalton

jdalton commented Apr 28, 2024

Copy link
Copy Markdown
Collaborator Author

@nirgit I updated it to keep the source readable and inline during build. I've added a way to annotate what is inlined. It allows for fine grain control and seems to be working well.

@jdalton jdalton force-pushed the jdalton/optimize branch 9 times, most recently from a6aef3c to 3be9669 Compare April 29, 2024 07:10
@nirgit

nirgit commented Apr 30, 2024

Copy link
Copy Markdown
Owner

@nirgit I updated it to keep the source readable and inline during build. I've added a way to annotate what is inlined. It allows for fine grain control and seems to be working well.

  1. thanks @jdalton for the update, and for the contribution of this PR, very much appreciated.
  2. i see that the amount of changes is getting quite big, which makes it longer/harder to review. so i suggest to perhaps stop at this point and allow a review and feedback take place.
    in case you have more changes planned, perhaps you could introduce those later on a following PR.

i'll try to go over the PR in the upcoming week.
thanks again

@jdalton jdalton marked this pull request as draft April 30, 2024 21:19
@jdalton

jdalton commented Apr 30, 2024

Copy link
Copy Markdown
Collaborator Author

@nirgit I split this PR out into #48 and #49.
Once those are merged I'll update this and convert it from DRAFT to READY for REVIEW.

@jdalton jdalton force-pushed the jdalton/optimize branch 9 times, most recently from e31a822 to c8cd0d4 Compare May 3, 2024 20:22
@jdalton jdalton force-pushed the jdalton/optimize branch 10 times, most recently from 0fd1c66 to 062161b Compare May 10, 2024 14:10
@jdalton jdalton marked this pull request as ready for review May 10, 2024 14:11
@jdalton jdalton force-pushed the jdalton/optimize branch 6 times, most recently from bd2488e to 0cc4a79 Compare May 13, 2024 18:58
@jdalton jdalton force-pushed the jdalton/optimize branch from 0cc4a79 to 68d9dfa Compare May 13, 2024 19:05

@nirgit nirgit left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

big PR.
i'd rather having this PR separated into several smaller PRs so its easier to review.
for instance:

  1. PR for converting everything to constants
  2. PR for refactoring the lexer
  3. PR for refactoring the parser

that way it'd be a whole lot easier to track, sorry.

i did not finish reviewing everything, and still need to go over the parser.js part and the astToJson.js, but since there are already some comments, i thought i'd already send those in so you can have a look, and we'll take it from there.

thanks

Comment thread test/benchmark.spec.js

const { readXMLFile } = require('./testUtils')
const { convertXML } = require('../src/xmlToJson')
const { convertXML } = require('../lib/simpleXmlToJson.min.js')

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, but this means that running npm test cannot happen without running npm run build. pls fix

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make it build before tests are run :)

Comment thread test/benchmark.spec.js
Comment on lines +10 to +11
const iterations = 4000
for (let i = 0; i < iterations; i += 1) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Comment thread src/transpiler.js

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why all this hassle? why not just rename transpiler.js to parser.js :)?

Comment thread src/parser.js

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any changes here?
if there aren't any, lets just do a rename for transpiler.js to parser.js?

Comment thread src/model.js
const Token = (type, value) => ({
type,
value
const Token = ($type, $value = '') => ({

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inlining vars is easier when they aren't shortcut properties.

Comment thread src/lexer.js
Comment on lines +14 to +15
let peekedPos = 0
let peekedTagName = ''

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused. remove?

Comment thread src/lexer.js
const isCharBlank = (char) =>
char === ' ' || char === '\n' || char === '\r' || char === '\t'
function createLexer(xmlAsString, { knownAttrib, knownElement } = {}) {
const { length } = xmlAsString

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls check if xmlAsString is null or undefined

Comment thread src/lexer.js
const scoping = []
let currScope = 0
let currToken = EOF_TOKEN
let currTokenType = TOKEN_TYPE.EOF

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this if we have currToken? we can just check currToken.type

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will for another biggie optimization after (accepting known elements and known attributes and skipping object creation and string slicing for those skipped).

Comment thread src/lexer.js
(code >= CHAR_CODE.DIGIT_0 && code <= CHAR_CODE.DIGIT_9) ||
code === CHAR_CODE.LODASH ||
code === CHAR_CODE.COLON ||
code === CHAR_CODE.HYPHEN

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think period should also be included
|| code === CHAR_CODE.PERIOD

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tag names can only start with a letter or an underscore.
so its a problematic check to include numbers and other invalid chars in the same if.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know. Currently (master branch) does allow digits and other chars:

const ELEMENT_TYPE_MATCHER = /[a-zA-Z0-9_:-]/

The inlining of the regexp reflects what is in master (more or less).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nirgit

i think period should also be included

Included in an Element tagName ?

Comment thread src/lexer.js
Comment on lines +309 to +315
: Token(
/*inline*/
TOKEN_TYPE.CONTENT,
buffer +
readAlphaNumericAndSpecialChars()
)
return currToken

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe i'm missing it, but this does not seem equivalent to the old implementation reading special chars.
i think you might not be reading the suffix appropriately, maybe i'm wrong.
can you pls double check?

image

@jdalton jdalton May 20, 2024

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'should support textual content for elements', test passes on the jdalton/optimize branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants