-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
simplify skiplist inclusion/cimport to be more cythonize-friendly #18420
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
Changes from 4 commits
d0dbe93
1e80126
35c87b7
0fde045
64d5482
81b1762
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# -*- coding: utf-8 -*- | ||
# cython: profile=False | ||
|
||
from cython cimport Py_ssize_t | ||
|
||
from numpy cimport double_t | ||
|
||
|
||
cdef extern from "src/skiplist.h": | ||
ctypedef struct node_t: | ||
node_t **next | ||
int *width | ||
double value | ||
int is_nil | ||
int levels | ||
int ref_count | ||
|
||
ctypedef struct skiplist_t: | ||
node_t *head | ||
node_t **tmp_chain | ||
int *tmp_steps | ||
int size | ||
int maxlevels | ||
|
||
skiplist_t* skiplist_init(int) nogil | ||
void skiplist_destroy(skiplist_t*) nogil | ||
double skiplist_get(skiplist_t*, int, int*) nogil | ||
int skiplist_insert(skiplist_t*, double) nogil | ||
int skiplist_remove(skiplist_t*, double) nogil | ||
|
||
|
||
# Note: Node is declared here so that IndexableSkiplist can be exposed; | ||
# Node itself not intended to be exposed. | ||
cdef class Node: | ||
cdef public: | ||
double_t value | ||
list next | ||
list width | ||
|
||
|
||
cdef class IndexableSkiplist: | ||
cdef: | ||
Py_ssize_t size, maxlevels | ||
Node head | ||
|
||
cpdef get(self, Py_ssize_t i) | ||
cpdef insert(self, double value) | ||
cpdef remove(self, double value) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,7 @@ | |
|
||
# Cython version: Wes McKinney | ||
|
||
cdef extern from "math.h": | ||
double log(double x) | ||
from libc.math cimport log | ||
|
||
# MSVC does not have log2! | ||
|
||
|
@@ -16,6 +15,7 @@ cdef double Log2(double x): | |
|
||
cimport numpy as np | ||
import numpy as np | ||
from numpy cimport double_t | ||
|
||
from random import random | ||
|
||
|
@@ -25,10 +25,10 @@ np.import_array() | |
# TODO: optimize this, make less messy | ||
|
||
cdef class Node: | ||
cdef public: | ||
double_t value | ||
list next | ||
list width | ||
# cdef public: | ||
# double_t value | ||
# list next | ||
# list width | ||
|
||
def __init__(self, double_t value, list next, list width): | ||
self.value = value | ||
|
@@ -43,9 +43,9 @@ cdef class IndexableSkiplist: | |
Sorted collection supporting O(lg n) insertion, removal, and | ||
lookup by rank. | ||
""" | ||
cdef: | ||
Py_ssize_t size, maxlevels | ||
Node head | ||
# cdef: | ||
# Py_ssize_t size, maxlevels | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
# Node head | ||
|
||
def __init__(self, expected_size=100): | ||
self.size = 0 | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,15 @@ np.import_array() | |
cimport util | ||
|
||
from libc.stdlib cimport malloc, free | ||
|
||
from libc.math cimport sqrt | ||
|
||
from numpy cimport ndarray, double_t, int64_t, float64_t | ||
|
||
from skiplist cimport (IndexableSkiplist, | ||
node_t, skiplist_t, | ||
skiplist_init, skiplist_destroy, | ||
skiplist_get, skiplist_insert, skiplist_remove) | ||
|
||
cdef np.float32_t MINfloat32 = np.NINF | ||
cdef np.float64_t MINfloat64 = np.NINF | ||
|
||
|
@@ -30,19 +35,9 @@ cdef inline int int_min(int a, int b): return a if a <= b else b | |
|
||
from util cimport numeric | ||
|
||
from skiplist cimport ( | ||
skiplist_t, | ||
skiplist_init, | ||
skiplist_destroy, | ||
skiplist_get, | ||
skiplist_insert, | ||
skiplist_remove) | ||
|
||
cdef extern from "../src/headers/math.h": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i wonder if this makes any difference (the sqrt & log above) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be pretty weird (isn't the path there incorrect anyway?) but no harm in reverting it just to see... Since skiplist is only used in window, and used to be "include"d anyway, I think pasting it at the bottom of window is a decent fallback option. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, reverting the
I'll push with this change and open an issue to check for other places where libc.sqrt is cimported instead of the nogil version. |
||
double sqrt(double x) nogil | ||
cdef extern from "src/headers/math.h": | ||
int signbit(double) nogil | ||
|
||
include "skiplist.pyx" | ||
|
||
# Cython implementations of rolling sum, mean, variance, skewness, | ||
# other statistical moment functions | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are you commenting these out? just to show what the structure is? I would remove this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aren't you supposed to declare the member variables in the .pyx, and not the .pxd?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAICT the rule is that if the class is declared in the .pxd, the member variables need to be declared there (and only there)
I leave these in place so we don't have to go back and check the pxd