Skip to content
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

Allocate less in limited sparse printing #22604

Merged
merged 4 commits into from
Jul 4, 2017
Merged

Allocate less in limited sparse printing #22604

merged 4 commits into from
Jul 4, 2017

Conversation

tkelman
Copy link
Contributor

@tkelman tkelman commented Jun 28, 2017

Avoids unnecessary allocation and reversal of an array of outputs from the method in #22536. Add a few more test cases with dense columns to be sure this is consistent with the output from master. The new tests should pass with or without the rest of the change.

edit: mac travis hung while downloading something, log backed up to https://gist.github.com/6433751028a10ec3d17391cd57bf0c26 and restarted

@tkelman tkelman added domain:display and printing Aesthetics and correctness of printed representations of objects. domain:arrays:sparse Sparse arrays labels Jun 28, 2017
iterate and print in forwards order instead of saving to an array and reversing
use searchsortedfirst to find which column to start in

fix tests for 32 bit
@@ -1779,6 +1779,21 @@ end
show(ioc, MIME"text/plain"(), sparse(Int64[1, 1], Int64[1, 2], [1.0, 2.0]))
@test String(take!(io)) == "1×2 SparseMatrixCSC{Float64,Int64} with 2 stored entries:\n ⋮"

# even number of rows
ioc = IOContext(IOContext(io, :displaysize => (8, 80)), :limit => true)
Copy link
Member

@Sacha0 Sacha0 Jun 30, 2017

Choose a reason for hiding this comment

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

(Tangent: The docstrings for IOContext would benefit from more examples. Edit: Opened an intro issue (#22637). )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

very much so, that feature has been underdocumented ever since it was added

@@ -1779,6 +1779,21 @@ end
show(ioc, MIME"text/plain"(), sparse(Int64[1, 1], Int64[1, 2], [1.0, 2.0]))
@test String(take!(io)) == "1×2 SparseMatrixCSC{Float64,Int64} with 2 stored entries:\n ⋮"

# even number of rows
ioc = IOContext(IOContext(io, :displaysize => (8, 80)), :limit => true)
Copy link
Member

Choose a reason for hiding this comment

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

Could simplify the nested IOContext calls to ioc = IOContext(io, displaysize = (8, 80), limit = true) (assuming I understand the docstring correctly and my experiments at the REPL confirm what they appear to)?

@@ -1779,6 +1779,21 @@ end
show(ioc, MIME"text/plain"(), sparse(Int64[1, 1], Int64[1, 2], [1.0, 2.0]))
@test String(take!(io)) == "1×2 SparseMatrixCSC{Float64,Int64} with 2 stored entries:\n ⋮"

# even number of rows
ioc = IOContext(IOContext(io, :displaysize => (8, 80)), :limit => true)
show(ioc, MIME"text/plain"(), sparse(Int64[1,2,3,4], Int64[1,1,2,2], [1.0,2.0,3.0,4.0]))
Copy link
Member

@Sacha0 Sacha0 Jun 30, 2017

Choose a reason for hiding this comment

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

(Another tangent: That methods of show accepting a stream aren't !-terminated seems inconsistent with the usual convention? Edit: Did not find anything during a brief issue search, so noted in the API consistency thread #20402 (comment).)

print(io, "\n \u22ee")
# find the column to start printing in for the last print_count elements
nextcol = searchsortedfirst(S.colptr, nnz(S) - print_count + 1)
for r = nnz(S) - print_count + 1 : S.colptr[nextcol] - 1
Copy link
Member

Choose a reason for hiding this comment

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

Though not strictly necessary, parens would make the range much easier to parse?

Copy link
Member

@Sacha0 Sacha0 left a comment

Choose a reason for hiding this comment

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

lgtm modulo the minor comments! :) (Caveat: Haven't thought through the potential edge cases.)

Copy link
Member

@Sacha0 Sacha0 left a comment

Choose a reason for hiding this comment

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

lgtm for the additional commit! :)

@tkelman
Copy link
Contributor Author

tkelman commented Jul 3, 2017

Will merge tomorrow then if I don't hear otherwise

@tkelman tkelman merged commit 0ab349c into master Jul 4, 2017
@tkelman tkelman deleted the tk/less-allocation branch July 4, 2017 00:29
@@ -1807,7 +1807,7 @@ end
@test String(take!(io)) == string("6×3 SparseMatrixCSC{Float64,$Int} with 18 stored entries:\n [1, 1]",
" = 1.0\n [2, 1] = 1.0\n ⋮\n [5, 3] = 1.0\n [6, 3] = 1.0")

ioc = IOContext(io, :displaysize => (9, 80))
ioc = IOContext(io, displaysize = (9, 80))
Copy link
Member

Choose a reason for hiding this comment

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

As of today, it seems to be faster and to allocate less to use the Pair version rather than keywords; so this commit was more for code-style rather than for reducing allocations right?

Copy link
Contributor Author

@tkelman tkelman Aug 17, 2017

Choose a reason for hiding this comment

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

the iocontext part was stylistic yes (see collapsed review at #22604 (comment)), but in test code would the difference be noticeable?

Copy link
Member

Choose a reason for hiding this comment

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

OK thanks; I don't believe it would be noticeable in test code, but in 893a65b Jameson did exactly the opposite for other test files, apparently in a performance related PR -- but he may have done it in test files only for stylistic reasons too ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:arrays:sparse Sparse arrays domain:display and printing Aesthetics and correctness of printed representations of objects.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants