Skip to content

Better _binary module use#4614

Closed
homm wants to merge 2 commits intopython-pillow:masterfrom
uploadcare:better-binary-use
Closed

Better _binary module use#4614
homm wants to merge 2 commits intopython-pillow:masterfrom
uploadcare:better-binary-use

Conversation

@homm
Copy link
Copy Markdown
Member

@homm homm commented May 8, 2020

  • remove extra i8 calls where input is proved bytes[] or int. Most of i8 calls are not required in py3.
  • use offset for all binary input functions instead of slicing which should be slightly faster especially when slicing "until the end" is used (s[4:]).

if greyscale:
if not linear:
self.lut = [i8(c) for c in palette[:256]]
self.lut = list(palette[:256])
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In [1]: [c for c in b'test']
Out[1]: [116, 101, 115, 116]

In [2]: list(b'test')
Out[2]: [116, 101, 115, 116]

@homm
Copy link
Copy Markdown
Member Author

homm commented May 8, 2020

I don't have enough permissions to restart build on Travis (

@hugovk
Copy link
Copy Markdown
Member

hugovk commented May 8, 2020

I don't have enough permissions to restart build on Travis (

Maybe you do: try logging out and back in to Travis, that brought back the restart buttons for me recently.

@homm
Copy link
Copy Markdown
Member Author

homm commented May 8, 2020

Thanks, it works)

if not byte:
break
byte = i8(byte)
byte = byte[0]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it just me, or is byte = byte[0] not as clear as byte = i8(byte) about what is happening here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You are right that i8() looks more clear at first glance. Mostly because indexing had changed from Py2 to Py3 for bytes. However, this saves one function call and type checking on each value. Also, IDEs handle types correctly and show variables as int.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

As an alternative, it is possible to change i8 and use it everywhere for consistency:

def i8(c, o=0):
    return c[o]

@radarhere
Copy link
Copy Markdown
Member

radarhere commented Dec 30, 2020

I've created a rebased version of this as #5156

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants