Skip to content

Conversation

@takluyver
Copy link
Contributor

It looks like the uses of buffer() here were to do a non-copying slice - i.e. buffer(data, p) is equivalent to data[p:], but it points to the existing memory used for data rather than copying it.

I'm proposing this change for discussion: it replaces the buffer calls with standard slicing. If bytes objects are passed in, this will copy data, with some potential performance loss - how much depends on how many times we're slicing each chunk of data. The tests are not measurably slower.

If the overhead of copying is significant, the caller of the parsing machinery can pass in a memoryview object, on which slices should be non-copying. I think this should mostly work with no changes to this code, but if that's important, we should obviously test it.

This reduces the number of test failures on Python 3 to 251 (26 failures, 225 errors). Tests still pass on Python 2.

It looks like the uses of buffer() here were just to do a non-copying
slice - i.e. buffer(data, p) is equivalent to data[p:], but it points to
the existing memory used for data rather than copying it.

I'm proposing this change for discussion: it replaces the buffer calls
with standard slicing. If bytes objects are passed in, this will copy
data, with some potential performance loss - how much depends on how
many times we're slicing each chunk of data. The tests are not
measurably slower.

If the overhead of copying is significant, the caller of the parsing
machinery can pass in a memoryview object, on which slices should be
non-copying. I think this should mostly work with no changes to this
code, but if that's important, we should obviously test it.

This reduces the number of test failures on Python 3 to 251 (26
failures, 225 errors). Tests still pass on Python 2.
@codecov-io
Copy link

Current coverage is 88.80%

Merging #12 into master will increase coverage by +0.05% as of 85e5d27

@@            master    #12   diff @@
=====================================
  Files           10     10       
  Stmts         1876   1876       
  Branches         0      0       
  Methods          0      0       
=====================================
+ Hit           1665   1666     +1
  Partial          0      0       
+ Missed         211    210     -1

Review entire Coverage Diff as of 85e5d27

Powered by Codecov. Updated on successful CI builds.

@petli
Copy link
Contributor

petli commented Feb 18, 2016

A bit of background here: the extreme uses of buffer and the generated functions for packing/unpacking were necessary to be able to run PLWM on a PentiumII laptop in 1999. It should fine to rewrite it all to use more straight-forward code when that is simpler, even on a Raspberry Pi these days. The unit tests can check that messages are packed and unpacked correctly with the rewritten code.

@takluyver
Copy link
Contributor Author

That's good to know, thanks!

@vasily-v-ryabov
Copy link
Collaborator

This is also fine to me. Merging...

vasily-v-ryabov added a commit that referenced this pull request Feb 18, 2016
Remove some uses of buffer()
@vasily-v-ryabov vasily-v-ryabov merged commit 6b3e1bd into python-xlib:master Feb 18, 2016
takluyver added a commit to takluyver/python-xlib that referenced this pull request Feb 18, 2016
Struct.to_binary, Struct.parse_value and Struct.parse_binary were doing
code generation to produce and store a specialised method for each
struct instance. Apparently (see python-xlib#12) this was a necessary optimisation
in the distant past, but it's almost certainly no longer needed, and it
makes the code considerably harder to follow.

On my machine the tests run slightly but noticeably faster with this
change, but of course the tests are probably not a realistic sample for
performance testing.

This doesn't directly impact Python 3 support, but it simplifies things.
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.

4 participants