RNA: Add missing raw types for DNA types #115761
No reviewers
Labels
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#115761
Loading…
Reference in New Issue
No description provided.
Delete Branch "Mysteryem/blender:fix_missing_raw_types_pr"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
RNA raw types were missing for the
int8_t
,uchar
(uint8_t
),ushort
(uint16_t
),int64_t
anduint64_t
DNA types types.This patch adds the missing RNA raw types for all DNA types that can have
raw access.
Functional changes
Properties with one of the new unsigned raw types will raise a Python
OverflowError
in#foreach_getset
when attempting to read a negativeinteger from
bpy_prop_collection.foreach_set()
. This is similar to theexisting behaviour of providing a Python
int
which is too large torepresent as a C
long
. The existing#foreach_getset
code will printthe
OverflowError
and then raise aTypeError
instead.CPython's signed integer parsing functions accept numeric types that are
not the Python
int
instances by calling their__index__()
method.CPython's unsigned integer parsing functions, however, only accept
Python
int
instances. To make#foreach_getset
accept the samenumeric types for unsigned raw types as it already accepts for signed
raw types, the unsigned integer parsing functions in
py_capi_utils.h
have been updated to also call the
__index__()
method when given anargument which is not a Python
int
instance.Because the new unsigned integer parsing in
#foreach_getset
is usingthe
PyC_
family of functions, which perform their own overflow checks,the existing signed integer parsing has also been updated to use the
PyC_
family of functions for signed integer parsing. Previously,OverflowError
would only have been raised when the parsed integer wastoo large to fit in a C
long
. With this patch,OverflowError
will beraised whenever the parsed integer is too large to fit in the property's
raw type. Integer properties already have well-defined maximum and
minimum values which should fit within the property's raw type, and enum
properties have a fixed number of values they can take depending on
their items. The bigger change here, is that setting bool properties
which use
PROP_RAW_BOOLEAN
will now only accept0
/False
and1
/True
.Now that
PROP_RAW_CHAR
parsing is using#PyC_Long_AsU8
,signed char
buffers ("b" format) have been updated to no longer beconsidered compatible with
PROP_RAW_CHAR
, matching the behaviour ofthe other unsigned types only being considered compatible with unsigned
buffer formats.
The
int64_t
anduint64_t
types can currently only be used by boolproperties (see
IS_DNATYPE_BOOLEAN_COMPAT
and the other macros inRNA_define.hh
), but bool properties only have raw access when they donot use a bitmask and it doesn't make much sense to use an entire 64
bits just for a single bool property, so
PROP_RAW_INT64
andPROP_RAW_UINT64
are expected to be unused.Performance changes
Providing raw types allows for faster access through
rna_access.cc#rna_raw_access
, especially when a buffer compatible withthe property's raw type is passed through from
bpy_rna.cc#foreach_getset
.Before this patch, the
bpy.types.Keyframe.handle_left_type
propertydid not have raw access, so
#foreach_getset
would fall back toPROP_RAW_INT
being the compatible type and then use the slower loop in#rna_raw_access
.With this patch, the
bpy.types.Keyframe.handle_left_type
property hasraw access with the
PROP_RAW_UINT8
type. Using a buffer compatiblewith this raw type can use the faster
#memcpy
loop in#rna_raw_access
. Using a Python list will iterate the list into anarray whose type matches
PROP_RAW_UINT8
, which will also use thefaster
#memcpy
loop in#rna_raw_access
.Given an
FCurve
with 10500 keyframe points:foreach_get
with thehandle_left_type
propertyint
compatible before,uint8_t
compatibleafter)
short
)foreach_set
with thehandle_left_type
propertyint
compatible before,uint8_t
compatibleafter)
short
)Most of the before and after cases appear to scale very closely to O(n),
so 10 times fewer elements is 10 times faster. The only exception is the
Compatible buffer case, which scales better in the after case. Given an
FCurve
with 1000 keyframe points,foreach_set
is only 8.3 timesfaster than before.
The handling of unsigned types in
#foreach_getset
may need some discussion because the signed/unsigned of a property's raw type is not exposed to the Python API, which can make it more difficult for addon developers to create performant code that usesforeach_get
/foreach_set
(by passing in a compatible buffer).In cases where differently named types are used interchangeably, e.g.
ushort
anduint16_t
, I went with theuint16_t
and similar types since they're suggested in the style guide, though I did not modify the existing types such asshort
->int16_t
forPROP_RAW_SHORT
.Fix #115413: Missing RNA raw types for some DNA typesto WIP: Fix #115413: Missing RNA raw types for some DNA typesWIP: Fix #115413: Missing RNA raw types for some DNA typesto WIP: RNA: Add missing raw types for some DNA typesWIP: RNA: Add missing raw types for some DNA typesto WIP: RNA: Add missing raw types for DNA typesWIP: RNA: Add missing raw types for DNA typesto RNA: Add missing raw types for DNA types@ -271,0 +277,4 @@
* means they cannot parse NumPy scalars and other numeric types.
*/
ulong PyC_Long_AsUnsignedLong(PyObject *value);
unsigned long long PyC_Long_AsUnsignedLongLong(PyObject *value);
It does look a little odd having
unsigned long long
next toulong
. I could addtypedef unsigned long long ulonglong;
toBLI_sys_types.h
and then change use ofunsigned long long
in this PR toulonglong
.From the Blender point of view we care about these being
uint32_t
anduint64_t
. So I would suggest to use those types here andAsU32
andAsU64
in the function name.I didn't think that
ulong
would be guaranteed to fit in auint32_t
since I thought it was usually 64-bit on 64-bit Linux.Similarly for fitting(Edit: I was thinking ofunsigned long long
intouint64_t
which apparently is sometimes 128-bit or 96-bit padded to 128-bit.long double
, I'm not sure in what caseslong long
could be larger than 64-bit.) Unless Blender is compiled with settings that force these types to be those fixed sizes? I've seen assumptions about the size ofint
and smaller types in other parts of Blender's source, but notlong
andlong long
.Since the intent is to replace
#PyLong_AsUnsignedLong
and#PyLong_AsUnsignedLongLong
from CPython's API, I thought it would be better to use their return types and keep the names similar. There are also already implementations for#PyC_Long_AsU32
and similar inpy_capi_utils.h
.I've had a further think about this and I don't think
ulong
potentially being 64-bit will be an issue because there is noPROP_RAW_ULONG
and code should be using fixed-width types where possible.I can instead implement the
__index__()
fallback directly intoPyC_Long_AsU64
,PyC_Long_AsU32
,PyC_Long_AsU16
andPyC_Long_AsU8
. I would remove both of the new functions inpy_capi_utils.h
and refactorPyC_Long_AsUnsignedLong
into astatic ulong pyc_Long_AsUnsignedLong(PyObject *value)
function inpy_capi_utils.cc
, to avoid duplicate code in the 32-bit and smaller functions.This would keep
py_capi_utils.h
using fixed-width types for integer parsing and would encourage the use of one of the fixed-width unsigned integer parsing functions instead of usingPyLong_AsUnsignedLong
orPyLong_AsUnsignedLongLong
.This sounds good to me! In general we want to avoid using
long
in Blender wherever we can, in the places we do need it having it more contained like this helps.@ideasman42 @brecht Can I get a review from you both on this please? It looks like Campbell worked a lot on
bpy_rna.cc#foreach_getset
andpy_capi_utils.h
and Brecht onrna_access.cc#rna_raw_access
.Marking as request changes.
@blender-bot build
@ -1639,0 +1673,4 @@
PyObject *value_converted = PyNumber_Index(value);
if (value_converted == nullptr) {
/* A `TypeError` will have been raised. */
return unsigned long long(-1);
I didn't know that functional-style casts can only be done with simple type specifiers, and for some reason it does compile on Windows.
My thoughts for fixing this would be either using
static_cast<unsigned long long>(-1)
or addingtypedef unsigned long long ulonglong;
toBLI_sys_types.h
and then doingulonglong(-1)
. Though, depending on feedback from the already requested changes, this code may be replaced with using fixed-with integer types, which would avoid the issue.Not sure about how it fits with the other Python code, but if possible, fixed width types seem nicest. Other than that
static_cast
is totally fine here. Appreciate your desire to follow the style guide though :)@blender-bot build
Thanks looks good to me.
I've updated the PR description to group together the changes based on whether they are functional or for performance, and I've updated and added the functional changes of the last few commits, reproduced below.
Updated:
New:
@ -5497,3 +5520,3 @@
break;
case PROP_RAW_BOOLEAN:
((bool *)array)[i] = int(PyLong_AsLong(item)) != 0;
((bool *)array)[i] = bool(PyC_Long_AsBool(item));
I think it may be better to revert the change to bool parsing here because there is the
py_capi_utils.h#PyC_AsArray()
function, which is a good candidate for replacing the conversion from Python sequence to array here, and its bool parsing matches the existingint(PyLong_AsLong(item)) != 0
rather than using#PyC_Long_AsBool()
. Reverting this change to bool parsing should make any future transition to using#PyC_AsArray()
easier.Edit: Or maybe not. While
#PyC_AsArray()
is faster forList
/Tuple
inputs, it appears to be slower for other sequences, such as NumPy ndarray, array.array and memoryview.I'll wait with merging this in case @ideasman42 wants to have a look at the Python implementation.
I think enough time has passed, merging.