Skip to content

Avoid duplicate keyword search #1066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 21, 2025

Conversation

fancycode
Copy link
Contributor

Fixes #1057

The example file contains a huge object with this content:

210891 0 obj
<</Limits [(node00000002) (node00275919)]
/Names [(node00000002) 8591 0 R (node00000006) 190838 0 R (node00000007) 66313 0 R (node00000008) 24685 0 R (node00000009) 208328 0 R (node00000010) 163561 0 R (node00000011) 27134 0 R (node00000012) 126131 0 R (node00000013) 9895 0 R (node00000014) 101713 0 R (node00000015) 64243 0 R (node00000016) 118039 0 R (node00000017)
... continues for more than 5 MBytes ...
(node00275919) 209354 0 R]>>
endobj

This blows up the parsing code in

func DetectKeywords(line string) (endInd int, streamInd int, err error) {

Here the keywords are searched lots of times after the start is advanced for each string literal inside the array (e.g. (node00000002), etc.).

The PR fixes this by searching only once for the keywords and then adjusting the position if strings / comments are found before them. In my tests the example file now parses in about 3 seconds.

@hhrutter
Copy link
Collaborator

hhrutter commented Jan 9, 2025

Sounds very cool..
Let me double check your patch of this critical code section.

@@ -1721,7 +1721,7 @@ func buffer(c context.Context, rd io.Reader) (buf []byte, endInd int, streamInd
growSize = min(growSize*2, maximumBufSize)
line := string(buf)

endInd, streamInd, err = model.DetectKeywords(line)
endInd, streamInd, err = model.DetectKeywordsWithContext(c, line)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why don't we leave the original method name DetectKeywords and pass in as 2nd parm the context from the caller. does that make any sense?

Copy link
Collaborator

@hhrutter hhrutter left a comment

Choose a reason for hiding this comment

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

There is a(n old) bug in posFloor:

if pos1 < pos2 {
    return pos2
}

should be

if pos1 < pos2 {
    return pos1
}

@hhrutter
Copy link
Collaborator

Happy to merge if you can take care of these 2 issues.

@tmm1
Copy link

tmm1 commented Feb 11, 2025

@fancycode ping

@hhrutter hhrutter force-pushed the avoid-duplicate-keyword-search branch from 1d7b124 to d94329a Compare February 21, 2025 14:33
@hhrutter hhrutter merged commit eb132fb into pdfcpu:master Feb 21, 2025
15 checks passed
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.

Improve the speed of reading basic PDF information
3 participants